Java—字节流读写数据
2021-05-09 本文已影响0人
夜希辰
学习笔记:字节流。
字节流抽象基类:
- public abstract class InputStream implements Closeable {}
//OutputStream是所有字节输入流的超类
- public abstract class OutputStream implements Closeable, Flushable {}
//OutputStream是所有字节输出流的超类
- FileOutputStream:文件输出流将数据写如File,FileOutputStream是OutputStream子类。
public FileOutputStream(String name):创建文件输出流以指定的名称写入文件
- FileInputStream :从文件中获取输入字节,FileInputStream是InputStream子类。
public FileInputStream(String name)
目录:
一、字节流写数据
1、字节流写数据的三种方式
2、字节流写数据如何换行,如何追加写入
二、字节流读数据
1、字节输入流读数据:一次读一个字节数据
2、字节输入流复制文本文件
3、字节数组流读数据:一次读一个字节数组数据
4、字节缓冲流
一、字节流写数据
1、字节流写数据的三种方式

public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//public FileOutputStream(String name):创建我呢见输出流以指定的名称写入文件
FileOutputStream fos = new FileOutputStream("idea_test\\fos.txt");//idea_test为当前模块
//public void write(int b)
fos.write(97);
//public void write(byte b[])
byte[] bys = {97,98,99,100};
fos.write(bys);
byte[] bytes = "abcdefghigk".getBytes();
fos.write(bytes,2,3);//cde
//最后都要释放资源
//public void close():关闭此文件输出流并释放与此流相关联的任何系统资源
fos.close();
}
}

2、字节流写数据如何换行,如何追加写入


二、字节流读数据
1、字节输入流读数据
- public int read()
import java.io.*;
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//1、创建字节输入流对象
FileInputStream fis = new FileInputStream("idea_test\\fos.txt");
//2、读数据
/*
//第一次读取数据
int read = fis.read();
System.out.println(read);
//第二次读取数据
read = fis.read();
System.out.println(read);
*/
//2、读数据 优化上面代码
/*
fis.read()读数据
by = fis.read():把读取到的数据赋值给by
by != -1:判断读取到的数据是否是-1
*/
int by;
while((by = fis.read()) != -1){
System.out.println((char)by);
}
//3、释放资源
fis.close();
}
}
2、字节输入流复制文本文件
public class FileDemo {
public static void main(String[] args) throws IOException {
//1、创建字节输入流对象
FileInputStream fis = new FileInputStream("F:\\02-silu\\KETTLE_TEST\\java.txt");
//根据目的地创建字节输出流对象
FileOutputStream fos = new FileOutputStream("idea_test\\java.txt");
//读写数据,复制文本文件
int by;
while((by = fis.read()) != -1){
fos.write(by);
}
//释放资源
fis.close();
fos.close();
}
}
3、字节数组流读数据:一次读一个字节数组数据
import java.io.*;
public class InterfaceDemo {
public static void main(String[] args) throws IOException {
//1、创建字节输入流对象
FileInputStream fis = new FileInputStream("F:\\02-silu\\KETTLE_TEST\\java.txt");
FileInputStream fis1 = new FileInputStream("F:\\02-silu\\KETTLE_TEST\\java.txt");
//读数据:方法一
byte[] bys = new byte[5];
//第一次读数据
int read = fis.read(bys);
System.out.println(read);
System.out.println(new String(bys,0,read));
//第二次读数据
read = fis.read(bys);
System.out.println(read);
System.out.println(new String(bys,0,read));
//第三次读数据
read = fis.read(bys);
System.out.println(read);
// System.out.println(new String(bys,0,read));
//第四次读数据
read = fis.read(bys);
System.out.println(read);
System.out.println("------------------");
// //读数据:方法二。读数据改进
byte[] b = new byte[1024];//1024及整数倍
int len;
while((len = fis1.read(b)) != -1){
System.out.print(new String(b,0,len));
}
//释放资源
fis.close();
fis1.close();
}
}

4、字节缓冲流

public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//1、创建字节缓冲流对象
FileInputStream fis = new FileInputStream("F:\\02-silu\\KETTLE_TEST\\java.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//创建字节缓冲流对象,方式2
BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("F:\\02-silu\\KETTLE_TEST\\java.txt"));
fis.close();
bis.close();
}
}