簡體   English   中英

使用Scanner讀取文件:為什么在Java中使用Scanner讀取文件時出現錯誤?

[英]read a file using Scanner: Why am I getting an error when using Scanner for read files in java?

本示例演示了如何使用Scanner逐行讀取文件(它不執行寫操作)我不知道為什么在嘗試編譯時會出錯。 有人可以向我解釋原因嗎? 我正在使用jcreatorLE和JDK 1.6運行我的程序:

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

public final class File_read {

  public static void main(String... aArgs) throws FileNotFoundException {
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
    parser.processLineByLine();
    log("Done.");
  }

  /**
  * @param aFileName full name of an existing, readable file.
  */
  public ReadWithScanner(String aFileName){
    fFile = new File(aFileName);  
  }

  /** Template method that calls {@link #processLine(String)}.  */
  public final void processLineByLine() throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile);
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
        processLine( scanner.nextLine() );
      }
    }
    finally {
      //ensure the underlying stream is always closed
      scanner.close();
    }
  }

  /** 
  * Overridable method for processing lines in different ways.
  *  
  * <P>This simple default implementation expects simple name-value pairs, separated by an 
  * '=' sign. Examples of valid input : 
  * <tt>height = 167cm</tt>
  * <tt>mass =  65kg</tt>
  * <tt>disposition =  "grumpy"</tt>
  * <tt>this is the name = this is the value</tt>
  */
  protected void processLine(String aLine){
    //use a second Scanner to parse the content of each line 
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if ( scanner.hasNext() ){
      String name = scanner.next();
      String value = scanner.next();
      log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
    }
    else {
      log("Empty or invalid line. Unable to process.");
    }
    //(no need for finally here, since String is source)
    scanner.close();
  }

  // PRIVATE //
  private final File fFile;

  private static void log(Object aObject){
    System.out.println(String.valueOf(aObject));
  }

  private String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
  }
} 

這是運行它的結果:

--------------------Configuration: <Default>--------------------
C:\Users\administrador\Documents\File_read.java:15: invalid method declaration; return type required
  public ReadWithScanner(String aFileName){
         ^
1 error

Process completed.

當您從此處提取代碼時:-),您重命名了該類,但未重命名該構造函數。 只允許構造函數沒有返回類型。

我建議您重命名該類或重命名該構造函數。

我希望這不是功課。 就目前而言,您的教育者將很容易證明time竊。 您至少需要更改變量名以及類名,您可能還需要對其進行一些重新格式化,包括更改類中方法的順序。

那是功課。 不是嗎? :-)

您的“ ReadWithScanner”構造函數需要匹配類的名稱(“ File_read”)

public File_read(String aFileName){
    fFile = new File(aFileName);  
}

您的類名為File_read ,構造函數名為ReadWithScanner 警告是您的構造函數需要與該類相同的名稱。

該類的名稱為File_read,因此構造函數名稱應為File_read,但您將名稱命名為ReadWithScanner,這就是其抱怨的原因。 編譯器認為它是方法名,因此期望返回類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM