2014-03-06 34 views
0
import java.io.File; 
import java.io.FileReader; 
import java.util.Scanner; 

public class lab4 { 

    public static void main(String[] args){ 

     /* 
     * Input is the name of the file and location typed by the user 
     * file is used as a new scanner of the file to later go into the FileReader 
     */ 

     String input; 
     Scanner file; 

     System.out.println("Please type the name of the file you wish to read into the program"); 

     // scanner to acquire input 
     Scanner scanner = new Scanner(System.in); 

     input = scanner.nextLine(); 

     System.out.println("the file input was " + input); 

     // tries to attach the specified file "input" to a new scanner "file" to later read into FileReader 
     try{ 
      file = new Scanner(new File(input)); 
     } 
     catch(Exception e){ 
      System.out.println("The requested file could not be found"); 
     } 

     FileReader(File file){ 
      while(file.hasNext()){ 
       String s = file.next(); 

      } 
     } 
    } 

} 

,我已經看過了例子,什麼IM做的應該是正確的,找不出問題用的FileReader

java: ')' expected 
java: illegal start of expression 
java: ';' expected 
java: class, interface, or enum expected 

錯誤指向FileReader的位置,所以顯然我使用它錯了,我不需要;我看到的例子是公共無效FileReader(文件「文件名」)的製作方法 我被告知把我的代碼在公共靜態void main(String [] args)

我看了YouTube並擡起頭API和沒有骰子。

+1

那你認爲'FileReader'塊是幹什麼的?它看起來像一個方法內部的方法(你不能這麼做),所以根據你的期望,你可以使它成爲一個方法或者只是創建一個FileReader實例。 – John3136

回答

1

更好的編碼做法可能是定義局部變量inputfile外的main()方法的...

public static String input;這樣的...

public static Scanner file;這...

雖然程序仍然可以工作,因爲代碼在使用前初始化這些局部變量。我認爲這可能一開始就是個問題,因爲編譯器有時候會很難解釋。儘管如此,只要他們使用static修飾符來處理main()的靜態上下文,就不會在main之外聲明這些變量。

Java不會自動初始化局部變量。如果在方法中使用之前未初始化,可能會導致錯誤。

另外FileReader是一個類,不能以與方法相同的方式使用。 首先,應該實例化FileReader對象。之後,您可以調用對象引用的方法,並通過引用更改FileReader對象上的成員字段的狀態。

根據Java 7 API,您可以通過3種方式實例化java.io.FileReader對象。 一種方法需要一個File對象,另一個需要一個String對象,另一個採用另一種我不熟悉的對象。

因此,舉例來說,你可以實例化的FileReader像這樣:

FileReader fR = new FileReader("myfile.csv"); 

FileReader fR2 = new FileReader(new File("myOtherFile.txt")); 

當你有時間閱讀本文件:http://docs.oracle.com/javase/7/docs/api/java/io/File.html

此外,檢查出這個傢伙的讀取文件中的代碼:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

最後,我編輯你的程序來讀取文件:

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

    public class lab4 { 

static BufferedReader br = null; 


public static void main(String[] args){ 
String s = null; 

    /* 
    * Input is the name of the file and location typed by the user 
    * file is used as a new scanner of the file to later go into the FileReader 
    */ 

    String input; 
    Scanner file; 

    System.out.println("Please type the name of the file you wish to read into the program"); 

    // scanner to acquire input 
    Scanner scanner = new Scanner(System.in); 

    input = scanner.nextLine(); 

    System.out.println("the file input was " + input); 

    // tries to attach the specified file "input" to a new scanner "file" to later read into FileReader 
    try{ 
     // wrap the FileReader object instantiated from the input variable in a 
     // BufferedReader object 
    br = new BufferedReader(new FileReader(input)); 

    // read each line to the console in this while loop that runs as long as it does not equal null 
    while((s = br.readLine()) != null){ 
    System.out.println(s); 
    } 
    } 
    catch(Exception e){ 
     System.out.println("The requested file could not be found"); 
    } 


} 

} 

快樂編碼!

請讓我知道這是否工作。

1

在做類似這樣的事情之前,您應該先運行Java的基礎知識。

無論如何,這裏有一個例子:

try 
    { 
     File file = new File("input-file.txt"); 
     FileReader fileReader = new FileReader(file); 
     BufferedReader bufferedReader = new BufferedReader(fileReader); 
     String line = null; 
     while ((line = bufferedReader.readLine()) != null) 
     { 
      // do stuff with the line that was read 
     } 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
+0

是的,我的第二個編程課,剛剛介紹到FileReader:S。仍然在基礎知識^ _ ^感謝您的幫助!即時通訊思想只是剝離我的代碼在主要的字符串,並與YouTube教程,只是不能使它的工作,這將看到一些例子星期五,並不會放棄太多的點,做它的另一種方式。 – user2884009

+0

我不是故意阻止你,這對於第二堂課非常適合。過一段時間後你會得到它的竅門。 'FileReader'是一個你「實例化」的類,而不是你調用的方法(就像你寫的那樣) - 這真的是唯一的誤解。祝好運夥伴! –