扫描程序找不到我的文件以读取[带有Eclipse的Java] [英] Scanner cant find my file to read [Java with eclipse]

查看:103
本文介绍了扫描程序找不到我的文件以读取[带有Eclipse的Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个接受用户提供的条件的项目,并将其与包含相似条件的对象的已创建列表进行比较.

I'm working on a project that takes in criteria supplied by a user, and compares it to an already created list of object containing similar criteria.

当前,我正在尝试获取程序来读取文件,但是我一直在获取异常而不是我想要的异常.我的扫描仪和文件代码如下:

Currently, I'm trying to get the program to read the file, but I keep getting my exception and not what I want. My code for the scanner and file is as followed:

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的文件位于同一软件包中,但我不断收到文件:找不到houses.txt".更麻烦的是,我得到的错误是:

My file is in the same package, as I am using eclipse, but I keep getting "File: houses.txt is not found". To be thourough, the error I get is :

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)

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

If anyone could even point me in the direction of what I'm missing here I would greatly appreciate it!

推荐答案

使用

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

如果要尝试访问的houses.txtclass文件共享同一目录,则获取文件路径.

to get the file path if houses.txt and the class file from which you are trying to access shares the same directory.

您可以修改

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"));

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

Other case is that the file is in some other directory. In this scenario, provide relative path of this file ex.

//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后面加上文件分隔符.

Above, you need to end addRelativeFilePathHere with file.separator or prefix houses.txt with file separator.

链接以查看这些属性所指向和它们的含义和更多信息

This link to see what these properties points to and their meanings and for more inormation

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

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

这篇关于扫描程序找不到我的文件以读取[带有Eclipse的Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆