IO流学习总结

java 中 IO 流分为几种?

按照流的流向分,可以分为输入流和输出流;
按照操作单元划分,可以划分为字节流和字符流;
按照流的角色划分为节点流和处理流。

Java Io流共涉及40多个类,这些类看上去很杂乱,但实际上很有规则,而且彼此之间存在非常紧密的联系, Java I0流的40多个类都是从如下4个抽象类基类中派生出来的。
InputStream/Reader: 所有的输入流的基类,前者是字节输入流,后者是字符输入流。
OutputStream/Writer: 所有输出流的基类,前者是字节输出流,后者是字符输出流。

按操作方式分类结构图:
在这里插入图片描述
按操作对象分类结构图:
在这里插入图片描述

字节流和字符流概念

字节流:字节流读取的时候,读到一个字节就返回一个字节;主要用于读取图片,MP3,AVI视频文件。
字符流:字符流使用了字节流读到一个或多个字节,如读取中文时,就会一次读取2个字节。只要是处理纯文本数据,就要优先考虑使用字符流。

常见文件操作

创建、删除文件夹

private static void createFile() {
		String path = "F:\\My\\Tmp\\file";
		File myFile = new File(path);

		if (!myFile.exists()) {
		    // 创建文件夹
		    myFile.mkdir(); //可以建立多级文件夹, mkdir()只会建立一级的文件夹
		    // myFile.mkdirs();
		   
		    System.out.println("file文件夹创建成功!!!");
		} /*else {
			 // 删除文件夹
		    myFile.delete();
		    System.out.println("file文件夹删除成功!!!");
		}*/
		
	}

创建、删除文件

private static void createTxt(String path,String filename) throws Exception {
		File myFile = new File(path,filename);
		if (!myFile.exists()) {
		    // 创建文件(前提是目录已存在,若不在,需新建目录即文件夹)
		    myFile.createNewFile();
		    System.out.println("file文件创建成功!!!");
		}
	}

写文件

/**
	 *  第一种:字节流FileOutputStream
	 * @param myFile
	 * @param content
	 * @throws Exception
	 */
	private static void createFileOutputStream(String myFile,String content) throws Exception {
		FileOutputStream fop = new FileOutputStream(myFile); 
		byte[] contentInBytes = content.getBytes();
		fop.write(contentInBytes);  
		fop.flush();  
		fop.close(); 
		System.out.println("file文件写入成功!!!");
	}
	
	/**
	 *  第二种:FileWriter(参数true为追加内容,若无则是覆盖内容)
	 * @param myFile
	 * @param content
	 * @throws Exception
	 */
	private static void createFileWriter(String myFile,String content) throws Exception {
		FileWriter fw = new FileWriter(myFile,true);
		fw.write(content);
		fw.close();
		System.out.println("file文件写入成功!!!");
	}
	
	/**
	 *  第三种:BufferedWriter
	 * @param myFile
	 * @param content
	 * @throws Exception
	 */
	private static void createBufferedWriter(String myFile,String content) throws Exception {
		BufferedWriter bw = new BufferedWriter(new FileWriter(myFile,true));
		bw.write(content);  
		bw.flush();  
		bw.close();
		System.out.println("file文件写入成功!!!");
	}
	
	/** 第四种:打印流PrintStream和PrintWriter
	 *  字节打印流:PrintStream
	 *  字符打印流:PrintWriter
	 * @param myFile
	 * @param content
	 * @throws Exception
	 */
	private static void createPrintWriter(String myFile,String content) throws Exception {
		PrintWriter pw = new PrintWriter(new FileWriter(myFile,true));   
		pw.println(content);      // 换行
//		pw.print(content);        // 不换行
		pw.close();
		System.out.println("file文件写入成功!!!");
	}

读文件

/**
	 * 第一种:以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 
	 * @throws Exception 
	 */
	private static void getFileOutputStream(String myFile) throws Exception {
		InputStream in = new FileInputStream(myFile);
		// 一次读多个字节
		int byteread = 0;
		byte[] tempbytes = new byte[100];
		while ((byteread = in.read(tempbytes)) != -1) {  
		    System.out.write(tempbytes, 0, byteread);  
		}
		
		in.close();
	}
	
	/**
	 *  第二种:以字符为单位读取文件,常用于读文本,数字等类型的文件 
	 * @param myFile
	 * @throws Exception
	 */
	private static void getInputStreamReader(String myFile) throws Exception {
		Reader reader = new InputStreamReader(new FileInputStream(myFile)); 

		// 一次读多个字节
		char[] tempchars = new char[30];  
		int charread = 0;  
		// 读入多个字符到字符数组中,charread为一次读取字符数  
		while ((charread = reader.read(tempchars)) != -1) {  
		    // 同样屏蔽掉\r不显示  
		    if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) {  
		        System.out.print(tempchars);  
		    } else {  
		        for (int i = 0; i < charread; i++) {  
		            if (tempchars[i] == '\r') {  
		                continue;  
		            } else {  
		                System.out.print(tempchars[i]);  
		            }  
		        }  
		    }  
		}
		reader.close();
	}
	
	/**
	 * 第三种:以行为单位读取文件,常用于读面向行的格式化文件 
	 * @param myFile
	 * @throws Exception
	 */
	private static void getBufferedReader(String myFile) throws Exception {
		BufferedReader reader = new BufferedReader(new FileReader(myFile));
		String tempString = null;  
		// 一次读入一行,直到读入null为文件结束  
		while ((tempString = reader.readLine()) != null) {  
		    System.out.println(tempString);  
		}  
		reader.close();
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值