java_IO_2
2024-08-30 21:37:55
1.字节流 InputStream(抽象类)
package ioStudy; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy2 {
public static void main(String[] args) {
File file = new File("test.txt"); //创建源 InputStream is = null; try {
is = new FileInputStream(file); // 选择流
int temp;
while ((temp = is.read()) != -1) { //一次读一个字节 如果包含中文可能会出现乱码 考虑使用字符流
System.out.print((char) temp); //操作
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close(); //释放资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package ioStudy; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy3 {
public static void main(String[] args) {
File file = new File("test.txt");
InputStream is = null;
try {
is = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) { //一次读取多个
String s = new String(buffer,0,len);
System.out.print(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.OutputStream
package ioStudy; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy4 {
public static void main(String[] args) {
File file = new File("output.txt"); // 不存在会自动创建
OutputStream os = null; try {
os = new FileOutputStream(file,true); //开启向后追加 不然每次都删掉原来的 再写 默认的是false
String temp = "hello world!\r\n";
byte[] b = temp.getBytes();
os.write(b, 0, b.length);
os.flush(); //刷新内存
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
3.实现文件的copy
package ioStudy; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class Copy {
public static void main(String[] args) {
copy("test.txt","testcopy.txt");
} public static void copy(String source, String destination) {
File src = new File(source);
File dest = new File(destination);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest); byte[] buffer = new byte[1034];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
os.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 流关闭的原则 先打开的后关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
最新文章
- MySQL的简单使用和JDBC示例
- 黑马程序员+Winform基础(下)
- Javascript之运动框架2
- Effective C++条款01: 视C++为一个语言联邦
- Linux 混杂设备、外部中断和输入子系统
- android 内部存储相关知识点: getfilestreampath getDir 子文件夹
- SQL后台分页三种方案和分析
- Swift 2.0 UIAlertView 和 UIActionSheet 的使用
- webstorm安装与本地激活
- TCP:传输控制协议
- celery学习笔记2
- Numpy库的学习(五)
- SpringBoot之基础
- DOM对象与jquery对象相互转换
- CentOS7编译安装mysql-5.6.43
- 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS
- 进程 day36
- Mac 安装 Ruby, Rails 运行环境
- unmappable character for US-ASCII
- C#学习笔记(20)——使用IComparer(自己写的)