在计算机中文件的分类
- 二进制文件
- 文本文件
在java中流的分类
- 字节流:操作二进制文件
- 字符流: 操作文本文件
流: stream 系列的都是字节流
图片.png
- InputStream 输入字节流的超类
- OutPutStream 输出字节流的超类
- FileInputStream 文件输入流,用于从文件中获得字节输入流
- FileOutputStream 文件输出流
- BufferedInputStream InputStream的子类,使用装饰者模式对FileInputStream进行装饰
- BufferedOutputStream
- DataInputStream:InputStream的子类,使用装饰者模式对输入输出流进行装饰,将变量输出到流
- DataOutputStream: 将数据读出,按照顺序读出
字节流使用
图片.png
使用FIleInput/OutputStream
//使用FileInput / OutputStream
//一个字节一个字节搞,太慢,可以使用BufferedInputStream提高速度
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream(new File("d:/img/1.mp4"));
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/img/2.mp4"));
int len = -1;
while ((len = fileInputStream.read())!=-1) {
fileOutputStream.write(len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
拷贝一个100M的视频所需时间(未使用缓存).png
使用BufferedInput/OutputStream ,这个类自带buff 为8K
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("d:/img/1.mp4"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("d:/img/2.mp4"));
int len = -1;
while ((len=bufferedInputStream.read())!=-1) {
bufferedOutputStream.write(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用自带buff的流来拷贝.png
使用DataInput / OutStream
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(new File("d:/img/1.txt")));
dataOutputStream.writeInt(132);
dataOutputStream.writeDouble(0.01);
dataOutputStream.writeUTF("这是一个字符串");
//读取刚才通过DataOutputStream 写入的数据
//要按照写入的顺序读取
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(new File("d:/img/1.txt")));
int readInt = dataInputStream.readInt();
double readDouble = dataInputStream.readDouble();
String readUTF = dataInputStream.readUTF();
System.out.println(readInt);
System.out.println(readDouble);
System.out.println(readUTF);
使用ObjectInput / OutputStrem 之前,需要将要写入的对象的类序列化,不然会引发异常
//将一个student对象写入
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
super();
}
public Student(String name, String address) {
super();
this.name = name;
this.address = address;
}
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("d:/img/1.abc")));
objectOutputStream.writeObject(student);
//尝试将对象读出来
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("d:/img/1.abc")));
Student student2 = (Student) objectInputStream.readObject();
System.out.println(student2);
字符流使用
通过字符流可以很方便的将字符写入writer
使用BufferedWriter/Reader
使用完Buffered后如果没有flush和close,数据将不会被写入进文件
//bufferWriter 若目标文件不存在,会自动创建文件
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/img/2.abc"))));
bufferedWriter.write("这个是一个字符串");
bufferedWriter.write("qweasd");
bufferedWriter.flush();
bufferedWriter.close();
//使用BufferedReader来将数据读出
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("d:/img/2.abc")));
String string = null;
StringBuilder stringBuilder = new StringBuilder();
while ((string = bufferedReader.readLine())!=null) {
stringBuilder.append(string);
}
使用FileWriter/Reader
若文件不存在,将自动创建文件
FileWriter fileWriter = new FileWriter(new File("d:/img/2.txt"));
fileWriter.write("这是一个字符串");
fileWriter.write("\n");
fileWriter.write("这是一个字符串2哦");
fileWriter.flush();
fileWriter.close();
//读取数据
FileReader fileReader = new FileReader(new File("d:/img/2.txt"));
int len = -1;
StringBuilder stringBuilder = new StringBuilder();
char[] buff = new char[512];
while ((len = fileReader.read(buff)) != -1) {
stringBuilder.append(buff, 0, len);
}
System.out.println(stringBuilder);
使用OutputStreamWriter/InputStreamReader
如果目标文件不存在,将会创建文件
图片.png
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("d:/img/3.txt"));
outputStreamWriter.write("这是一个男字符串");
outputStreamWriter.write("这是一个女字符串");
outputStreamWriter.flush();
outputStreamWriter.close();
//读取数据
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("d:/img/3.txt"));
int len = -1;
char[] buff = new char[512];
StringBuilder sBuilder = new StringBuilder();
while ((len = inputStreamReader.read(buff))!=-1) {
sBuilder.append(buff,0,len);
}
System.out.println(sBuilder);
网友评论