首页 > 编程语言 > 详细

java.IO

时间:2014-06-11 11:38:55      阅读:326      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!