0%

字节流

字节输出流

1
2
3
4
5
6
7
8
9
10
11
12
13
public void writeFile() throws Exception{
Scanner input = new Scanner(System.in);
//将文件封装为File对象
File file = new File("X:\\xxx.xxx");
//输出数据
String str = input.next();
//字节输出流
FileOutputStream fos = new FileOutputStream(file,true);
//将代码数据保存到磁盘
fos.write(str.getBytes());
//关闭流
fos.close();
}

字节输入流

1
2
3
4
5
6
7
8
9
10
11
12
13
public void readFile() throws Exception{
//将文件封装为File对象
File file = new File("X:\\xxx.xxx");
建立磁盘通向程序代码的管道(字节输入流)
FileInputStream fis = new FileInputStream(file);
int len = 0;
//读取数据
while((len = fis.read()) != -1){
System.out.print((char)len);
}
//关闭流
fis.close();
}

(缓冲流)文件的复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void copyVideoQuik() throws Exception{
//磁盘通往代码的管道
FileInputStream fis = new FileInputSteam("X:\\xxx.xxx");
//管道加粗
BufferedInputSteam bis = new BufferedInputSteam(fis);
//代码通往磁盘的管道
FileOutputStream fos = new FileOutputStream("X:\\xxx.xxx");
//管道加粗
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] car = new byte[1024];//一次读取1024字节(速度最优)
int len = 0;//len=-1代表读取结束
while((len = bis.read(car)) != -1){
//读取的数据保存到磁盘
bos.write(car,0,len);//从car里读取,从下标0开始,读1024个
//System.out.println(new String(car,0,len));//转字符串逐行打印
}
//关闭流:先关外管道流,再关内管道流
//关输入流
bis.close();
fis.close();
//关输出流
bos.close();
fos.close();
}
------ THEEND ------

欢迎关注我的其它发布渠道