FileInputStream、FileReader、FileInputStream、FileWriter使用小结

本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识

InputStream包含如下三个方法:

  • int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为int类型)
  • int read(byte[] b):从输入流中最多读取b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数
  • int read(byte[] b, int off, int len):从输入流中最多读取len个字节的数据,并将其存储在数组b中;放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数

FileInputStream继承InputStream,使用FileInputStream读取文件内容

代码1-1

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("请输入路径");
		}
		FileInputStream fis = null;
		try {
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			fis = new FileInputStream(args[0]);
			while ((hasRead = fis.read(bbuf)) > 0) {
				System.out.print(new String(bbuf, 0, hasRead));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

代码1-1运行结果:

root@lejian:/home/software/.io# cat text 
Hello Spring
Hello Hibernate
Hello MyBatis
root@lejian:/home/software/.io# java FileInputStreamTest text 
Hello Spring
Hello Hibernate
Hello MyBatis

Reader包含如下三个方法:

  • int read():从输入流中读取单个字符,返回从读取的字符数据(字符数据可直接转化为int类型)
  • int read(char cbuf[]):从输入流中最多读取cbuf.length个字符的数据,并将其存储在字符数组的cbuf中,返回实际读取的字符数
  • int read(char cbuf[], int off, int len):从输入流中最多读取len个字符的数据,并将其存储在数组b中;放入数组cbuf中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数

FileReader的父类继承自Reader,使用FileReader读取文件

代码1-2

import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("请输入路径");
		}
		FileReader fr = null;
		try {
			fr = new FileReader(args[0]);
			char[] cbuf = new char[32];
			int hasRead = 0;
			while ((hasRead = fr.read(cbuf)) > 0) {
				System.out.print(new String(cbuf, 0, hasRead));
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

代码1-2运行结果:

root@lejian:/home/software/.io# cat text 
Java编程思想
算法
编译原理
root@lejian:/home/software/.io# java FileReaderTest text 
Java编程思想
算法
编译原理

代码1-3

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

public class FileOutputStreamTest {

	public static void main(String[] args) {
		if (args == null || args.length < 2) {
			throw new RuntimeException("请输入两个路径");
		}
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(args[0]);
			fos = new FileOutputStream(args[1]);
			byte[] bbuf = new byte[32];
			int hasRead = 0;
			while ((hasRead = fis.read(bbuf)) > 0) {
				fos.write(bbuf, 0, hasRead);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

OutputStream和Writer:

  • void write(int b):将指定的字节/字符输出到输出流中,其中c代表字节,也可以代表字符
  • void write(byte b[]/char cbuf[]):将字节数组/字符数组中的数据输出到指定输出流中
  • void write(byte b[]/char cbuf[], int off, int len):将字节数组/字符数组中从off位置开始,长度为len的字节/字符输出到输出流中

Writer里还包含如下两个方法:

  • void write(String str):将str字符串里包含的字符输出到指定输出流中
  • void write(String str, int off, int len):将str字符串里从off位置开始,长度为len的字符输出到指定的输出流中

使用FileInputStream来执行输入,FileOutputStream来执行输出

代码1-3运行结果:

root@lejian:/home/software/.io# cat input 
Java编程思想
算法
编译原理
root@lejian:/home/software/.io# java FileOutputStreamTest input output 
root@lejian:/home/software/.io# cat output 
Java编程思想
算法
编译原理

使用FileWriter来执行输出

代码1-4

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("请输入路径");
		}
		FileWriter fw = null;
		try {
			fw = new FileWriter(args[0]);
			fw.write("数据结构与算法\n");
			fw.write("Python基础教程\n");
			fw.write("C和指针\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fw != null) {
					fw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

代码1-4运行结果:

root@lejian:/home/software/.io# java FileWriterTest book 
root@lejian:/home/software/.io# cat book 
数据结构与算法
Python基础教程
C和指针

Java7之后,很多IO资源类都实现了AutoCloseable接口,只要在try语句中声明的IO流都可以自动关闭,但有一些IO流像FileWriter这样的输出流,如果IO流不显示的关闭,输出流缓冲区的数据无法flush到实际的物理节点,因为执行IO流的close()方法前,会先执行flush()方法

原文地址:https://www.cnblogs.com/baoliyan/p/6220092.html