java IO ——文件流详解

本篇博文学习了一下内容

FileInputStream 字节输入流

构造方法

读取文件

FileOutputStream 字节输出流

构造方法

写入文件

FileRearder 字符输入流

构造方法

读取文件

FileWriter 字符输出流

构造方法

写出文件

字节文件和字符文件

总结


FileInputStream 字节输入流

构造方法

构造方法
方法 功能
FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
FileInputStream(File file)  通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

 这两种构造方法都会报异常(文件不存在的情况下),必须显式的处理。

//两种构造方法
FileInputStream fis  = new FileInputStream(new File(path1));
FileInputStream fis = new FileInputStream("D:\\file");

读取文件

read 方法
方法 功能
read()

          从此输入流中读取一个数据字节。

返回:下一个数据字节;如果已到达文件末尾,则返回 -1

read(byte b)

          从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。

返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1

read(byte[] b,int off,int len)

          从此输入流中将最多 len 个字节的数据读入一个字节数组中。

返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1

/**
 *  read(byte b[])  本质调用的是readBytes(b, 0, b.length) 方法
 */
public int read(byte b[]) throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int bytesRead = 0;
        try {
            bytesRead = readBytes(b, 0, b.length);
        } finally {
            IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
        }
        return bytesRead;
    }
/**
 *  read(byte b[], int off, int len) 调用的是readBytes(b, off, len) 方法
 */

 public int read(byte b[], int off, int len) throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int bytesRead = 0;
        try {
            bytesRead = readBytes(b, off, len);
        } finally {
            IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
        }
        return bytesRead;
    }

从上面的源码来看 read(byte b) 和 read(byte[] b,int off,int len) 本质上是一样的,相当于 read(byte b) 调用了read( b,0,b.length)方法。

从磁读取文件到程序里
@Test
	public void test2(){
		FileInputStream fis = null;
		try {
			//构造方法:建立一个流的对象
			fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\file\\file3.txt");
			//每次读取 5 个字节的内容
			byte[] b = new byte[5];
			//每次读取的长度
			int len;
			//确保读入了内容
			while((len = fis.read(b)) != -1){
				//将读入的内容输出到控制台上
					for(int i = 0;i < len;i++){
						System.out.print((char)b[i]);
					}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fis != null){
				try {
					//关闭流对象
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 结果

dishgsdihkfw

FileOutputStream 字节输出流

构造方法

构造方法
方法 功能
FileOutputStream(File file)  创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(String name)  创建一个向具有指定名称的文件中写入数据的输出文件流。 
FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 
FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流
FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\file\\file3.txt",true);

 如果这个文件不存在,则会新建一个文件,不会报异常。

默认情况下都是输出的字节覆盖掉原来的字节,最后两个构造方法当 append 为 true 时,将输出的字节文件追加到原来文件的末尾,而不是覆盖它。

写入文件

write 方法
方法 功能
write(int b)  将指定字节写入此文件输出流。 
write(byte[] b)  将 b.length 个字节从指定字节数组写入此文件输出流中。
write(byte[] b, int off, int len)  将指定字节数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 

我们类比 FileInputStream 来看,write(byte[] b) 也相当于实现了 write(b, 0, b.length) 方法一样。 

@Test
	public void test3(){
		FileOutputStream fos = null;
		try {
			//创建一个流对象,以追加的形式输出字节文件
			fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\file\\file3.txt",true);
			//写入的内容
			String str = "  i love china";
			//写入
			fos.write(str.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

FileRearder 字符输入流

构造方法

构造方法
方法 功能
FileReader(File file)   在给定从中读取数据的 File 的情况下创建一个新 FileReader。
FileReader(String fileName)  在给定从中读取数据的文件名的情况下创建一个新 FileReader。

 和 FileInputStream 一样,这个构造方法会报异常(文件不存在),必须显式的处理。

FileReader fr = new FileReader("C:\\Users\\Administrator\\Desktop\\file\\hah.txt");
FileReader fr = new FileReader(new File("C:\\Users\\Administrator\\Desktop\\file\\hah.txt");

读取文件

我们可以类比的学习 FileReader,这里的 read 方法和 FileInputStream 方法一样

FileWriter 字符输出流

构造方法

构造方法
方法 功能
FileWriter(File file)  创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileWriter(String name)  创建一个向具有指定名称的文件中写入数据的输出文件流。 
FileWriter(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 
FileWriter(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流

写出文件

这里的 writer 方法和 FileOutputStream 方法一样,FileOutputStream 的writer 方法写的是字节数组,这里的writer 方法写的是字符数组。

@Test
	public void test2(){
		FileWriter writer = null;	
			//建立流对象
			try {
				writer  = new FileWriter("C:\\Users\\Administrator\\Desktop\\file\\file3.txt");  
				String str = " i love china";
				char[] ch = str.toCharArray();
				writer.write(ch);	
			} catch (IOException e1) {
				e1.printStackTrace();
			}finally{
				if(writer != null){
					try {
						writer.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
	}
}

字节文件和字符文件

一般来说,纯文本文件是字符文件,比如.txt文件(word 是字节文件)

字节文件:图片、视频等都是字节文件。

总结

通过总结文件流,我们可以看出FileInputStream 和 FileReader 相对应,FileOutputStream 和 FileWriter 相对应。

它们的构造方法以及处理文件的方法都大致相同,我们可以类比的学习文件流,达到事半功倍的效果。

猜你喜欢

转载自blog.csdn.net/Alyson_jm/article/details/81431537