2012-05-14 260 views
5

我目前正在研究我的cpe類中的實驗,我們必須創建一個簡單的程序,它可以從.txt文件掃描字符串並打印他們到一個不同的.txt文件。到目前爲止,我已經繪製出了基本程序,但是我的例外一直在拋出,儘管我擁有所有必需的文件。任何人都可以幫我調試嗎?文件I/O:從一個文件讀取並寫入另一個文件(Java)

import java.io.*; 
import java.util.*; 

public class FileIO { 

public static void main(String args[]) {   
    try { 
     File input = new File("input"); 
     File output = new File("output"); 
     Scanner sc = new Scanner(input); 
     PrintWriter printer = new PrintWriter(output); 
     while(sc.hasNextLine()) { 
      String s = sc.nextLine(); 
      printer.write(s); 
     } 
    } 
    catch(FileNotFoundException e) { 
     System.err.println("File not found. Please scan in new file."); 
    } 
} 
} 
+1

你的輸入/輸出文件不具有擴展名? – apnorton

+1

他已經說過他們是.txt文件了...所以看起來就是這個問題。對計劃進行編程的計算機提供一些建議:將文件瀏覽器設置爲始終顯示文件擴展名。 – Tharwen

+0

不要忘記關閉掃描儀和寫入器。 –

回答

4

您需要計算出"input"文件的外觀。當您只指定"input"時,它會在當前工作目錄中查找該文件。使用IDE時,該目錄可能不是您想象的那樣。

嘗試以下操作:

System.out.println(new File("input").getAbsolutePath()); 

,看看它會查找該文件。

+0

謝謝!事實證明,我的.txt文件在錯誤的文件夾中。所以它找到了它們,但現在它不會將我的input.txt寫到output.txt。 Output.txt仍然是空的。 – Mike

+0

*這是*,因爲你不會調用flush。我建議你在完成後調用printer.close()(這會爲你刷新它)。 – aioobe

+0

謝謝!它現在有效!我關閉了掃描儀,但沒有打印機。 – Mike

3

使用Java I/O訪問文件時,必須包含文件的文件類型擴展名(如果存在)。

File input = new File("input.txt"); 
    File output = new File("output.txt"); 
+0

之前我有.txt擴展名,我仍然會拋出異常。我正在使用eclipse,並且.txt文件與我的.java文件一起位於src中。 – Mike

+0

我添加了擴展,並將文件移動到正確的文件夾中,但是現在每次運行我的程序時,我的output.txt都會更新,但是當我打開它時,仍然沒有任何內容。 – Mike

2

可能你只是使用的FileInputStream對象讀取文件忘記flush()

 try { 
      File input = new File("input"); 
      File output = new File("output"); 
      Scanner sc = new Scanner(input); 
      PrintWriter printer = new PrintWriter(output); 
      while (sc.hasNextLine()) { 
       String s = sc.nextLine(); 
       printer.write(s); 
      } 
      **printer.flush();** 
     } 
     catch (FileNotFoundException e) { 
      System.err.println("File not found. Please scan in new file."); 
     } 
+0

他說他得到一個例外。 – aioobe

+0

@aioobe你的例外是什麼? – isvforall

+0

*我*沒有任何例外,* Mike *(發佈該問題的人)擁有。 – aioobe

0

我們可以做到這一點,並寫入使用FileOutputStream中對象另一個文件。

下面是示例代碼

package java_io_examples; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.Vector; 

public class Filetest { 

    public static void main(String[] args) { 

    try { 

      FileInputStream fin = new FileInputStream("D:\\testout.txt"); 

      int i = 0; 
      String s = ""; 

      while((i=fin.read())!=-1) { 

       s = s + String.valueOf((char)i); 

      } 

      FileOutputStream fout = new 
      FileOutputStream("D:\\newtestout1.txt"); 
      byte[] b = s.getBytes(); 

      fout.write(b); 
      fout.close(); 

      System.out.println("Done reading and writing!!"); 

     } catch(Exception e){ 
     System.out.println(e); 
     } 

    } 

} 
0
public void readwrite() throws IOException 
{ 
    // Reading data from file 
    File f1=new File("D:/read.txt"); 
    FileReader fr=new FileReader(f1); 
    BufferedReader br=new BufferedReader(fr); 

    String s = br.readLine(); 

    // Writing data 
    File f2=new File("D:/write.txt"); 
    FileWriter fw=new FileWriter(f2); 
    BufferedWriter bw=new BufferedWriter(fw); 
    while(s!=null) 
    { 
     bw.write(s); 
     bw.newLine(); 
     System.out.println(s); 
     s=br.readLine(); 

    } 
    bw.flush(); 
    bw.close(); 

} 
相關問題