Re: [問題] 為什麼開啟後的檔案內容都不見了!?

看板java作者 (黑輪)時間14年前 (2011/08/17 17:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《leochai (小虎)》之銘言: : 請各位大大幫我看一下: : import java.io.BufferedReader; : import java.io.File; : import java.io.FileInputStream; : import java.io.FileNotFoundException; : import java.io.IOException; : import java.io.InputStreamReader; : public class TransferFile { : public static void main(String[] args) { : File file = new File(args[0]); : try { : FileInputStream fis = new FileInputStream(file); : BufferedReader br = new BufferedReader(new InputStreamReader(fis)); : System.out.println(br.readLine()); : } catch (FileNotFoundException e) { : // TODO Auto-generated catch block : e.printStackTrace(); : } catch (IOException e) { : // TODO Auto-generated catch block : e.printStackTrace(); : } : } : } : 為什麼當我執行java TransferFile c:\leo.txt後,leo.txt的檔案內容都被清空, : 而且無論我檔案內容有幾行程式都只顯示一行呢? import java.io.*; public class TransferFile{ public static void main(String[] args) { File file = new File(args[0]); try { FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String temp=null; StringBuffer sb=new StringBuffer(); while((temp=br.readLine())!=null){ sb.append(temp); } br.close(); System.out.println(sb); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.254.200.84
文章代碼(AID): #1EIugd5p (java)
文章代碼(AID): #1EIugd5p (java)