java IO

2021-04-19 09:30

阅读:469

标签: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 

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


评论


亲,登录后才可以留言!