我对java IO流简单的理解

一、Java IO流按流向可分为输入流(读数据)和输出流(写数据),按操作对象分可分为字符流和字节流。下面是IO流相关类的关系图:



二、文件的复制:

</pre><p></p><p></p><pre code_snippet_id="1628667" snippet_file_name="blog_20160330_1_7791617" name="code" class="java"><pre name="code" class="java">/**
	 * @param fileSource  要复制的文件
	 * @param fileCopy    复制后的文件
	 */
	private static void copyFile(File fileSource, File fileCopy) {
		InputStream is = null;
		OutputStream os = null;
		byte[] buffer = new byte[512];//每次读取512字节大小的数据
		int totalBytes = 0;
		try {
			is = new FileInputStream(fileSource);
			os = new FileOutputStream(fileCopy);

			while ((totalBytes = is.read(buffer)) != -1) {
				
				os.write(buffer, 0, totalBytes);

			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null && os != null) {
				try {
					is.close();
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}

	/**用字符流读取文件txt格式的文件*/
	private static void readFileByChar() {
		Reader reader = null;
		try {	
			File file=new File("d:/测试.txt");
			//设置读取的编码格式为“gbk”,如果读取的编码格式和该文件的编码格式不一致,则读出来的会是乱码
			reader = new InputStreamReader(new FileInputStream(file),"gbk");
			int tempchar;
			while ((tempchar = reader.read()) != -1) {			
				// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
				// 但如果这两个字符分开显示时,会换两次行。
				// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
				if (((char) tempchar) != '\r') {
					System.out.print((char) tempchar);
				}
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


 
/** 每次读一个字节数组大小的数据 ***/
	private static void readByByteArray() {
		File file = new File("d:/测试.txt");
		InputStream is = null;
		try {
			StringBuilder sb = new StringBuilder();
			is = new FileInputStream(file);
			byte[] bytes = new byte[1024];

			String str = "";
			int length=0;
			while ((length=is.read(bytes)) != -1) {
				str = new String(bytes,0,length,"gbk");
				sb.append(str);
			}
			
			System.out.println("-------sb---=" + sb.toString());

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}



/* 用缓冲流读取文件 **/
	private static void bufferReadFile() {

		File file = new File("d:/测试.txt");
		StringBuffer sb = new StringBuffer();
		String s = "";

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
			while (((s = reader.readLine()) != null)) {
				sb.append(s + "\n");
			}
			System.out.println("----readResult--=" + sb.toString());
			reader.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	/** 用StringReader读字符数据 **/
	
	private static void stringReadTest() {

		StringReader sr = new StringReader("马克思主义");
		int totalChar;

		char[] chars = new char[10];

		StringBuilder sb = new StringBuilder();
		String s = "";

		try {
			while ((totalChar = sr.read(chars)) != -1) {
				s = new String(chars, 0, totalChar);
				sb.append(s);
			}

			System.out.println("----result--=" + sb.toString());
			sr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}



/**用PrintWriter写数据**/
	
	private static void printWriterTest() {
		File file = new File("d:/测试.txt");
		PrintWriter writer = null;
		OutputStream os = null;
		try {
			os = new FileOutputStream(file,true);//“true”  写操作是追加 在文件末尾的
			writer = new PrintWriter(os);
			writer.write("----哈哈,马克思");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			writer.close();
		}

	}




  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值