扫描仪无法找到我的文件阅读[Java with eclipse]

时间:2014-10-08 04:06:39

标签: java eclipse file java.util.scanner

我正在处理一个项目,该项目接受用户提供的标准,并将其与已创建的包含类似标准的对象列表进行比较。

目前,我正试图让程序读取文件,但我不断得到我的异常,而不是我想要的。我的扫描仪和文件代码如下:

package project205;
import java.util.*;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class HouseList {
ArrayList<House> houseList = new ArrayList<House>();
public HouseList(String fileName)
{

    //Open the data file

Scanner myFileIn = null;
    try
    {
      myFileIn = new Scanner(new File("houses.txt"));
    } 


    catch (FileNotFoundException e)
        {
            System.out.println("File: " + "houses.txt" + " is not found");
        }


    // First piece of data is the number of records
    int numRecords = myFileIn.nextInt();


    String address1;
    int price1;
    int area1;
    int numBedroom1;

    // Temp variable to accumulate the sum
    //double sum = 0.0;

    //Read the data line by line and build the 
    //array lists containing names and incomes
    for (int k = 0; k < numRecords; k++)
    {
        address1 = myFileIn.next();
        price1 = myFileIn.nextInt();
        area1 = myFileIn.nextInt();
        numBedroom1 = myFileIn.nextInt();

        House house1 = new House(address1, price1, area1, numBedroom1);
        houseList.add(house1);
     }
    // Close the input file
    myFileIn.close();

}

public String getHouses(Criteria c)
{
    String result = "";

    for(int i = 0; i < houseList.size(); i++)
    {
        House h1 = houseList.get(i);
        if (h1.satisfies(c))
        {
            result = result + h1.toString();
        }

    }
    return result;
}

public void printHouses(Criteria c)
{
    System.out.println(getHouses(c));
}

}

我的文件在同一个软件包中,因为我正在使用eclipse,但我一直得到&#34;文件:找不到Houses.txt&#34;。要成为过去,我得到的错误是:

File: houses.txt is not found
Exception in thread "main" java.lang.NullPointerException
at project205.HouseList.<init>(HouseList.java:29)
at project205.HouseListTester.main(HouseListTester.java:7)

如果有人甚至可以指出我在这里失踪的方向,我将不胜感激!

2 个答案:

答案 0 :(得分:0)

This may help you:   

      //creating File instance to reference text file in Java
        File text = new File("<file location>/houses.txt");

        //Creating Scanner instnace to read File in Java
        Scanner scnr = new Scanner(text);

        //Reading each line of file using Scanner class
        while(scnr.hasNextLine()){
            //process file
        }       

答案 1 :(得分:0)

使用

System.getProperty("user.dir") + System.getProperty("file.separator") + "houses.txt"

获取文件路径,如果houses.txt和您尝试访问的class文件共享同一目录。

你可以修改

myFileIn = new Scanner(new File("houses.txt"));
//to
myFileIn = new Scanner(new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "houses.txt"));
//or
myFileIn = new Scanner(new File(System.getProperty("file.separator") + "houses.txt"));

其他情况是该文件位于其他目录中。在这种情况下,请提供此文件的相对路径。

//current dir is c: and your file is in d: then do
myFileIn = new Scanner(new File("addRelativeFilePathHere" + "houses.txt"));

在上方,您需要使用addRelativeFilePathHere结束file.separator或使用文件分隔符结束前缀houses.txt

This链接查看这些属性指向的内容及其含义和更多信息

http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

http://docs.oracle.com/javase/7/docs/api/java/io/File.html