码迷,mamicode.com
首页 > 编程语言 > 详细

java.IO

时间:2014-06-11 11:41:53      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

java IO 是以文件相对程序的流向定义的 

 

bubuko.com,布布扣

javaIO流可分为三类

1、输入流,输出流

2、字节流,字符流

3、节点流,处理流(这里用到了装饰着模式)

bubuko.com,布布扣
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);
} }
bubuko.com,布布扣

大文件读写用循环读写

bubuko.com,布布扣
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();
            }
        }
    }
}
bubuko.com,布布扣

bubuko.com,布布扣

字节流和字符流

bubuko.com,布布扣
    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();
            }
        }
    }
}
bubuko.com,布布扣

字节流 和 处理流 的关系其实就是用装饰者模式 ,将需要处理的对象传到处理流里面.

 

java.IO,布布扣,bubuko.com

java.IO

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/kevinhuspace/p/3772809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!