简体   繁体   English

使用Scanner读取文件:为什么在Java中使用Scanner读取文件时出现错误?

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

This example demonstrates using Scanner to read a file line by line (it does not perform a write operation) I don't know why I get an error when I try to compile. 本示例演示了如何使用Scanner逐行读取文件(它不执行写操作)我不知道为什么在尝试编译时会出错。 Could somebody explain the reason to me?. 有人可以向我解释原因吗? I'm using jcreatorLE and JDK 1.6 to run my program: 我正在使用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;
  }
} 

This is the result from running it: 这是运行它的结果:

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

Process completed.

When you lifted that code from here :-), you renamed the class but not the constructor. 当您从此处提取代码时:-),您重命名了该类,但未重命名该构造函数。 Only constructors are allowed to not have return types. 只允许构造函数没有返回类型。

I suggest you either rename the class back or rename the constructor. 我建议您重命名该类或重命名该构造函数。

I hope this isn't homework. 我希望这不是功课。 As it stands, your educator would have an easy time proving plagiarism. 就目前而言,您的教育者将很容易证明time窃。 You'll need to at least change the variable names as well as class names, you might want to also reformat it a bit including changing the order of methods in the class. 您至少需要更改变量名以及类名,您可能还需要对其进行一些重新格式化,包括更改类中方法的顺序。

That's if it's homework. 那是功课。 Which it's not, right? 不是吗? :-) :-)

Your "ReadWithScanner" constructor needs to match the name of the class ("File_read") 您的“ ReadWithScanner”构造函数需要匹配类的名称(“ File_read”)

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

Your class is named File_read and your constructor is named ReadWithScanner . 您的类名为File_read ,构造函数名为ReadWithScanner The warning is that your constructor needs to be named the same as the class. 警告是您的构造函数需要与该类相同的名称。

The name of the class is File_read, so the constructor name should be File_read but you gave the name as ReadWithScanner that is why its complaining. 该类的名称为File_read,因此构造函数名称应为File_read,但您将名称命名为ReadWithScanner,这就是其抱怨的原因。 Compiler thinking its a method name so expecting a return type. 编译器认为它是方法名,因此期望返回类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM