常用IO类总结_1(FileInputStream、FileOutputStream)

说明:常用IO类介绍中,包括FileInputStream、FileOutputStream;FileWriter、FileReader;BufferedInputStream、BufferedOutputStream。

        本文介绍的是FileInputStream、FileOutputStream

============================================

1.首先是IO体系


2.IO流的分类:
 * 按照数据流向的不同:输出流、输入流
 * 按照处理数据的单位的不同:字节流、字符流(处理的文本文件)

 * 按照角色的不同:节点流、处理流(流直接作用在文件上的是节点流(4个),除此之外都是处理流)

====================================================

3.File类简介:

1)在java.io包下

2)File类:java程序中的此类的一个对象,就对应着硬盘中的一个文件或网络中的一个资源。   

    File file1 = new File("d:\\io\\helloworld.txt");

    File file2 = new File("d:\\io\\io1");

3)File既可以表示一个文件(.doc  .xls  .mp3  .avi  .jpg等),也可以表示一个文件目录。

4)File类的对象是与平台无关的。

5)File类针对于文件或文件目录,只能进行新建、删除、重命名、上层目录等等的操作。如果涉及到访问文件的内容,File是无能为力的,只能使用IO流下提供的相应的输入输出流来实现。

6)常把File类的对象作为形参传递给相应的输入输出流来实现。

===================================================

===================================================

4.以下将通过代码方式直接使用FileInputStream、FileOutputStream操作文件,其中注释都非常详细。

package com.lin.fileinputstream;

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

import org.junit.Test;

/**
 * 1.流的分类:
 *		按照数据流向的不同:输出流、输入流
 *		按照处理数据的单位的不同:字节流、字符流(处理的文本文件)
 *		按照角色的不同:节点流、处理流(流直接作用在文件上的是节点流(4个),除此之外都是处理流)
 * 
 *2.IO的体系:
 *		抽象基类			节点流(文件流)			缓冲流(处理流的一种,可以提升文件操作的效率)
 *		InputStream		FileInputStream		        BufferedInputStream
 *		OutputStream	        FileOutputStream	        BufferedOutputStream(写完数据后要调用flush())
 *		Reader			FileReader			BufferedReader(readLine())
 *		Writer			FileWriter			BufferedWriter(写完数据后要调用flush())
 **/

public class TestFileInputOutputStream {

	//从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream
	//要读取的文件一定要存在,否则会抛出FileNotFoundException异常
	//如果是写入文件的话则文件可以不存在
	@Test
	public void testFileInputStream() throws IOException {
		
		//1.创建一个File类的对象(需要指定文件的所在位置,我是放在桌面,有以下两种写法)
//		File file = new File("C:\\Users\\Administrator\\Desktop\\hello.txt");
		File file = new File("C:/Users/Administrator/Desktop/hello.txt");
		//2.创建一个FileInputStream类的对象
		FileInputStream fis = new FileInputStream(file);
		
		//3.调用FileInputStream的方法,实现file文件的读取(两种写法)
//		//方式一
//		int b = fis.read();//read()方法读取文件的一个字节。当执行到文件结尾时,返回-1
//		while(b!=-1){
//			//将读取的每一个字节都打印在控制台上
//			System.out.print((char)b);
//			//读取文件的下一个字节
//			b = fis.read();
//		}
		//方式二
		int b;
		while( (b=fis.read())!=-1){
			System.out.print((char)b);
		}
		
		//关闭相应的流
		fis.close();
	}
	
	//对上一部分代码进行完善
	//使用try/catch的方式处理如下的异常更合适:保证流的关闭操作一定可以执行
	@Test
	public void testFileInputStream2(){
		
		long startTime=System.currentTimeMillis();//获取开始时间(测试程序运行效率)
		
		//定义一个FileInputStream类的对象
		FileInputStream fis = null;
		
		//1.创建一个File类的对象(需要指定文件的所在位置,我是放在桌面,有以下两种写法)
//		File file = new File("C:\\Users\\Administrator\\Desktop\\hello.txt");
		File file = new File("C:/Users/Administrator/Desktop/hello.txt");
		try {
			//2.创建一个FileInputStream类的对象
			fis = new FileInputStream(file);
			//3.调用FileInputStream的方法,实现file文件的读取(两种写法)
//			//方式一
//			int b = fis.read();//read()方法读取文件的一个字节。当执行到文件结尾时,返回-1
//			while(b!=-1){
//				//将读取的每一个字节都打印在控制台上
//				System.out.print((char)b);
//				//读取文件的下一个字节
//				b = fis.read();
//			}
			//方式二
			int b;
			while( (b=fis.read())!=-1){
				System.out.print((char)b);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{//不管是否发生异常,都能保证将相应的流关闭
			if(fis!=null){
				//关闭相应的流
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		long endTime=System.currentTimeMillis(); //获取结束时间(测试程序运行效率)
		System.out.println();
		//输出读取完文件需要的总时间
		System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
	}
	
	
	/*============以上是每次读取单个字节,下面是一次读取多个字节===========*/
	//使用try/catch的方式处理如下的异常更合适:保证流的关闭操作一定可以执行
	@Test
	public void testFileInputStream3(){
		
		long startTime=System.currentTimeMillis();   //获取开始时间(测试程序运行效率)
		
		//定义一个FileInputStream类的对象
		FileInputStream fis = null;
		
		//1.创建一个File类的对象(需要指定文件的所在位置,我是放在桌面,有以下两种写法)
//		File file = new File("C:\\Users\\Administrator\\Desktop\\hello.txt");
		File file = new File("C:/Users/Administrator/Desktop/hello.txt");
		try {
			//2.创建一个FileInputStream类的对象
			fis = new FileInputStream(file);
			//3.调用FileInputStream的方法,实现file文件的读取(两种写法)
			byte[] b = new byte[5];//每次读取到的数据要写入的数组(与每次读取一个字节相比,这个效率会高出很多!)
			int len;//每次读入到byte中的字节的长度
			while((len = fis.read(b)) != -1){
//				//遍历方式一:
//				for(int i=0; i<len; i++){
//					System.out.print((char)b[i]);
//				}
				//遍历方式二:
				String str = new String(b, 0, len);
				System.out.print(str);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{//不管是否发生异常,都能保证将相应的流关闭
			if(fis!=null){
				//关闭相应的流
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		long endTime=System.currentTimeMillis(); //获取结束时间(测试程序运行效率)
		System.out.println();
		//输出读取完文件需要的总时间
		System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
	}
	
	
	//=====================以上是文件读取的测试,以下是文件写入的测试=====================
	
	//文件的写入
	@Test
	public void testFileOutputStream(){
		//创建一个FileOutputStream对象,将file的对象作为形参传递给FileOutputStream的构造器
		FileOutputStream fos = null;
		
		try{
			//1.创建一个File对象,表明要写入的文件位置(输出的物理文件可以不存在,若存在则将原始文件覆盖)
			File file = new File("C:/Users/Administrator/Desktop/hello2.txt");
			fos = new FileOutputStream(file);
			//写入操作。  .getBytes()表示将字符串转为字节数组
			fos.write("这是我写的FileOutputStream测试!!!".getBytes());
			System.out.println("写入文件成功!");
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//关闭输出流
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//====================以下是两个类一起测试==========================
	
	//从硬盘中读取一个文件(可以是图片或者视频文件),并写入到另一个位置。(相当于文件复制)
	@Test
	public void testFileInputStreamOutputStream(){
		//1.提供读入、写出的文件
		File file1 = new File("C:/Users/Administrator/Desktop/hello2.txt");
		File file2 = new File("C:/Users/Administrator/Desktop/hello3.txt");
//		//图片测试通过
//		File file1 = new File("C:/Users/Administrator/Desktop/壁纸01.jpg");
//		File file2 = new File("C:/Users/Administrator/Desktop/壁纸011.jpg");
	
		//2.提供相应的流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try{
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			
			//3.实现文件的读取并写入(复制)
			byte[] b = new byte[20];//每次读取到的数据要写入的数组
			int len;//每次读入到byte中的字节的长度
			while((len = fis.read(b)) != -1){
				fos.write(b, 0, len);
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


猜你喜欢

转载自blog.csdn.net/satisfy_555/article/details/80579720