2016-02-07 152 views
1

我正在使用nio2在我的桌面上使用eclipse讀取外部文件。我得到以下代碼的例外。訪問外部文件

java.nio.file.NoSuchFileExceptionC:\Users\User\Desktop\JEE\FirstFolder\first.txt

請告知如何解決呢?也嘗試使用命令提示符。獲得相同的例外。

public class ReadingExternalFile { 

    public static void main(String[] args) { 

     Path p1= Paths.get("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt"); 
     System.out.println(p1.toString()); 
     System.out.println(p1.getRoot()); 

     try(InputStream in = Files.newInputStream(p1); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in))) 
      { 
      System.out.println("Inside try"); 
      String line=null;   
      while((line=reader.readLine())!=null){ 
       if (!line.equals("")) { 
        System.out.println(line); 
        } 
       //System.out.println(line); 
      } 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
    } 
} 

回答

0

我不明白爲什麼你用的是Path對象,你可以使用文件對象只是使文件,只是使用字符串作爲路徑,然後在文件讀取器對象包木窗它,然後包裹在一個緩衝的讀者,到底應該是這個樣子:

public static void main(String[] args) { 

    try { 
     File file = new File("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt"); 
     FileReader fr = new FileReader(file); 
     BufferedReader bfr = new BufferedReader(fr); 

     System.out.println(bfr.readLine()); 

     bfr.close(); 

    } catch (IOException e){ 
     e.printStackTrace(); 
    } 
} 

不要忘了閱讀和寫作後關閉您的視頻流,也可以使用可讀的名稱(不這樣做我所做的事情,使用有意義的名稱!)

+0

你好謝謝你的info.I我知道這樣,我努力學習NIO2並得到了錯誤,同時使用「路徑」 class.Can你建議如何在這種情況下使用nio2來讀取文件? – Yathish

+0

如果您可以添加控制檯輸出,包括問題底部的錯誤,並在您的問題中明確表示要使用nio2軟件包,那將會很不錯。 – James

0

試試下面的代碼希望這會幫助你。

Path p1= Paths.get("C:\\Users\\user\\Desktop\\FirstFolder\\first.txt"); 


try(
    BufferedReader reader = Files.newBufferedReader(p1, Charset.defaultCharset())) 
    { 
    System.out.println("Inside try"); 
    String line=null;   
    while((line=reader.readLine())!=null){ 
     if (!line.equals("")) { 
      System.out.println(line); 
      } 
     //System.out.println(line); 
    } 
} catch (IOException e) { 
    System.out.println(e); 
} 
+0

嗨,我試過了,但我得到了同樣的錯誤。 – Yathish

0

試試這個。

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:\\Users\\User\\Desktop\\FirstFolder\\first.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(); 
      } 
     } 

    } 
} 
+0

謝謝。我對這種方式很熟悉,可以告訴我如何使用Paths類讀取數據。我試圖學習nio2,並在嘗試時發現了這個錯誤。 – Yathish

+0

你在這裏使用了哪個版本的java? java 8 ?? –

+0

Nope正在使用Java 7. – Yathish

0
public static void main(String[] args) { 

try { 
    File file = new File("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt"); 
    FileReader freader = new FileReader(file); 
    BufferedReader bufreader = new BufferedReader(freader); 

    System.out.println(bufreader.readLine()); 

    bufreader.close(); 

} catch (IOException e){ 
    e.printStackTrace(); 
} 

}

+0

嗨謝謝你的信息。我知道這種方式,我正在嘗試學習nio2,並在使用「路徑」類時遇到了錯誤。你可以建議在這種情況下如何使用nio2來讀取文件? – Yathish