IO流

IO流

使用IO流技术读取文件的内容数据。

IO流解决问题: 解决设备与设备之间的数据传输问题。 内存—>硬盘 硬盘—>内存

一、IO流分类

1.按照数据的流向划分:
  • 输入流
  • 输出流

判断是输入流还是输出流:

以当前程序作为参照物,观察数据是流入还是流出,如果是流入则是输入流,如果是流出则是输出流。

判断输入输出流.png

2.按照处理的单位划分:
  • 字节流:字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。
  • 字符流:字符流读取的数据是以字符为单位的 。 字符流也是读取文件中的二进制数据,但是会把这些二进制数据转换成我们能识别的字符。

字符流 = 字节流 + 解码

二、字节流

字节流:

输入字节流
	-----------| InputStream 输入字节流的基类   抽象类
	----------------| FileInputStream 读取文件数据的输入字节流。
	----------------| BufferedInputStream 缓冲输入字节流    缓冲输入字节流出现的目的: 为了提高读取文件数据的效率。 该类内部维护了一个8kb字节数组
 
输出字节流:
	---------| OutputStream 输出字节流的基类。  抽象类
	--------------| FileOutStream  向文件输出数据的输出字节流。
	--------------| BufferedOutputStream 缓冲输出字节流。  该类出现的目的是为了提高写数据的效率 。 该类内部也是维护了一个8kb的数组
	当调用其write方法的时候数据默认是向它内部的数组中存储的,只有调用flush方法或者是close方法或者是8kb的字节数组存储满数据的时候才会真正的向硬盘输出。
1.输入字节流
--------| InputStream 所有输入字节流的基类  抽象类
------------| FileInputStream  读取文件数据的输入字节流 

使用FileInputStream读取文件数据的步骤:

  1. 找到目标文件
  2. 建立数据的输入通道。
  3. 读取文件中的数据。
  4. 关闭资源.
public class Demo1 {
	
	public static void main(String[] args) throws IOException {
		readTest4();
	}
	
	//方式4:使用缓冲数组配合循环一起读取。28
	public static void readTest4() throws IOException{
		long startTime = System.currentTimeMillis();
		//找到目标文件
		File file = new File("F:\\study\\1.jpg");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//建立缓冲数组配合循环读取文件的数据。
		int length = 0; //保存每次读取到的字节个数。
		byte[] buf = new byte[1024]; //存储读取到的数据    缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。  理论上缓冲数组越大,效率越高
		
		while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
			System.out.print(new String(buf,0,length));
		}
		
		//关闭资源
		fileInputStream.close();
		

		long endTime = System.currentTimeMillis();
		System.out.println("读取的时间是:"+ (endTime-startTime)); //446
	}
	
	
	//方式3:使用缓冲 数组 读取。    缺点: 无法读取完整一个文件的数据。     12G
	public static void readTest3() throws IOException{
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//建立缓冲字节数组,读取文件的数据。
		byte[] buf = new byte[1024];  
		// 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,
		//而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
		int length = fileInputStream.read(buf); 
		System.out.println("length:"+ length);
		
		//使用字节数组构建字符串
		String content = new String(buf,0,length);
		System.out.println("内容:"+ content);
		//关闭资源
		fileInputStream.close();
	}

	//方式2 : 使用循环读取文件的数据
	public static void readTest2() throws IOException{
		long startTime = System.currentTimeMillis();
		//找到目标文件
		File file = new File("F:\\study\\1.jpg");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//读取文件的数据
		int content = 0; //声明该变量用于存储读取到的数据
		while((content = fileInputStream.read())!=-1){
			System.out.print((char)content);
		}
		//关闭资源
		fileInputStream.close();
		
		long endTime = System.currentTimeMillis();
		System.out.println("读取的时间是:"+ (endTime-startTime)); //446
	}
	
	
	
	//读取的方式一缺陷: 无法读取完整一个文件 的数据.
	public static void readTest1() throws IOException{
		//1. 找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输入通道。
		FileInputStream fileInputStream = new FileInputStream(file);
		//读取文件中的数据
		int content = fileInputStream.read(); // read() 读取一个字节的数据,把读取的数据返回。
		System.out.println("读到的内容是:"+ (char)content);
		//关闭资源    实际上就是释放资源。 
		 fileInputStream.close();
	}
	
	
}
2.输出字节流

输出字节流:

 --------| OutputStream 是所有输出字节流 的父类。  抽象类
 -----------| FileOutStream 向文件输出数据的输出字节流。

FileOutputStream写出文件的步骤:

  1. 找到目标文件
  2. 建立数据的输出通道。
  3. 把数据转换成字节数组写出。
  4. 关闭资源
public class Demo1 {
	
	public static void main(String[] args) throws IOException {
		writeTest3();
	}
	
	
	//使用字节数组把数据写出。
	public static void writeTest3() throws IOException{
		//找到目标文件
		File file = new File("F:\\b.txt");
		//建立数据输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把数据写出。
		String data = "abc";   
		byte[] buf = data.getBytes();
		fileOutputStream.write(buf, 0, 3); // 0 从字节数组的指定索引值开始写, 2:写出两个字节。

		//关闭资源
		fileOutputStream.close();
	}
		
	
	
	//使用字节数组把数据写出。
	public static void writeTest2() throws IOException{
		//找到目标文件
		File file = new File("F:\\b.txt");
		//建立数据输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(file,true);
		//把数据写出。
		String data = "\r\nhello world";
		fileOutputStream.write(data.getBytes());
		//关闭资源
		fileOutputStream.close();
	}
	
	
	
	//每次只能写一个字节的数据出去。
	public static void writeTest1() throws IOException{
		//找到目标文件
		File file = new File("F:\\b.txt");
		//建立数据的输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把数据写出
		fileOutputStream.write('h');
		fileOutputStream.write('e');
		fileOutputStream.write('l');
		fileOutputStream.write('l');
		fileOutputStream.write('o');
		//关闭资源
		fileOutputStream.close();
		
		
	}
	
	
}

FileOutputStream要注意的细节:

  1. 使用FileOutputStream的时候,如果目标文件不存在,那么会自动创建目标文件对象。
  2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
    3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
    4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。
public class Demo2 {
	
	public static void main(String[] args) throws IOException {
		readTest();
	}
	
	public static void readTest() throws IOException{
		//找到目标文件
		File file = new File("F:\\hcx.txt");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//建立缓冲输入读取文件数据
		byte[] buf = new byte[4];
		//读取文件数据
		int length = fileInputStream.read(buf);
		System.out.println("字节数组的内容:"+ Arrays.toString(buf));
		//关闭资源
		fileInputStream.close();
		
	}

	public static void writeTest() throws FileNotFoundException, IOException {
		//找到目标文件
		File file = new File("F:\\hcx.txt");
		//建立数据的输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把数据写出
		fileOutputStream.write(511);
		//关闭资源
		fileOutputStream.close();
	}

}

拷贝一张图片:

public class CopyImage {
	
	
	public static void main(String[] args) throws IOException {
		//找到目标文件
		File inFile = new File("F:\\1.jpg");
		File destFile = new File("E:\\1.jpg");
		//建立数据的输入输出通道
		FileInputStream fileInputStream = new  FileInputStream(inFile);
		FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....
		
		//每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 
		//每写出一次,指针都会出现相应移动。
		//建立缓冲数据,边读边写
		byte[] buf = new byte[1024]; 
		int length = 0 ; 
		while((length = fileInputStream.read(buf))!=-1){ 
			fileOutputStream.write(buf,0,length); 
		}
		//关闭资源 原则: 先开后关,后开先关。
		fileOutputStream.close();
		fileInputStream.close();
	}

}

三、IO异常的处理

在发生异常时,要阻止后面的代码执行,并且要通知调用者这里出错了:

通过在catch块中抛出运行时异常,并把异常传递过去,抛出运行时异常,方法上可以声明也可以不声明,调用者可以处理也可以不处理,把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是为了让调用者使用变得更加灵活。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.management.RuntimeErrorException;

/*
 IO异常 的处理

 */
public class Demo1 {

	public static void main(String[] args) {
	//	readTest();
		
		copyImage();
	}

	// 拷贝图片
	public static void copyImage() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;

		try {
			// 找到目标文件
			File inFile = new File("F:\\1.jpg");
			File outFile = new File("E:\\1.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();
					System.out.println("关闭输出流对象成功...");
				}
			} catch (IOException e) {
				System.out.println("关闭输出流资源失败...");
				throw new RuntimeException(e);
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
						System.out.println("关闭输入流对象成功...");
					} catch (IOException e) {
						System.out.println("关闭输入流对象失败...");
						throw new RuntimeException(e);
					}
				}

			}
		}
	}

	public static void readTest() {
		FileInputStream fileInputStream = null;
		try {
			// 找到目标文件
			File file = new File("F:\\a.txt");
			// 建立数据输入通道
			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));
			}
		} catch (IOException e) {
			/*
			 * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
			 * RuntimeException(e);
			 * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
			 * 为了让调用者使用变得更加灵活。
			 */
			System.out.println("读取文件资源出错....");
			throw new RuntimeException(e);
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
					System.out.println("关闭资源成功...");
				}
			} catch (IOException e) {
				System.out.println("关闭资源失败...");
				throw new RuntimeException(e);
			}
		}
	}

}

四、缓冲字节流

缓冲输入字节流对象,可以更高效率读取文件。

1.缓冲输入字节流

输入字节流体系:

----| InputStream  输入字节流的基类。 抽象
----------| FileInputStream 读取文件数据的输入字节流
----------| BufferedInputStream 缓冲输入字节流  缓冲输入字节流的出现主要是为了提高读取文件数据的效率。(其实该类内部只不过是维护了一个8kb的字节数组而已。)

注意: 凡是缓冲流都不具备读写文件的能力。

使用BufferedInputStream的步骤:

  1. 找到目标文件
  2. 建立数据的输入通道
  3. 建立缓冲输入字节流流
  4. 关闭资源

注意:有缓冲流时,只需要关闭缓冲流,因为调用BufferedInputStream的close方法实际上关闭的是FileinputStream.

BufferedInputStream出现的目的是了提高读取文件的效率,但是BufferedInputStream的read方法每次读取一个字节的数据,而FileInputStreram每次也是读取一个字节的数据,那么BufferedInputStream效率高的原因是什么?
缓冲类内部维护了一个8kb的字节数组,而BufferedInputStream的read()方法如下:

    public synchronized int read() throws IOException {
        if (pos >= count) {
            fill();
            if (pos >= count)
                return -1;
        }
        return getBufIfOpen()[pos++] & 0xff;
    }

count:目前缓冲数组中存储了几个字节的数据
pos:当前已经读取到了第几个字节(指针)

第一次的时候,count为0,pos也为0,调用fill()方法,使用内部维护的缓冲数组去读取文件数据,一次最多读取8kb的字节数据到缓冲数组中。最后从字节数组中取出数据并返回。当第二次往后调用时,每次都从字节数组中取出数据并返回,直到取完之后,再次填充。而fileInputStream每次都是从硬盘中读取数据。

使用bufferedInputStream和自己创建一个8kb的字节数组,两者的效率是一样的。严格意义上说,自己创建数组效率更高一些,因为没有了判断的步骤。

public class Demo1 {
	
	public static void main(String[] args) throws IOException {
		readTest2();
	}
	
	public static void readTest2() throws IOException{
		//找到目标文件
		File file = new File("F:\\a.txt");
		
		FileInputStream fileInputStream= new FileInputStream(file);
		//创建BufferedInputStream的时候需要传入fileInputStream:BufferedInputStream本身不具备读文件的能力,
		//需要借助fileInputStream来读取文件的数据。
		BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
		bufferedInputStream.read();

		
		//读取文件数据
		int content = 0 ;
		//BufferedInputStream出现 的目的是了提高读取文件的效率,但是BufferedInputStream的read方法每次读取一个字节的数据
		//而FileInputStreram每次也是读取一个字节的数据,那么BufferedInputStream为什么效率更高
		while((content = fileInputStream.read())!=-1){
			System.out.print((char)content);
		}
		
		//关闭资源
		bufferedInputStream.close();//调用BufferedInputStream的close方法实际上关闭的是FileinputStream.
	}

	//读取文件的时候都是使用缓冲数组读取。效率会更加高(推荐)
	public static void readTest() throws IOException{
		File file = new File("F:\\a.txt");
		//建立数组通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//建立缓冲数组读取数据
		byte[] buf = new byte[1024*8];
		int length = 0; 
		while((length = fileInputStream.read(buf))!=-1){
			System.out.print(new String(buf,0,length));
		}
		//关闭资源
		fileInputStream.close();
	}
	
}

2.缓冲输出字节流

输出字节流

--------| OutputStream  所有输出字节流的基类  抽象类
------------| FileOutputStream 向文件 输出数据 的输出字节流
------------| Bufferedoutputstream  缓冲输出字节流  BufferedOutputStream出现的目的是为了提高写数据的效率。 (内部也是维护了一个8kb的字节数组)

BufferedOutputStream要注意的细节:

  1. 使用BufferedOutputStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。
  2. 使用BufferedOutputStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写到硬盘上面,需要调用flush方法或者是close方法,当内部维护的字节数组已经填满数据的时候,会自动把数据刷出去。
public class Demo2 {

	public static void main(String[] args) throws IOException {
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输出通道
		FileOutputStream  fileOutputStream = new FileOutputStream(file);
		//建立缓冲输出字节流对象
		BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
		//把数据写出
		bufferedOutputStream.write("hello world".getBytes()); 
		//把缓冲数组中内部的数据写到硬盘上面。
		//bufferedOutputStream.flush();
		bufferedOutputStream.close();
	}
	
}

注意:使用read方法如果传入了缓冲数组,内容是存储到了缓冲数组中,返回值返回的是存储到缓冲数组中的字节个数。如果read方法没有传入缓冲数组,那么返回值是读取到的内容。

使用缓冲输入输出字节流拷贝图片:

public class CopyImage {

	public static void main(String[] args) throws IOException {
		//找到目标文件
		File  inFile = new File("F:\\美女\\1.jpg");
		File outFile = new File("E:\\1.jpg");
		//建立数据输入输出通道
		FileInputStream fileInputStream = new FileInputStream(inFile);
		FileOutputStream fileOutputStream = new FileOutputStream(outFile);
		//建立缓冲输入输出流
		BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
		//边读边写
		int content = 0; 
		// int length = bufferedInputStream.read(buf);   如果传入了缓冲数组,内容是存储到缓冲数组中,返回值是存储到缓冲数组中的字节个数。
		while((content = bufferedInputStream.read())!=-1){ // 如果使用read方法没有传入缓冲数组,那么返回值是读取到的内容。
			bufferedOutputStream.write(content);
//			bufferedOutputStream.flush();
		}
		//关闭资源
		bufferedInputStream.close();
		bufferedOutputStream.close();

	}
	
}

五、字符流

使用字节流来读写中文都是比较麻烦的:

public class Demo1 {
	
	public static void main(String[] args) throws IOException {
//		writeTest();
		readrTest();
	}
	
	
	//使用字节流读取中文
	public static void readrTest() throws IOException{
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//读取内容
		//int content = 0;
		
		/*while((content = fileInputStream.read())!=-1){ //出现乱码的原因: 一个中文在gbk码表中默认是占两个字节,
													   // 目前你只读取了一个字节而已,所以不是一个完整的中文。
			System.out.print((char)content);
		}*/
		
		byte[] buf = new byte[2];
		for(int i = 0 ; i < 3 ; i++){
			fileInputStream.read(buf);
			System.out.print(new String(buf));
		}
		//关闭资源
		fileInputStream.close();
		
	}

	//使用字节流写中文。   
	//字节流之所以能够写中文是因为借助了字符串的getBytes方法对字符串进行了编码(字符---->数字)。 
	public static void writeTest() throws IOException{
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输出通道
		FileOutputStream fileOutputStream  = new FileOutputStream(file);
		//准备数据,把数据写出。
		String data = "大家好";
		byte[] buf = data.getBytes();	//把字符串转换成字节数组
		System.out.println("输出的内容:"+ Arrays.toString(buf));//[-76,-13,-68,-46,-70,-61]
		fileOutputStream.write(buf);
		///关闭资源
		fileOutputStream.close();
	}

}

字节流:字节流读取的是文件中的二进制数据,读到的数据并不会帮你转换成你看得懂的字符。

1.输入字符流

字符流: 字符流会把读取到的二进制的数据进行对应的编码与解码工作。
字符流 = 字节流 + 编码(解码)

输入字符流:

-------| Reader 所有输入字符流的基类。 抽象类
----------| FileReader 读取文件字符串的输入字符流。
----------| BufferedReader   缓冲输入字符流 。 缓冲输入字符流出现的目的是为了提高读取文件 的效率和拓展了FileReader的功能。(其实该类内部也是维护了一个字符数组)

注意:缓冲流都不具备读写文件的能力。

public class Demo2 {

	public static void main(String[] args) throws IOException {
		readTest2();
	}
	
	//使用缓冲字符数组读取文件。
	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));
		}
	}

	
	public static void readTest1() throws IOException{
		//找到目标文件
		File file = new File("F:\\Demo1.java");
		//建立数据的输入通道
		FileReader fileReader = new FileReader(file);
		int content = 0 ;
		while((content = fileReader.read())!=-1){ //每次只会读取一个字符,效率低。
			System.out.print((char)content);
		}
		//关闭资源
		fileReader.close();
	}
	
}

2.输出字符流

输出字符流:

------| Writer 输出字符流的基类。抽象类
-----------| FileWriter 向文件输出数据的输出字符流

FileWriter要注意的事项:

  1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它内部维护的字符数组中,如果需要把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组。
  2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。
    3.使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先清空文件中的数据,然后再写入数据 , 如果需要在原来的基础上追加数据,需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true。
public class Demo1 {

	public static void main(String[] args) throws IOException {
		writeTest1();
	}
	
	public static void  writeTest1() throws IOException{
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据输出通道
		FileWriter fileWriter = new FileWriter(file,true);
		//准备数据,把数据写出
		String data = "你好 世界";
		fileWriter.write(data);  //字符流具备解码的功能。
		//刷新字符流
//		fileWriter.flush();
		//关闭资源
		fileWriter.close();

	}
}

使用FileReader进行文件的拷贝没问题,但是拷贝图片时数据会丢失,图片损坏

public class CopyImage {
	
	public static void main(String[] args) throws IOException {
		File inFile = new File("F:\\1.jpg");
		File outFile = new File("E:\\2.jpg");
		
		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();
	}

}

原因:计算机中的所有信息都是以二进制形式进行的存储(1010)图片中的也都是二进制。在读取文件的时候字符流自动对这些二进制按照码表进行了编码处理,但是图片本来就是二进制文件,不需要进行编码。有一些巧合在码表中有对应,就可以处理,并不是所有的二进制都可以找到对应的。信息就会丢失。所以字符流只能拷贝以字符为单位的文本文件(以ASCII码为例是127个,并不是所有的二进制都可以找到对应的ASCII,有些对不上的,就会丢失信息。)

使用字符流的应用场景: 如果是读写字符数据的时候则使用字符流。
使用字节流的应用场景: 如果读写的数据都不需要转换成字符的时候,则使用字节流。

六、缓冲字符流

1.缓冲输入字符流

输入字符流:

-------| Reader 所有输入字符流的基类。 抽象类
----------| FileReader 读取文件字符串的输入字符流。
----------| BufferedReader   缓冲输入字符流 。缓冲输入字符流出现的目的是为了提高读取文件的效率和拓展了FileReader的功能。(该类内部也是维护了一个字符数组)

注意:缓冲流都不具备读写文件的能力。

public class Demo1 {

	public static void main(String[] args) throws IOException {
//		 readTest1();
		File file  = new File("F:\\Demo1.java");
		//建立数据的输入通道。
		FileReader fileReader = new FileReader(file);
		String line =  null;
		
		while((line = myReadLine(fileReader))!=null){
			System.out.println(line);
		}
	}

	
	//实现readLine方法。
	public static String myReadLine(FileReader fileReader) throws IOException{
		//创建一个字符串缓冲类对象
		StringBuilder sb = new StringBuilder();  //StringBuilder主要是用于存储读取到的数据
		int content = 0 ;
		while((content = fileReader.read())!=-1){
			if(content=='\r'){
				continue;
			}else if(content=='\n'){
				break;
			}else{
				//普通字符
				sb.append((char)content);
			}
		}
		//代表已经读取完毕了。
		if(content ==-1){
			return null;
		}
		
		return sb.toString();  
	}
	
	public static void readTest1() throws IOException{
		//找到目标文件
		File file  = new File("F:\\a.txt");
		//建立数据的输入通道。
		FileReader fileReader = new FileReader(file);
		//建立缓冲输入字符流
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		//读取数据
		/*int content = bufferedReader.read();  //读到了一个字符。 读取到的字符肯定也是从Bufferedreader内部的字符数组中获取的到。所以效率高。
		System.out.println((char)content);*/
		//使用BUfferedReader拓展的功能,readLine()  一次读取一行文本,如果读到了文件的末尾返回null表示。
		String line =  null;
		while((line = bufferedReader.readLine())!=null){ // 虽然readLine每次读取一行数据,但是返回的line是不包含\r\n的
			System.out.println(Arrays.toString("a".getBytes()));
		}
		//关闭资源
		bufferedReader.close();
		
	}
	
}
2.缓冲输出字符流

输出字符流

----------| Writer  所有输出字符流的基类,  抽象类。
--------------- | FileWriter 向文件输出字符数据的输出字符流。 
----------------| BufferedWriter 缓冲输出字符流        

缓冲输出字符流作用: 提高FileWriter的写数据效率与拓展FileWriter的功能。
BufferedWriter内部提供了一个8192长度的字符数组作为缓冲区,拓展了FileWriter的功能。

public class Demo2 {

	public static void main(String[] args) throws IOException {
		//找到目标文件
		File file = new File("F:\\a.txt");
		//建立数据的输出通道
		FileWriter fileWriter = new FileWriter(file,true);
		//建立缓冲输出流对象
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 
		//写出数据
//		bufferedWriter.newLine(); //newLine() 换行。 实际上就是向文件输出\r\n.
		bufferedWriter.write("\r\n");
		bufferedWriter.write("我要去到莲花山顶看灯光秀!!");
		//关闭资源
		bufferedWriter.flush();
//		bufferedWriter.close();
		
	}
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值