标签:style class blog code java http
java IO 是以文件相对程序的流向定义的
javaIO流可分为三类
1、输入流,输出流
2、字节流,字符流
3、节点流,处理流(这里用到了装饰着模式)
public class IOtest { public static void main(String[] args) throws Exception{ String filename = "D:"+File.separator+"helloIo.txt"; File file = new File(filename); //crate a FileOutputStream FileOutputStream out = new FileOutputStream(file); String str = "hello to my test"; byte[] b =str.getBytes(); out.write(b); out.close(); FileInputStream fis = new FileInputStream("D:"+File.separator+"helloIo.txt"); byte[] buffer = new byte[b.length]; fis.read(buffer, 0, b.length); String strl = new String(buffer);
fis.close(); System.out.println(strl);
} }
大文件读写用循环读写
public class IOtest { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("d:/to.txt"); fis = new FileInputStream("d:/from.txt"); byte[] buffer = new byte[1024]; while(true){ int temp = fis.read(buffer, 0, buffer.length); if(temp == -1){ break; } fos.write(buffer, 0, temp); } } catch (Exception e) { // TODO Auto-generated catch block } finally{ try { fos.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
字节流和字符流
public static void main(String[] args) { // TODO Auto-generated method stub FileReader fir = null; BufferedReader bfr = null; try { fir = new FileReader("d:/buffer.txt"); bfr = new BufferedReader(fir); while(true){ String str = bfr.readLine(); if(str==null){ break; } System.out.println(str); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fir.close(); bfr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
字节流 和 处理流 的关系其实就是用装饰者模式 ,将需要处理的对象传到处理流里面.
标签:style class blog code java http
原文地址:http://www.cnblogs.com/kevinhuspace/p/3772809.html