Java中使用字節流類讀取文本內容


package cn.jbit.inputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            // 創建一個FileInputStream對象

            File file = new File("D:\\c.txt");

            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bs = new byte[100];
            
            // 將文件的內容讀入 數組中
            fileInputStream.read(bs);
            
            // 將byte數組轉成String
            String str = new String(bs);
            System.out.println(str);

            // 寫入文件
            FileOutputStream fileOutputStream = new FileOutputStream(
                    "D:\\c1.txt", true);
            fileOutputStream.write(bs, 0, bs.length);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM