2012-01-09 128 views
0

我正在使用此代碼使用filereader對象在java中讀取文件。然而,應用程序會拋出異常,說明它無法找到file.Can誰能幫助這個新手傢伙剛進入Java編程簡單java代碼中的FileNotfound異常

import java.util.*; 
import java.io.*; 
import java.util.Scanner; 
import java.io.FileReader; 
import java.io.PrintWriter; 


public class CFileReader { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws FileNotFoundException 
    { 
     String objLine; 
     String strInputFileName; 
     FileReader objReader = null; 
     Scanner objScanner; 
     File objFile; 

     if(args.length > 0) 
     { 
      for(int i = 0;i < args.length ;i++) 
      { 
       //Gets the arguments into the strInputFileName variable 
       strInputFileName = args[i]; 

       System.out.println("Filename entered was : " + strInputFileName); 

       //Create a file object which points to the filename i.e strInputFileName 
       objFile = new File(strInputFileName); 

       //Create a FileReader object with the File object i.e objFile as the input parameter 
       objReader = new FileReader(objFile); 

       System.out.println("Filereader object is : " + objReader.toString()); 

       //Create a scanner object with FileReader as input parameter 
       objScanner = new Scanner(objReader); 

       System.out.println(args); 


       //Scans the file if it has next line 
       while(objScanner.hasNextLine()) 
       { 
        //Store the contents i.e. first line in the objLine Variable 
        objLine = objScanner.nextLine(); 


        //prints the contents 
        if(objLine.indexOf(i) > 0) 
         { 
          System.out.println(objLine); 
         } 
       } 


      } 

     } 
     else 
     { 
      //Warn the user to enter the command line arguments if he has not entered 
      System.out.println("Please Enter Command line args"); 
     } 

    } 

} 

編譯我使用

javac CFileReader.java 

和運行程序

java CFileReader "C:\\Hello.txt" 

其中hello.txt的是一些內容的簡單文本文件

非常感謝您提前致謝

+2

你可以打印stacktrace(錯誤信息?) – OscarRyz 2012-01-09 18:03:19

+0

你的(root)'C:'目錄中是否存在'Hello.txt'?你真的需要雙倍\\在那裏? (不是100%肯定後者)。 – 2012-01-09 18:05:20

+0

有一件事要檢查:嘗試打印出'objFile.getAbsolutePath()'(在創建文件後),看看它是否是你期望的。我不確定這些命令將如何解析。 – 2012-01-09 18:06:01

回答

2

除了@Adel指出的args [0]錯誤,您也不必在命令行中跳過「\」...「C:\ Hello.txt「就足夠了。

0

我一般把下面幾行我的File對象及其使用之間:

if (!objFile.isFile() || !objFile.exists()) { 
    System.out.println("Could not find file: " + objFile.getPath().toString(); 
} 

這往往會顯示在文件名中的字符串格式的問題。

+0

我使用了這段代碼,但應用程序在這個階段沒有拋出任何異常,但只有當我嘗試使用filereader對象讀取文件時 – 2012-01-09 18:28:19

+0

不顯示任何內容,那麼讀取文件時會出現實際問題 - 文件有效但無法讀取。如果我不得不猜測,我會說權限?因爲據我所知,其餘的設置是正確的。我會檢查以確保您嘗試閱讀的文件具有開放權限。我從來沒有使用掃描儀對象,但它可能有限制,或者我可能不知道它的用法。 – vextorspace 2012-01-10 12:49:19

+0

任何有關Windows 7操作系統特殊處理的想法? – 2012-01-23 05:51:53

0

該代碼適用於我的MacOS系統,問題在於您在Windows中編寫文件路徑的方式。在我的情況下:

java CFileReader /home/jenaiz/test.txt 
1

你不需要\\。 Java不會解析輸入字符串中的轉義序列,並且單個反斜槓是Windows shell中的路徑分隔符。

0

我在Windows上自己編譯和測試了這個代碼,它工作正常。您所在的文件不存在,或者您在命令行中鍵入文件名稱時出錯。

+0

奇怪我還是不能運行這個x( – 2012-01-09 18:16:31

+0

你是否100%確定你正在嘗試使用的文件存在? – Peter 2012-01-09 18:18:32

0

檢查您的輸入文件是否存在。當我在機器上運行你的代碼時,我沒有權限在C:的根目錄下創建一個文件。你是如何創建測試文件的?

當我將文件移動到另一個驅動器時,例如「t:\ hello.txt」它第一次運作。

+0

我試圖使用「D」驅動器上的測試文件..但仍然編無法搜索n找到文件:( – 2012-01-09 18:22:50

+0

嘗試將文件移動到您的源所在的同一目錄。這樣您就知道您擁有讀/寫權限並可以排除此問題。 – 2012-01-09 18:49:03