Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
很难将文本文件读入java(Eclipse)_Java_Eclipse - Fatal编程技术网

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

很难将文本文件读入java(Eclipse),java,eclipse,Java,Eclipse,我很难将文本文件读入java。基本上,当程序告诉我“输入文件名”时,我会这样做。然而,它回来了 java.io.FileNotFoundException: Students.txt (The system cannot find the file specified) 我不知道它为什么这么做。我花了几个小时在网上寻找帮助和娜达。我添加了一个屏幕截图来显示我如何拥有我的文件。我不知道这是否有帮助 我试过添加import java.FileNotFoundException什么都没有 这是我的

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

java.io.FileNotFoundException: Students.txt (The system cannot find the file specified)
我不知道它为什么这么做。我花了几个小时在网上寻找帮助和娜达。我添加了一个屏幕截图来显示我如何拥有我的文件。我不知道这是否有帮助

我试过添加
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);
        }
    }
}
import java.util.*;
导入java.io.*;
导入java.util.Scanner;
公开课比赛
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入文件名:”);
字符串文件名=input.next();
学生[]学生=新生[100];
对于(int i=0;i<100;i++)
学生[我]=新学生();
尝试
{
Scanner kbd=新扫描仪(新文件读取器(文件名));
整数计数=0;
while(kbd.hasNextLine())
{
扫描仪行=新扫描仪(kbd.nextLine());
line.useDelimiter(“[\t-]”);
学生[count].setname(line.next());
student[count].setgender(line.next().charAt(0));
学生[count].getbirthDay().setmonth(行.nextInt());
学生[count].getbirthDay().setday(line.nextInt());
学生[count].getbirthDay().setyear(line.nextInt());
学生[count].getpref().setquietime(line.nextInt());
学生[count].getpref().setmusic(line.nextInt());
学生[count].getpref().setreading(line.nextInt());
学生[count].getpref().setchating(line.nextInt());
计数++;
}
for(int i=0;ibestScore))
{
最佳匹配=s2;
最佳得分=s1。比较(最佳匹配);
}
}
如果(最佳匹配!=null)
{
System.out.println(s1.getname()+”与“+bestMatch.getname()+”匹配,分数为“+bestScore”);
s1.设置匹配(真);
bestMatch.setmatch(true);
}
其他的
System.out.println(s1.getname()+“没有匹配项”);
}
}
捕获(无接触元素例外e)
{
系统输出打印ln(e);
}
catch(filenotfounde异常)
{
系统输出打印ln(e);
}
}
}

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

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

当您运行程序时,让我们说

 /home/myaccount/workspace/project2
目录,您正在查找一个文件sample.txt,java希望您在相对位置查找它,所以

/home/myaccount/workspace/project2/sample.txt
如果你想在你的SU系统中给一个特定的文件,你需要给它一个完整的路径,从你的根开始

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

尝试将文件移动到项目根目录(看起来它在
src
atm中)。感谢您的编辑。我很难格式化那个部分。Lol只需选择整个代码,然后下次单击
{}
图标;)当我将文件移动到{src}时,它会将其放入默认包中。我只是创建了一个不同的文件夹,看看这是否有帮助,但没有任何帮助。看起来该文件与程序工作目录不在同一目录中。尝试进入完整路径。该死,忘了你在使用windows,我在inux环境中玩我的游戏,但想法是一样的:)谢谢!你们是我的英雄!去他妈的超人,我抓到你了,伙计,我也想检查一下你的绿色标志,但我不能:(我爱你,伙计,没关系,祝你愉快:)。