1.
使用DataInputStream/DataOutputStream来读写二进制文件
小结
FileReader int b=fr.read(); 读取一个字符
int length=fr.read(char []); 读取字符数组
FileWriter fw.write(char); 写入一个字符
fw.write(char[]); 写入字符数组
bufferedReader(字符缓冲流)
BufferedReader br=new BufferedReader(new FileReader("文件路径"))
String str=br.readerLine();读取一行字符
BufferedWriter(字符输出缓冲流)
BufferedWriter bw=new BufferedWriter(new FileWriter("文件路径"));
bw.write(字符串);
缓冲流不能单独存在 读写字符文件
BufferedReader br=new BufferedReader(new FileReader(文件路径)); //读
BufferedWriter bw=new BufferedWriter(new FileWriter(文件路径,boolean)); //写
读写字节文件
DataInputStream dis=new DataInputStream(new FileInputStream(文件路径)); //读
DataOutputStream out=new DataOutputStream(new FileOutPutStream(文件路径)); //写
读取整个字符
String str=null;
while((str=br.readLine)!=null){
System.out.println(str);
}
读取整个字节文件
int b;
while((b=dis.read())!=-1){
System.out.println((char)b);
}
示例:
DataInputStream dis=new DataInputStream(new FileInputStream("src/a.txt"));
DataOutputStream out=new DataOutputStream(new FileOutputStream("src/b.txt"));
// int a=dis.read();
// System.out.println(a);
int b;
//输出文件里面的内容
while((b=dis.read())!=-1){
System.out.print((char)b);
}
//将a文件中的内容赋值到b文件中去
while((b=dis.read())!=-1){
out.write(b);
}
//关闭流
dis.close();
out.close();