1. 程式人生 > >Java IO總結

Java IO總結

IO流

抽象基類 節點流 緩衝流 轉換流
Inputstream FileInputStream bufferinputstream
outputstream fileoutputstream bufferoutputstream (flush())
reader filereader bufferreader inputstreamreader(將位元組流轉化為字元流)
writer filerwriter bufferwriter (flush()) outputstramwriter(將位元組流轉化為字元流)

節點流

位元組流

  1. OutputStream(向檔案中寫資料)
    @Test
    public void write() {
        File file = new File("E:\\log\\log.log");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(new String("我叫龍海成").getBytes());
            fileOutputStream.close();
        } catch
(FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
  1. InputStream(活得指定檔案中的內容)
    @Test
    public void getcontent
() { File file = new File("E:\\log\\hhk.txt"); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); byte[] s = new byte[1024]; int b; while ((b = fileInputStream.read(s)) != -1) { String str = new String(s, 0, b); System.out.println(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
  1. 檔案的複製和黏貼實現
@Test
    public void copy() {
        FileInputStream input = null;
        FileOutputStream outputStream = null;
        try {
            input = new FileInputStream("E:\\log\\1.txt");
            outputStream = new FileOutputStream("E:\\log\\2.txt",true);
            byte[] b = new byte[1024];
            int num;
            while ((num = input.read(b)) != -1) {
                outputStream.write(new String(b, 0, num).getBytes());
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

字元流

1.filereader:讀入檔案

    @Test
    public void getread() {
        File file = new File("E:\\log\\2.txt");
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        char[] c = new char[1024];
        int a;
        try {
            while ((a = fileReader.read(c)) != -1) {
                String s = new String(c, 0, a);
                System.out.println(s);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

2.檔案的黏貼複製

@Test
    public void copy() {
        File file1 = new File("E:\\log\\log.log");
        File file2 = new File("E:\\log\\1.txt");
        FileReader reader = null;
        FileWriter writer = null;
        try {
            reader = new FileReader(file2);
            writer = new FileWriter(file1,true);
            char[] ch = new char[1024];
            int a;
            while((a = reader.read(ch)) != -1) {
                writer.write(new String (ch,0,a));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

緩衝流(速度比節點流更快BUff)

  1. 讀取檔案
    @Test
    public void readfile() {
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("E:\\log\\1.txt")));
            byte[] b = new byte[1024];
            int a;
            while ((a = bufferedInputStream.read(b)) != -1) {
                String af = new String(b, 0, a);
                System.out.println(af);

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferedInputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

2.檔案的複製與黏貼

@Test
    public void readfile() {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("E:\\log\\1.txt")));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("E:\\log\\hhk.txt")));
            byte[] b = new byte[1024];
            int a;
            while ((a = bufferedInputStream.read(b)) != -1) {
                bufferedOutputStream.write(new String(b, 0, a).getBytes());
                bufferedOutputStream.flush();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.close();
                bufferedInputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

3.bufferreader讀取檔案中的資料

    @Test
    public void bufferreader() {
        File file = new File("E:\\log\\1.txt");
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader readers = new BufferedReader(reader);
        String str;
        try {
            while ((str = readers.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

轉換流(inputstreamreader和outputstreamwrite)

    @Test
    public void getreader() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        BufferedReader bufferreader = null;
        BufferedWriter bw = null;
        try {
            fis = new FileInputStream(new File("E:\\log\\1.txt"));
            isr = new InputStreamReader(fis, "utf8");
            bufferreader = new BufferedReader(isr);
            fos = new FileOutputStream(new File("E:\\log\\2.txt"));
            osw = new OutputStreamWriter(fos, "utf8");
            bw = new BufferedWriter(osw);
            String str;
            while ((str = bufferreader.readLine()) != null) {
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
            System.out.println("結束");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bufferreader.close();
                isr.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

標準輸入輸出流


  • System.in
  • System.out
從鍵盤讀取輸入,並在控制檯輸出
    @Test
    public void getstream() {
        BufferedReader bufferedReader = null;
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while (true) {
            System.out.println("請輸入字串:");
            try {
                str = bufferedReader.readLine();
                if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {
                    System.out.println("程式結束");
                    break;
                }
                String s = str.toUpperCase();
                System.out.println(s);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

隨機檔案的讀取

RandomAccessFile類

    @Test
    public void accessfile() {
        RandomAccessFile r1 = null;
        RandomAccessFile r2 = null;
        try {
            r1 = new RandomAccessFile(new File("E:\\log\\log.log"), "r");
            r2 = new RandomAccessFile(new File("E:\\log\\2.txt"), "rw");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] b = new byte[1024];
        int num;
        try {
            while ((num = r1.read(b)) != -1) {
                r2.write(b, 0, num);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (r2 != null) {
                try {
                    r2.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (r1 != null) {
                try {
                    r1.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

在某個點之後插入字元

    @Test
    public void insertchar() {
        RandomAccessFile radomaccessfile = null;
        try {
            radomaccessfile = new RandomAccessFile(new File("E:\\log\\2.txt"), "rw");
            try {
                radomaccessfile.seek(3);
                radomaccessfile.write("軟體".getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (radomaccessfile != null) {
                try {
                    radomaccessfile.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }