Java中使用字節流類讀取文本內容
阿新 • • 發佈:2017-09-30
length inpu pre puts output 寫入 log oid cnblogs
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數組轉成StringString 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(); } } }
Java中使用字節流類讀取文本內容