总结:
File 类 仅能获取文件的属性数据或者是修改文件的属性数据而已,不能读取文件内容数据。
如果需要操作文件内容数据,那么就需要学习”IO流”技术
IO流类别:
1. 流向划分
输入流
输出流
什么时候使用输入流什么时候使用输出流?
以当前程序作为参照物,数据流入 则使用输入流, 数据流出则使用输出流。
- 处理单位:
字节流:字节流就是用于读取文件的字节数据的,读取到的数据不会经过任何的处理。
字符流: 读取到的字节数据还会帮你转换成你看得懂的字符数据,读取的是以字符作单位 的数据。 字符流 = 字节流+ 解码
1字节流
输入字节流:
———|InputStream 抽象类 输入字节流的基类。
————| FileInputStream 读取文件数据 的输入字节流
使用FileInputStream 读取文件数据步骤:
-
找到目标文件
-
建立数据的输入通道
- 读取文件数据
1.1.1 常用的读取文件数据方式:使用循环配合缓冲 数组读取 能读取完一个文件数据,速度快
public static void read() throws IOException{
File file = new File("F:\\a.txt");
FileInputStream fileInputStream= new FileInputStream(file);
byte[] buf = new byte[1024];
int length = 0 ;
while((length = fileInputStream.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
fileInputStream.close();
}
1.2 OutputStream
输出字节流:
——–| OutputStream 抽象类,所有输出字节字节流的父类。
————| FileOutputStream 向文件输出数据的输出字节流。
使用FileOutputStream步骤:
1. 找到目标文件
2. 建立数据的输出通道
3. 准备数据,把数据写出
4. 把字符串转成字节数组
5. 字节数组写出
6. 关闭资源
FileOutputStream要注意的细节:
1. new FileOutputStream 的时候,如果目标文件不存在,那么会先创建目标 文件,然后再写入。
2. new FileOutputStream(file) 如果目标文件已经存在,那么会先清空 目标文件的数据,然后再写入新的数据.
3. 写入数据的时候如果需要以追加的形式写入,那么需要使用new FileOutputStream(file,true) 这个构造函数。
4. 使用write(int b)方法的时候,虽然参数接受的一个int类型的数据,但是实际上只会把数据的低八位写出,其他24位丢弃。
00000000-00000000-00000001-10000000
write(byte[] b, int off, int len)
1.2.1常用写出的三种方式
public static void write1() throws IOException{
File file = new File("F:\\a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
String str = "\r\nhello world";
byte[] buf = str.getBytes();
fileOutputStream.write(buf);
fileOutputStream.close();
}
public static void write2() throws IOException {
File file = new File("F:\\a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
String data = "abcd";
byte[] buf = data.getBytes();
fileOutputStream.write(buf, 0, 2);
fileOutputStream.close();
}
public static void write3() throws IOException{
File file = new File("f:\\a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write('h');
fileOutputStream.write('e');
fileOutputStream.write('l');
fileOutputStream.write('l');
fileOutputStream.write('o');
fileOutputStream.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
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
/*
4. 使用write(int b)方法的时候,虽然参数接受的一个int类型的数据,但是实际上只会把数据的低八位写出,其他24位丢弃。
00000000-00000000-00000001-10000000
*/
public static void writeTest() throws FileNotFoundException, IOException {
File file = new File("F:\\b.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(384);
fileOutputStream.close();
}
1.3 字节流文件拷贝
File inFile = new File("F:\\美女\\1.mp3");
File outFile = new File("E:\\拷贝.mp3");
FileInputStream fileInputStream = new FileInputStream(inFile);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int length = 0 ;
while((length=fileInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
fileOutputStream.close();
fileInputStream.close();
1.4 字节流的异常处理
public static void copyImage(){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try{
File inFile = new File("F:\\美女\\1.jpg");
File outFile = new File("F:\\拷贝.jpg");
fileInputStream = new FileInputStream(inFile);
fileOutputStream = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int length = 0 ;
while((length = fileInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
}catch(IOException e){
System.out.println("拷贝出错了...");
throw new RuntimeException(e);
}finally{
try{
if(fileOutputStream!=null){
fileOutputStream.close();
}
}catch(IOException e){
throw new RuntimeException(e);
}finally{
try{
if(fileInputStream!=null){
fileInputStream.close();
}
}catch(IOException e){
throw new RuntimeException(e);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
1.5 字节缓冲流
使用FileInputStream读取文件数据的时候如何读取效率是最高?
使用字节数组作为缓冲区读取的效率是最高的。
1.5.1 输入字节缓冲流
我们知道使用缓冲字节数组读取文件的效率最高,sun也知道使用缓冲字节数组读取的效率高,这时候sun为了方便我们工作,编写一个缓冲输入字节流给我去使用。
缓冲输入字节流的作用: 提高我们读取文件数据的效率。
输入字节流的体系:
———-| InputStream 抽象类 所有输入字节流的基类
————–| FileInputStream 读取文件数据的输入字节流
————–|BufferedInputStream 缓冲输入字节流 该类的本质其实只是在内部维护了一个8kb的字节数组而已。 主要是为了提高我们的读取文件 的效率。
凡是缓冲流都没有读写文件的能力。
BuffereInputStream注意的事项:
1. BuffereInputStream 的close方法实际上关闭的就是你传递进去的FileInputStream对象。
public static void readTest() throws IOException {
File file = new File("F:\\a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
int content = 0;
while((content = bufferedInputStream.read())!=-1){
System.out.print((char)content);
}
bufferedInputStream.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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
1.5.2 输出字节缓冲流
缓冲输出字节流: 为了提高写文件的效率。
———| OutputStream 抽象类, 所有输出字节流的基类。
————-| FileOutputStream 向文件写出数据的输出字节流对象。
————-| BufferedOutputStream 缓冲输出字节流, 为了提高写文件数据的效率。
BufferedOutputStream 需要注意的事项: BufferedInputStream
1. 使用BufferedOutputStream的write方法时候,数据其实是写入了BufferedOutputStream内部维护的字节数组中,只有你调用BufferedOutputStream的close方法或者是flush方法数据才会真正的写到硬盘上去或者内部维护的字节数组已经存储满数据了,这时候
数据也会写到硬盘上去。
2/. BufferedOutputStream 的close方法实际上关闭的就是你传入的OutputStream对象的close方法。
File file = new File("f:\\a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
String data = "hello world";
bufferedOutputStream.write(data.getBytes());
bufferedOutputStream.close();
1.5.3 使用缓冲输入输出字节流拷贝数据
public class CopyImage {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
File inFile = new File("F:\\美女\\1.jpg");
File outFile = new File("E:\\拷贝.jpg");
FileInputStream fileInputStream = new FileInputStream(inFile);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int content = 0 ;
while((content = bufferedInputStream.read())!=-1){
bufferedOutputStream.write(content);
}
bufferedOutputStream.close();
bufferedInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
2 字符流
2.1 Reader
输入字符流:
———-| Reader 抽象类 所有输入字符流的基类。
—————-| FileReader 读取文件数据的输入字符流。
FileReader的 使用步骤:
1. 找到目标文件。
读文件的两种方式
public static void readTest2() throws IOException{
File file = new File("F:\\Demo1.java");
FileReader fileReader = new FileReader(file);
char[] buf = new char[1024];
int length = 0;
while((length = fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
fileReader.close();
}
public static void readTest() throws IOException{
File file = new File("f:\\a.txt");
FileReader fileReader = new FileReader(file);
int content= 0;
while((content=fileReader.read())!=-1){
System.out.print((char)content);
}
fileReader.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
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.2 writer
输出字符流 :
——–| Writer 抽象类 输出字符流的基类。
————| FileWriter 向文件写出数据输出字符流.
FileWriter 使用步骤: FileReader
1. 找到目标文件
2. 建立数据的输出通道
3. 写出数据
4. 关闭资源
FileWriter 要注意的事项:
1. new FileWriter(file)的时候 , 如果目标文件不存在,那么会创建目标文件对象, 如果目标文件已经存在了,那么则不再重新创建。
2. 使用new FileWriter(file) 这个构造方法的时候,默认是会先清空文本的数据,然后再写入新的数据。如果需要追加数据则需要使用 new FileWriter(file,true)这个构造方法。
3. 使用FileWriter的write方法的时候,数据是写入了FileWriter内部维护的字符数组中,如果需要把数据真正的写到硬盘上去,需要调用flush方法或者 是close方法或者是内部维护的字符数组已经满了,这时候也会写到硬盘上。
public static void writerTest() throws IOException{
File file = new File("F:\\a.txt");
FileWriter fileWriter = new FileWriter(file);
String data = "现在雨停了";
fileWriter.write(data);
fileWriter.flush();
}
2.3 输入输出字符流的拷贝数据
总结 :
需求:使用输入输出字符流拷贝Java文件.
何时使用字符流:何时使用字节流:
- 如果操作的是文本数据时,这时候就应该使用字符流。
- 如果操作的是非文本数据时,这时候则应该使用字节流。 图片 、 视频 、 音频、 word
public class CopyJava {
public static void main(String[] args) throws IOException {
File inFile = new File("F:\\Demo1.java");
File outFile = new File("E:\\Demo2.java");
FileReader fileReader = new FileReader(inFile);
FileWriter fileWriter = new FileWriter(outFile);
char[] buf = new char[1024];
int length = 0 ;
while((length= fileReader.read(buf))!=-1){
fileWriter.write(buf,0,length);
}
fileWriter.close();
fileReader.close();
}
}