[問題] 如何分辨檔案有換行呀!?

看板java作者 (小虎)時間14年前 (2011/08/18 15:57), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
原始碼如下: package com.example.leo.p2p; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class TransferFile { public static void main(String[] args) { File file1 = new File("c:\\from.txt"); File file2 = new File("c:\\to.txt"); try { FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file2); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); String s = br.readLine(); while (s != null) { bw.write(s); s = br.readLine(); } bw.flush(); bw.close(); br.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 請問我讀出來的檔案內容怎麼都不會換行呀,原始檔案內容是有換行的呦!? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.100.51

08/18 16:16, , 1F
readLine 不包含 \n,你在 write 要自己加上去....
08/18 16:16, 1F

08/18 16:19, , 2F
08/18 16:19, 2F

08/18 16:27, , 3F
not including any line-termination characters
08/18 16:27, 3F

08/19 06:12, , 4F
newLine();
08/19 06:12, 4F
文章代碼(AID): #1EJCO5dQ (java)