-1

我在设置系统以从两个 .data 文件将数据读入 Java 程序时遇到了一些问题...

我使用 Eclipse 作为我的 IDE,并在我要使用的两个 .data 文件所在的文件夹中创建了项目。我才刚刚开始这个项目,所以我还处于起步阶段......

这两个 .data 文件是:car.data 和 owner.data,它们是我启动项目所必须的全部。

我创建了几个类:Owner.java、Car.java 和 ReadFile.java(从 .data 文件中读取数据)。

目前,我的 Owner.java 文件如下所示:

import java.io.*;

public class Owner {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    ReadFile rf = new ReadFile("Owner.data");
    rf.read("Owner.data");

}
File f;
public String id;
public String lastName;
public String firstName;
public String street;
public String city;

public void readOwner() throws FileNotFoundException{
    //File f = new File("Smart Stream Associate Software Engineer (Java) - Bristol/assessment/src/Owner.java");
    //InputStream IS = new FileInputStream(f);
}

}

我的 Car.java 文件如下所示:

public class Order {

public String orderID;
public String orderNo;
public String personID;


}

我的 ReadFile.java 文件如下所示:

import java.io.*;

public class ReadFile {
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
String[] data = new String[100];

public void read() throws IOException{
    FileReader fr = new FileReader("Person.data");
    BufferedReader br = new BufferedReader(fr);
    String line;

    int i = 0;
    while((line = br.readLine())!= null){
        data[i] = line;
        System.out.println(data[i]);
        i++;
    }
    br.close();

    String[] dataNew = new String[i];
    System.arraycopy(data, 0, dataNew, 0, i);
    data = dataNew;
    System.out.println("Data length: " + data.length);
}
}

目前,当我尝试从 Owner.java 类运行程序时(因为这是 main 方法所在的位置),我收到一个异常,上面写着:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor ReadFile(String) is undefined
The method read() in the type ReadFile is not applicable for the arguments (String)

它抱怨的那一行是:

ReadFile rf = new ReadFile("Owner.data");

有人可以向我指出为什么我会得到这个例外,以及我忘记做什么来避免得到它?提前谢谢了。

编辑 25/09/2013

所以,我尝试编辑我的代码以反映下面@sushain97 建议的更改,现在我有一个'Owner.java 类,如下所示:

import java.io.*;

public class Person {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    ReadFile rf = new ReadFile("Owner.data");
    rf.read();

}
File f;
public String id;
public String lastName;
public String firstName;
public String street;
public String city;

public void readPerson() throws FileNotFoundException{
    //File f = new File("Smart Stream Associate Software Engineer (Java) - Bristol/assessment/src/Person.java");
    //InputStream IS = new FileInputStream(f);
}

}

和一个如下所示的 ReadFile.java 类:

import java.io.*;

public class ReadFile {
//File file;
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
String[] data = new String[100];
private File file;


public ReadFile(String fileName){
    this.file = new File(fileName); 
}

public void read() throws IOException{
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line;

    int i = 0;
    while((line = br.readLine())!= null){
        data[i] = line;
        System.out.println(data[i]);
        i++;
    }
    br.close();

    String[] dataNew = new String[i];
    System.arraycopy(data, 0, dataNew, 0, i);
    data = dataNew;
    System.out.println("Data length: " + data.length);
}
}

但是,当我从 Owner.java 类运行我的代码时,我现在收到一条错误消息:

Exception in thread "main" java.io.FileNotFoundException: Owner.data (The system cannot find the file specified)

at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at ReadFile.read(ReadFile.java:15)
at Person.main(Owner.java:8)

我认为这意味着它无法找到“Owner.data”文件,但该文件存储在与“Owner.java”和“ReadFile.java”类存储位置相同的文件夹中......任何想法为什么它找不到该文件,我如何确保它可以找到?

编辑 25/09/2013 @ 09:45

据我了解,我已经编辑了我的代码以显示 PlanetSaro 在他们的回答中建议的更改,所以我现在有:

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

public class ReadFile {
static File file;
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
String[] data = new String[100];

private static void readFile(String fileName){
    try{
        File file = new File("Person.data");
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }
    scanner.close();
    } catch (FileNotFoundException e){
        e.printStackTrace();
    }

}

public void read(File file2) throws IOException{
    FileReader fr = new FileReader("Person.data");
    BufferedReader br = new BufferedReader(fr);
    String line;

    int i = 0;
    while((line = br.readLine())!= null){
        data[i] = line;
        System.out.println(data[i]);
        i++;
    }
    br.close();

    String[] dataNew = new String[i];
    System.arraycopy(data, 0, dataNew, 0, i);
    data = dataNew;
    System.out.println("Data length: " + data.length);
}
}

但我仍然收到一条错误消息Exception in thread "main" java.io.FileNotFoundException: Owner.data (The system cannot find the file specified)

我不明白这是为什么?

编辑 25/09/2013 @ 10:35

好的,所以我似乎无法从迄今为止给出的任何答案中得到这个工作(这可能只是因为我不完全理解答案 - 我已经对此发表了评论,所以如果是这种情况,请更全面地解释它们(或者基本上 - 我是初学者)。

但是,当我运行程序时,我设法减少了控制台中显示的错误数量。我的两个班级现在看起来像这样:

读取文件.java:

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

public class ReadFile {
static File file;
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
String[] data = new String[100];

private static void readFile(file){
    try{
        File file = new File("D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\srcPerson.data");
        Scanner scanner = new Scanner(file1);
        while(scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }
    scanner.close();
    } catch (FileNotFoundException e){
        e.printStackTrace();
    }

}

public void read(File file2) throws IOException{
    FileReader fr = new FileReader("D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\srcPerson.data");
    BufferedReader br = new BufferedReader(fr);
    String line;

    int i = 0;
    while((line = br.readLine())!= null){
        data[i] = line;
        System.out.println(data[i]);
        i++;
    }
    br.close();

    String[] dataNew = new String[i];
    System.arraycopy(data, 0, dataNew, 0, i);
    data = dataNew;
    System.out.println("Data length: " + data.length);
}
}

人.java:

import java.io.*;

public class Person {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    ReadFile rf = new ReadFile();
    rf.read(ReadFile.file);

}
static File f;
public String id;
public String lastName;
public String firstName;
public String street;
public String city;

public void readPerson() throws FileNotFoundException{
    //File f = new File("Smart Stream Associate Software Engineer (Java) - Bristol/assessment/src/Person.java");
    //InputStream IS = new FileInputStream(f);
}

}

我现在只收到一个控制台错误,它说:

“线程“主”java.lang.Error 中的异常:未解决的编译问题:令牌“文件”上的语法错误,无法将此令牌文件解析为类型后预期的 VariableDeclaratorId”

并抱怨 ReadFile.java 中的第 9 行,即该行:

private static void readFile(file){

和 Person.java 中的第 7 行,即该行:

ReadFile rf = new ReadFile();

任何人都有任何想法为什么会这样,以及我该如何正确处理?

4

3 回答 3

0

您尚未声明ReadFile可以接受String参数的构造函数。你需要一个像下面这样的。当您使用它时,添加一个File您可以在阅读时重复使用的字段。

public class ReadFile {
    private File file;
    public ReadFile(String file) {
        this.file = new File(file);
    }
    ... 
    // use the following in your read() method
    // FileReader fr = new FileReader(file);
}

然后你可以做

ReadFile rf = new ReadFile("Owner.data"); // "Owner.data" passed as an argument

您还将在下一行收到异常

rf.read("Owner.data");

因为您的read()方法也不接受任何参数。

使用您传递给构造函数的值来选择要从中读取的文件。

于 2013-09-24T17:12:40.307 回答
0

您需要在ReadFile类定义中添加一个重载的构造函数。默认构造函数(它是隐式的,不需要声明)不接受任何参数,但是你试图给它一个参数,即 a String: "Owner.data"

要解决此问题,您需要向ReadFile类添加自定义构造函数,如下所示:

public ReadFile(String fileName) //Constructor for a class has the same name as the Class
{
     //Define the fileObject that your read method needs to access to an instance variable
     this.file = new File(fileName); 
}

当然,这需要声明变量:

private File file; 

最后,您需要在方法file内部的构造函数中访问 set read

FileReader fr = new FileReader(file); //file here refers to the variable set earlier

所以,我们最终得到了一个稍微修改的ReadFile类:

public class ReadFile {
    String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
    String[] data = new String[100];
    private File file;

    public ReadFile(String fileName) {
     this.file = new File(fileName); 
    }

    public void read() throws IOException {
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;

        int i = 0;
        while((line = br.readLine())!= null){
            data[i] = line;
            System.out.println(data[i]);
            i++;
        }
        br.close();

        String[] dataNew = new String[i];
        System.arraycopy(data, 0, dataNew, 0, i);
        data = dataNew;
        System.out.println("Data length: " + data.length);
    }
}

最后,用法将从rf.read("Owner.data")变为rf.read()

于 2013-09-24T17:22:15.013 回答
0

这是因为,ReadFile 对象没有 String 类型的构造函数参数,并且方法 read() 没有传入任何 string 类型的参数。

于 2013-09-24T17:15:52.967 回答