很难将文本文件读入java(Eclipse)

时间:2014-12-10 07:21:38

标签: java eclipse

我很难尝试将文本文件读入java。基本上,当程序告诉我“输入文件名”时,我会这样做。然而它回来了

java.io.FileNotFoundException: Students.txt (The system cannot find the file specified)

我不知道为什么会这样做。我花了几个小时在互联网上寻求帮助和nada。我添加了一个截图,以显示我的文件。我不知道它是否有帮助。

Here is a screen shot of how I have my files. I don't know if it helps or not.

我已经尝试添加import java.FileNotFoundException而没有添加任何东西。

所以这是我的代码:

import java.util.*;

import java.io.*;

import java.util.Scanner;

public class Match 
{


    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter file name: ");
        String filename = input.next();


        Student[] student = new Student[100];

        for (int i=0; i < 100; i++)
            student[i]= new Student();

        try 
        {
            Scanner kbd = new Scanner (new FileReader(filename));
            int count = 0;

            while (kbd.hasNextLine())
            {
                Scanner line = new Scanner(kbd.nextLine());
                line.useDelimiter("[\t-]");

                student[count].setname(line.next());
                student[count].setgender(line.next().charAt(0)); 
                student[count].getbirthDay().setmonth(line.nextInt()); 
                student[count].getbirthDay().setday(line.nextInt());
                student[count].getbirthDay().setyear(line.nextInt());
                student[count].getpref().setquietTime(line.nextInt());
                student[count].getpref().setmusic(line.nextInt());
                student[count].getpref().setreading(line.nextInt());
                student[count].getpref().setchatting(line.nextInt());
                count++;
            }

            for(int i=0; i< count; i++)
            {
                Student s1 = student[i];
                Student bestMatch=null;
                int bestScore=0;
                int currentScore;

                if (s1.getmatch())
                    continue;


                for (int j= i+1; j<100; j++)
                {
                    Student s2 = student[j];
                    currentScore=s1.compare(s2);

                    if (!s2.getmatch() && (currentScore>0) && (bestMatch == null || currentScore > bestScore))
                    {
                        bestMatch = s2;
                        bestScore=s1.compare(bestMatch);
                    }

                }

                if (bestMatch != null)
                {
                    System.out.println(s1.getname() + " matches with " + bestMatch.getname() + " with the score " + bestScore);
                    s1.setmatch(true);
                    bestMatch.setmatch(true);
                }
                else
                    System.out.println(s1.getname() + " has no matches.");
            }

        }

        catch (NoSuchElementException e)
        {
            System.out.println(e);
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

只需将Students.txt放在项目中,而不是在您的包裹下。因此,您的文件位置为:

.......\project2\Students.txt

答案 1 :(得分:0)

当您运行程序时,请在

中说
 /home/myaccount/workspace/project2

目录,你正在寻找一个文件sample.txt,java希望你在相对位置看它,所以它是

/home/myaccount/workspace/project2/sample.txt

如果你想在你的系统中提供一个特定的文件,你需要给它一个完整的路径,从你的根开始。

解决您的问题: 将您的文件移动到您的java类所在的目录,即:)