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

java IO

时间:2020-07-12 19:06:13      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:advance   str   exce   mamicode   pictures   console   exception   byte   res   

Introduction

技术图片


技术图片

An Example : FileReader

// read the file to console
    @Test
    public void test2(){
        FileReader fr = null;
        try {
            // step 1 : new the file
            File file = new File("/Users/truman/Desktop/a.txt");

            // step 2 : provide the IO STREAM
            fr = new FileReader(file);

            // step 3: read the file
            int data = fr.read(); // read() : return a character or -1 if reach the end

            while (data != -1) {
                System.out.print((char) data);
                data = fr.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // step 4: close the stream
            try {
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

Advance on Read()

 @Test
    public void testAdvancedRead(){
        FileReader fr = null;
        try {
            // I need a file
            File file = new File("/Users/truman/Desktop/a.txt");

            // create the stream
            fr = new FileReader(file);
//****************************************************************
            // read
            char[] cbuf = new char[5];
            int len ;
            while ((len = fr.read(cbuf)) != -1){
                for(int i = 0; i < len; i++){
                    System.out.print(cbuf[i]);
                }
            }
//****************************************************************           
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close the stream
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

FileWriter

  @Test
    public void testWrite(){
        FileWriter fw = null;
        try {
            // step 1: the file you wanna write to
            File file = new File("/Users/truman/Desktop/b.txt");

            // step 2 : create the writer
            fw = new FileWriter(file);

            // step 3: write()
            fw.write("Hello");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            // step 4 : close the resource
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

技术图片

Test of FileInputStream & FileOutputStream

@Test
    public void testFileCopy(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // the image file
            File image1 = new File("/Users/truman/Pictures/Lady.jpg");
            File image2 = new File("/Users/truman/Desktop/YoungLady.jpg");
            // create the fileInputStream and fileOutputStream
            fis = new FileInputStream(image1);
            fos = new FileOutputStream(image2);

            byte[] bbuf = new byte[1024];
            int len;
            while ((len = fis.read(bbuf)) != -1){
                fos.write(bbuf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close the resources
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

You should use BUFFERED then JUST FILEINPUTSTREAM or something

    @Test
    public void testBuffered(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(new File("/Users/truman/Pictures/loveActually.jpg")));
            bos = new BufferedOutputStream(new FileOutputStream(new File("/Users/truman/Desktop/love.jpg")));

            byte[] bbuf = new byte[1024];
            int len;
            while ((len = bis.read(bbuf)) != -1){
                bos.write(bbuf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close the resource
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

转换流 InputStreamReader OutputSteamWriter

技术图片


技术图片

java IO

标签:advance   str   exce   mamicode   pictures   console   exception   byte   res   

原文地址:https://www.cnblogs.com/nedrain/p/13288573.html

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