Java IO 简介

224 阅读3分钟

这是我参与更文挑战的第18天,活动详情查看:更文挑战

什么是IO?

IO的名称又来是Input与Output的缩写,也就是输入流和输出流。输入流用于从源读取数据,输出流用于向目标写数据。

Java IO.png

字符流

  字符流有两个抽象类:WriterReader类。 其对应子类FileWriterFileReader可实现文件的读写操作。 BufferedWriterBufferedReader能够提供缓冲区功能,用以提高效率。

public static void main(String[] args)  {
    try {
        test();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void test() throws IOException {
    String str;
    // 使用 System.in 创建 BufferedReader 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("输入字符, 输入 'quit' 退出。");
    // 读取字符
    do {
        str=br.readLine();
        System.out.println("您输入的字符是:"+str);
    } while(!str.equals("quit"));
    br.close();
}

然后我们输入 helloquit结果如下:

输入字符, 输入 'quit' 退出。
hello
您输入的字符是:hello
您输入的字符是:
quit
您输入的字符是:quit

  通过上述示例我们可以简单的了解下了字符流。 一般来说,我们主要用字符流的情况是读写文件,大部分也是文本文件,比如.txt后缀的。这里我们也顺便介绍下如何使用。

/**
 *
 * 写入和读取文件
 * @throws IOException
 */
private static void test2() throws IOException {
    //创建要操作的文件路径和名称  
    String path ="E:/test/hello.txt";
    String str="hello world";
    FileWriter fw = new FileWriter(path);  
    fw.write(str);  
    fw.close();  

    FileReader fr = new FileReader(path);  
    StringBuffer sb=new StringBuffer();
    while(fr.ready()){
        sb.append((char)fr.read());
    }
    System.out.println("输出:"+sb.toString());
    fr.close();
}

  上述代码示例中,我们使用FileWriterFileReader 这两个类对文件进行读写,虽然可以实现字符的写入和读取,但是效率并不高,因为是对磁盘的直接读写。一般对于文件的读写,我们会使用缓冲。使用缓冲的好处就像 倒垃圾一样,将垃圾进行整理堆积,然后到了一定的规模在丢弃,而不是有一点垃圾就倒一次。

那么在上述的代码中加上BufferedWriterBufferedReader类来进行缓冲。

代码示例:

/**
 * 写入和读取文件
 * @throws IOException
 */
private static void test3() throws IOException {
    //创建要操作的文件路径和名称  
    String path ="E:/test/hello.txt";
    String str="你好!";
    FileWriter fw = new FileWriter(path);  
    BufferedWriter bw=new BufferedWriter(fw);
    bw.write(str);  
    bw.close();
    fw.close();  

    FileReader fr = new FileReader(path);  
    BufferedReader br=new BufferedReader(fr);
    StringBuffer sb=new StringBuffer();
    while(br.ready()){
        sb.append((char)br.read());
    }
    System.out.println("输出:"+sb.toString());
    br.close();
    fr.close();
}

字节流

  字节流也有两个抽象类:InputStreamOutputStream类。 其对应子类有FileInputStreamFileOutputStream实现文件读写操作。 BufferedInputStreamBufferedOutputStream提供缓冲区功能

  字节流也能对文本进行读取,但是它的主要使用的场景是读取无法直接获取文本信息的二进制文件,比如音乐文件、视频文件、图片文件等等。 这里我们依旧对文件进行读取和写入,不过我们把之前写入到hello.txt文件的内容加上 '你好' 写入到新的文件中。由于这里使用的了中文,所以需要设置相应的编码。

代码示例:

 /**
	 * 创建一个文件并读取记录 
	 * @throws IOException
	 */
	private static void test4() throws IOException {
		String path="E:/test/hello.txt";
		String path2="E:/test/你好.txt";
		String str="你好!";
		//从文件读取数据
		InputStream input = new FileInputStream(path);
		InputStreamReader reader = new InputStreamReader(input, "UTF-8");
	    StringBuffer sb=new StringBuffer();
		while(reader.ready()){
			sb.append((char)reader.read());
		}
		
		input.close();
		reader.close();
		
		//创建一个文件并向文件中写数据
		OutputStream output = new FileOutputStream(path2);
		OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
		writer.write(sb+str);
		
		writer.close();
		output.close();
		
		//从文件读取数据
		InputStream input2 = new FileInputStream(path2);
		InputStreamReader reader2 = new InputStreamReader(input2, "UTF-8");
	    StringBuffer sb2=new StringBuffer();
		while(reader2.ready()){
			sb2.append((char)reader2.read());
		}
		System.out.println("输出:"+sb2);
		input2.close();
		reader2.close();
	}	

结果:

	输出:hello world你好!

可以看到结果符合我们的预期。