【每日日报】第三十三天---文件操作(缓冲流)
1 今天继续看书
缓冲流读文件
1 package File;
2 import java.io.BufferedReader;
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 public class BufferedReaderDemo {
8 public static void main(String[] args)throws IOException{
9 read();
10 }
11 public static void read() throws IOException{
12 BufferedReader read=new BufferedReader (new InputStreamReader(new FileInputStream("D:/Hello.txt")));
13 String line=null;
14 while((line=read.readLine())!=null){
15 System.out.println(line);
16 }
17 read.close();
18 }
19
20 }
缓冲流写文件
1 package File;
2 import java.io.BufferedWriter;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStreamWriter;
6
7 public class BufferedWriterDemo {
8 public static void main(String[] args)throws IOException{
9 write();
10 }
11 public static void write()throws IOException{
12 BufferedWriter w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/Hello.txt")));
13 w.write("Hello");
14 w.write("Java");
15 w.close();
16 }
17
18 }
使用BufferedInputStream和BufferedOutputStream读写图片
1 package File;
2 import java.io.BufferedInputStream;
3 import java.io.BufferedOutputStream;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 public class BufferedInputOutput {
9 public static void main(String[] args)throws IOException{
10 outIn();
11 }
12 public static void outIn()throws IOException{
13 BufferedInputStream in=new BufferedInputStream(new FileInputStream("C:\\Users\\linmob\\Pictures\\ps\\a.png"));
14 BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("C:\\Users\\linmob\\Pictures\\ps\\a2.png"));
15 byte[] buf=new byte[1024];
16 int len=-1;
17 while((len=in.read(buf))!=-1){
18 out.write(buf,0,len);
19 }
20 out.close();
21 in.close();
22 }
23 }
2 没有其他问题
3 明天继续看书
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步