Java-从.data文件中读取

时间:2013-09-24 17:09:58

标签: java eclipse file-io

我在设置系统以从两个.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

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

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

ReadFile.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);
}
}

Person.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:未解决的编译问题:     令牌“file”上的语法错误,此令牌后预期的VariableDeclaratorId     文件无法解析为“

类型

并且正在抱怨ReadFile.java中的第9行,即行:

private static void readFile(file){

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

ReadFile rf = new ReadFile();

任何人都有任何想法,为什么会这样,以及我怎么能把它做好?

3 个答案:

答案 0 :(得分: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()方法也不接受任何参数。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您需要在ReadFile类定义中添加重载的构造函数。默认构造函数(隐式且不需要声明)不接受任何参数,但是您试图给它一个,即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方法中构造函数中设置的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()