IO流,简单描述

版权声明:本文为博主原创文章,未经博主允许不得转载——Cc。 https://blog.csdn.net/mufeng633/article/details/82928172

流:
所有输入流类都是抽象类InputStream(字节输入流) 或抽象类Reader(字符输入流)的子类
而所有输出流都是抽象类OutputStream(字节输出流) 或Writer(字符输出流)的子类

字节流:常用于读取视频,音频,文件等
字符流:用于读取文本

File流常用方法:

File file = new File("E:/A测试流/使用说明.txt");

  file.exists()	    	判断文件是否存在
  file.getName()		得到文件名称
  ile.createNewFile()	创建文件
  file.mkdirs()			创建一个文件夹的目录
  file.isFile()			是否为一个文件
  file.isDirectory()	是否为一个目录
  file.getPath()		得到相对于工程的相对路径
  file.getAbsolutePath()	得到真实路径
  file.length()				获取多少字节
  
  注:流关闭顺序,先打开的后关,后打开的先关

使用字节读取文件

	public static void read(File file){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file);
			System.out.println("可读取的字节:"+fis.available());
			File file2 = new File("E:/A测试流/使用说明2.txt");//将参数file获取的流,写入到该文件中
			//如果该文件不存在,则去创建该文件
			if( ! file2.exists()) {
				file2.createNewFile();
			}
			//将读取到的文件(字节)流,放到输出流中
			fos = new FileOutputStream(file2);
			byte[] b = new byte[512];
			int i = 0;	//标识读取结束
			// fis.read(b) 含义:读取到的字节,放到字节数组 b 中,如果读到最后没有数据则返回-1
			while((i = fis.read(b)) != -1) {
			//将指定 byte 数组中从偏移量 0 开始的 i 个字节写入此文件输出流
				fos.write(b,0,i);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				//关闭流
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

将 字节流 转换 字符流进行写入

	public static void write2(File file) {
		FileInputStream fis  = null;
		FileOutputStream fos = null;
		try {
			if(file.exists()) {
				file.createNewFile();
			}
			//获取本地字符编码
			System.out.println(System.getProperty("file.encoding"));

			fis = new FileInputStream(file);
			fos = new FileOutputStream("E:/A测试流/使用说明2.txt");
			InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//设置编码格式
			OutputStreamWriter ops = new OutputStreamWriter(fos);
			char[] cbuf = new char[512];//因为是字符写入,所以要创建字符数组
			int len = 0;
			while((len = isr.read(cbuf)) != -1) {
				ops.write(cbuf, 0, len);
			}
			//关闭流,先打开的 后关闭
			ops.close();
			isr.close();
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

使用 缓冲区流 进行操作文件

public static void  reader(File file) {
		//1.创建输入流
		FileInputStream fis = null;	//字节输入流
		InputStreamReader is = null;	//字符输出流
		BufferedReader br = null;	//带有缓冲功能的,字符缓冲输入流
		//2.创建输出流
		FileWriter fw = null;			//字符输出流
		BufferedWriter bw = null;		//带有缓冲功能的,字符缓冲输出流
		try {
			//1.创建输入流
			fis = new FileInputStream(file);
			//设置读取文件的编码格式,并转换为字符流
			is  = new InputStreamReader(fis,"UTF-8");
			//将字符流转换为带有缓冲区的字符流
			br = new BufferedReader(is);
			
			//2.创建输出流
			fw = new FileWriter("E:/A测试流/pet2.txt");
			bw= new BufferedWriter(fw);	//因为有默认缓冲区大小,没有满,如果不flush或刷新,则不会写入到文件中,所以要刷新缓冲区,把字符写入到文件中
			
			String mess = null;
			//读取一行文字,返回的是一个字符串
			while((mess = br.readLine()) != null) {
				bw.write(mess);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bw.flush();
				fw.close();
				br.close();
				is.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

单纯 使用 字符流 进行读取文件并写入

	public static void sbRreader(File file) {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader(file);
			fw = new FileWriter("E:\/A测试流/pet3.txt");
			char[] cbuf = new char[512];
			int leng = 0;
			while((leng = fr.read(cbuf)) != -1) {
				fw.write(cbuf,0,leng);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		
	}

详细参考:Java中IO流,输入输出流概述与总结

猜你喜欢

转载自blog.csdn.net/mufeng633/article/details/82928172