zoukankan      html  css  js  c++  java
  • 字节流,字符流

    字节流

    File file = new File("C:\0928\test\aaa_new.txt"); // 实例化一个流  传入地址(文件位置)
    		File file_out = new File("C:\0928\test\aaa_new_out.txt"); // 实例化一个流  传入地址(文件位置)
    		// 输入流
    		InputStream in = null; // 字节输入流
    		OutputStream out = null; // 字节输出流
    		int result = -1;
    		try {
    			in = new FileInputStream(file); // 读取本地字节数据
    			out = new FileOutputStream(file_out); // 输出本地字节数据
    
    			while ((result = in.read()) != -1) {
    				// in.read() 判断是否输入完毕 完毕后 返回值为-1;
    				System.out.print((char) result);// unicode
    				out.write(result);
    			}
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			// 一定会执行的代码 放在 finally 里
    			try {
    				in.close(); // 关闭
    				out.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    

     字符流

    File file = new File("c:\0928\test\aaa_new.txt");
    		File fileout = new File("c:\0928\test\aaa_new_writer_out.txt");
    		int result = -1;
    		try {
    			//字符流 , 一次读取2字节
    			Reader reader = new FileReader(file); // 输入
    			Writer writer = new FileWriter(fileout); // 输出
    			while ((result = reader.read()) != -1) {
    				System.out.print((char)result);
    				writer.write(result);
    			}
    			reader.close();
    			writer.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
  • 相关阅读:
    Windows Phone 7 利用计时器DispatcherTimer创建时钟
    杭电1163 Eddy's digital Roots
    随感1
    杭电 1194 Beat the Spread!
    杭电 1017 A Mathematical Curiosity
    杭电1202 The calculation of GPA
    关于递归的思想
    杭电1197 Specialized FourDigit Numbers
    杭电 1062 Text Reverse
    杭电1196 Lowest Bit
  • 原文地址:https://www.cnblogs.com/bkyljk/p/8157694.html
Copyright © 2011-2022 走看看