IO流
用于读(input)写(output)文件中的数据
1.IO流的分类
2.FileOutputStream
1.操作本地文件的字节输出流,可以把程序中的数据写到本地文件中
2.1书写步骤
2.1创建字节输出流对象
2.2写数据
2.3释放资源
//1.创建对象
FileOutputStream fos = new FileOutputStream("文件路径..."); //默认在根目录下文件夹之间用\\
//2.写出数据
fos.write(97); //a
//3.释放资源
fos.close();
/*
字节输出流的细节:
1.创建字节输出流对象
细节1:参数是字符串表示的路径或者是File对象都是可以的
细节2:如果文件不存在会创建一个新的文件,但是要保证父级路径是存在的
细节3:如果文件已经存在,则会清空文件
2.写数据
细节: write方法的参数是整数 但是实际上写到本地文件中的是整数ASCII码对应的字符
3.释放资源
每次使用完流之后 都要释放资源
*/
2.2写数据
public static void main(String[] args) throws IOException {
//1.创建对象
FileOutputStream fos = new FileOutputStream("后端\\JavaStudy\\src\\com\\grg\\day10\\test\\a.txt");
//2.写出数据
//fos.write(97); //a
byte[] bytes = {97, 98, 99, 100, 101};
//fos.write(bytes); //abcde
//数组、起始索引、个数
fos.write(bytes, 0, 3); //abc
//3.释放资源
fos.close();
}
2.3续写、换行写
public static void main(String[] args) throws IOException {
/*
换行写:再次写出一个换行符就行了
windows: \r回车 \n换行
linux: \n换行
Mac: \r换行
续写: 打开续写开关 默认是false
new FileOutputStream("文件路径", true);
*/
//1.创建对象
FileOutputStream fos = new FileOutputStream("后端\\JavaStudy\\src\\com\\grg\\day10\\test\\a.txt" , true);
//写数据
String str = "abcdefg";
byte[] bytes = str.getBytes();
fos.write(bytes);
String wrap = "\r\n";
byte[] bytes2 = wrap.getBytes();
fos.write(bytes2);
String str2 = "666";
byte[] bytes1 = str2.getBytes();
fos.write(bytes1);
//3.释放资源
fos.close();
}
3.FileInputStream
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("后端\\JavaStudy\\src\\com\\grg\\day10\\test\\a.txt");
//读取文件的一个字节 并且返回其 字符编码code值
int a1 = in.read();
System.out.println(a1);
int a2 = in.read();
System.out.println(a2);
in.close();
}
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("后端\\JavaStudy\\src\\com\\grg\\day10\\test\\a.txt");
byte[] data = new byte[50];
//当前函数返回值 代表读取内容的长度 in.read(byte数组)
int len = in.read(data);
System.out.println(len);
System.out.println(Arrays.toString(data));
//将byte数组 转换成字符串形式
String s = new String(data,0,len);
System.out.println(s);
in.close();
}
4.文件复制
/**
* 文件复制
* @throws Exception
*/
@Test
public void test03() throws Exception{
//1.读取文件
FileInputStream in = new FileInputStream("E:/AAA/123.txt");
//2.写入文件
FileOutputStream out = new FileOutputStream("E:/AAA/321.txt");
byte[] data = new byte[10];
int len = 0;
while ((len = in.read(data)) != -1) {
out.write(data,0,len);
}
in.close();
out.close();
}
5.异常处理
@Test
public void test03() {
FileInputStream in = null;
FileOutputStream out = null;
try {
//1.读取文件
in = new FileInputStream("E:/AAA/123.txt");
out = new FileOutputStream("E:/AAA/321.txt");
byte[] data = new byte[10];
int len = 0;
while ((len = in.read(data)) != -1) {
out.write(data, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
6.字符输出流和字符输入流
@Test
public void test04() throws Exception {
FileWriter w = new FileWriter("E:/AAA/222.txt");
w.write("nihao shijie hahaha");
w.close();
}
@Test
public void test04() throws Exception {
FileReader r = new FileReader("E:/AAA/123.txt");
char[] data = new char[50];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = r.read(data)) != -1){
String s = new String(data, 0, len);
sb.append(s);
}
System.out.println(sb);
r.close();
}
7.四大基流总结
关键字 | 类型 |
---|---|
FileInputStream | 字节输入流(读 |
FileOutputStream | 字节输出流(写 |
FileReader | 字符输入流(读 |
FileWriter | 字符输出流(写 |
8.包装流
在基流的基础之上添加缓冲池
@Test
public void test05() throws Exception {
FileOutputStream out = new FileOutputStream("E:/AAA/123.txt");
BufferedOutputStream outputStream = new BufferedOutputStream(out);
outputStream.write("你好".getBytes());
outputStream.close();
}
@Test
public void test05() throws Exception {
FileInputStream in = new FileInputStream("E:/AAA/123.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
byte[] data = new byte[50];
int b = 0;
StringBuilder sb = new StringBuilder();
while ((b = bufferedInputStream.read(data)) != -1) {
String s = new String(data, 0, b)
sb.append(s);
}
System.out.println(sb);
in.close();
}
9.包装流之对象包装流
对象包装流就是将内存中的Java对象 持久化到 硬盘当中
序列化:将java对象转换成二进制数据流的操作
反序列化:将二进制数据流转换为java对象的操作
@Test
public void test06() throws Exception{
People p1 = new People(1, "好家伙","111", "999");
FileOutputStream out = new FileOutputStream("E:/AAA/people.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject( p1 );
objectOutputStream.close();
}
public class People implements Serializable {
int id;
String nickname;
String level;
String money;
public People() {
}
public People(int id, String nickname, String level, String money) {
this.id = id;
this.nickname = nickname;
this.level = level;
this.money = money;
}
}
如果将某个类设定实现 Serializable接口 此时表明当前类 允许被序列化
Serializable接口作用就是 标记当前类 允许被序列化
10.对象输入流
@Test
public void test07() throws Exception{
FileInputStream in = new FileInputStream("E:/AAA/people.txt");
ObjectInputStream inputStream = new ObjectInputStream(in);
Object o = inputStream.readObject();
People people = (People) o;
inputStream.close();
System.out.println(o);
}
如果输出类之前,类发生了改变 会抛出异常 显示版本不匹配
private static final long serialVersionUID = -6849794470754667710L;
在类中加入 序列化版本id
11.包装流总结
关键字 | 类型 |
---|---|
BufferedOutputStream | 缓冲输出流 |
BufferedInputStream | 缓冲输入流 |
ObjectOutputStream | 对象输出流 |
ObjectInputStream | 对象输入流 |