2013-08-31 42 views
0

我想不出對我的生活有什麼不對這個程序:如何打開文本文件?

 import java.io.*; 

    public class EncyptionAssignment 
    { 
     public static void main (String[] args) throws IOException 
     { 
      String line; 
      BufferedReader in; 

      in = new BufferedReader(new FileReader("notepad encypt.me.txt")); 
      line = in.readLine(); 

      while(line != null) 
      { 
        System.out.println(line); 
        line = in.readLine(); 
      } 

      System.out.println(line); 
     } 

    } 

該錯誤消息說,文件無法找到,但我知道該文件已經存在。我是否需要將文件保存在特殊文件夾中?

+0

是在項目的文件夾中的文件? – ChiefTwoPencils

+0

你認爲該文件在哪裏? –

+3

文件的名稱是什麼:「notepad encypt.me.txt」或「encypt.me.txt」? – Julien

回答

-1
package com.mkyong.io; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class BufferedReaderExample { 

    public static void main(String[] args) { 

     BufferedReader br = null; 

     try { 

      String sCurrentLine; 

      br = new BufferedReader(new FileReader("C:\\testing.txt")); 

      while ((sCurrentLine = br.readLine()) != null) { 
       System.out.println(sCurrentLine); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null)br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

    } 
} 

參考:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

+1

請總結鏈接。 –

+0

他有很棒的教程,但只鏈接只有答案是不鼓勵的。 –

2

該錯誤是"notepad encypt.me.txt"。 由於您的文件名爲「encypt.me.txt」,因此您不能在其名稱前面放置「記事本」。此外,名爲「notepad encypt.me.txt」的文件可能不存在或者不是您想要打開的文件。

此外,如果文件不在您的項目文件夾中,則必須提供文件的路徑(絕對或相對)。

我將假設您在Microsoft Windows系統上。

如果您的文件具有「C:\ foo \ bar \ encypt.me.txt」的絕對路徑,則必須將其作爲"C:\\foo\\bar\\encypt.me.txt""C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt"傳遞。

如果仍然沒有工作,你應該驗證文件:

1)存在於提供的路徑。 您可以通過下面的一段代碼做到這一點:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt"); 
System.out.println(encyptFile.exists()); 

如果提供的路徑是正確的,它應該是真實的。

2)可以通過應用程序可以讀取

您可以通過下面這段代碼做到這一點:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt"); 
System.out.println(encyptFile.canRead()); 

如果你需要閱讀文件的權限,它應該是真實的。

更多信息:

Javadoc of File

Informations about Path in computing

0
import java.io.*; 

public class Test { 
    public static void main(String [] args) { 

    // The name of the file to open. 
    String fileName = "temp.txt"; 

    // This will reference one line at a time 
    String line = null; 

    try { 
     // FileReader reads text files in the default encoding. 
     FileReader fileReader = 
      new FileReader(fileName); 

     // Always wrap FileReader in BufferedReader. 
     BufferedReader bufferedReader = 
      new BufferedReader(fileReader); 

     while((line = bufferedReader.readLine()) != null) { 
      System.out.println(line); 
     } 

     // Always close files. 
     bufferedReader.close();   
    } 
    catch(FileNotFoundException ex) { 
     System.out.println(
      "Unable to open file '" + 
      fileName + "'");     
    } 
    catch(IOException ex) { 
     System.out.println(
      "Error reading file '" 
      + fileName + "'");     
     // Or we could just do this: 
     // ex.printStackTrace(); 
     } 
    } 
}