文件未在Java中打开

文件未在Java中打开,java,eclipse,inputstream,Java,Eclipse,Inputstream,我正在尝试读取一个包含内容的简单文本文件 input.txt Line 1 Line 2 Line 3 但它总是转到异常并打印错误 import java.io.*; import java.util.*; public class Main { public static void main(String args[]){ List<String> text = new ArrayList<String>(); try{

我正在尝试读取一个包含内容的简单文本文件 input.txt

Line 1
Line 2
Line 3
但它总是转到异常并打印错误

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String args[]){

        List<String> text = new ArrayList<String>();
        try{
            BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
            for (String line; (line = reader.readLine()) != null; ) {
                 text.add(line);
            }
            System.out.println(text.size()); //print how many lines read in
            reader.close();
        }catch(IOException e){
            System.out.println("ERROR");
        }
    }   
}
import java.io.*;
导入java.util.*;
公共班机{
公共静态void main(字符串参数[]){
列表文本=新的ArrayList();
试一试{
BufferedReader=新的BufferedReader(新文件读取器(“input.txt”);
for(字符串行;(line=reader.readLine())!=null;){
文本。添加(行);
}
System.out.println(text.size());//打印读入的行数
reader.close();
}捕获(IOE异常){
System.out.println(“错误”);
}
}   
}
我使用Eclipse作为我的IDE,如果这有什么不同的话。我已经试过这个密码了
它运行得很好,为什么不在Eclipse中运行呢?

给出完整的文件路径,如
“C:\\folder\u name\\input.txt”
或将input.txt放在Eclipse项目的src目录中。

给出完整的文件路径,如
“C:\\folder\u name\\input.txt”
或将input.txt放在Eclipse项目的src目录中。

公共类Main{
public class Main {
    public static void main(String args[]){

        List<String> text = new ArrayList<String>();
        try{
            BufferedReader reader = new BufferedReader(
                    new FileReader("input.txt"));  //<< your problem is probably here, 
            //More than likely you have to supply a path the input file.
            //Something like "C:\\mydir\\input.txt"
            for (String line; (line = reader.readLine()) != null; ) {
                 text.add(line);
            }
            System.out.println(text.size()); //print how many lines read in
            reader.close();
        }catch(IOException e){
            System.out.println("ERROR"); //This tells you nothing.
            System.out.println(e.getMessage());  //Do this
            //or 
            e.printStackTrace(); //this or both

        }
    }   
}
公共静态void main(字符串参数[]){ 列表文本=新的ArrayList(); 试一试{ BufferedReader reader=新的BufferedReader( 新的文件阅读器(“input.txt”);//
public类Main{
公共静态void main(字符串参数[]){
列表文本=新的ArrayList();
试一试{
BufferedReader reader=新的BufferedReader(

新FieleRADER(“输入.txt”);//您很可能有一个坏路径。
public class Main {
    public static void main(String args[]) throws Exception {

        List<String> text = new ArrayList<String>();

        BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
        for (String line; (line = reader.readLine()) != null; ) {
             text.add(line);
        }
        System.out.println(text.size()); //print how many lines read in
        reader.close();
    }   
}
公共类主{
公共静态void main(字符串args[])引发异常{
列表文本=新的ArrayList();
BufferedReader=新的BufferedReader(新文件读取器(“input.txt”);
for(字符串行;(line=reader.readLine())!=null;){
文本。添加(行);
}
System.out.println(text.size());//打印读入的行数
reader.close();
}   
}

“抛出异常”添加使您能够专注于代码,并考虑以后更好的错误处理。还考虑使用<代码>文件f=新文件(“输入.txt”)< /C> >,并使用它,因为它允许您打印<代码> F.GababyTurPATH()。它告诉你它实际上在寻找的文件名。

你很可能有一个坏的路径。考虑这个主要是:

public class Main {
    public static void main(String args[]) throws Exception {

        List<String> text = new ArrayList<String>();

        BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
        for (String line; (line = reader.readLine()) != null; ) {
             text.add(line);
        }
        System.out.println(text.size()); //print how many lines read in
        reader.close();
    }   
}
公共类主{
公共静态void main(字符串args[])引发异常{
列表文本=新的ArrayList();
BufferedReader=新的BufferedReader(新文件读取器(“input.txt”);
for(字符串行;(line=reader.readLine())!=null;){
文本。添加(行);
}
System.out.println(text.size());//打印读入的行数
reader.close();
}   
}

“抛出异常”添加使您能够专注于代码,并考虑以后更好的错误处理。还考虑使用<代码>文件f=新文件(“输入.txt”)< /C> >,并使用它,因为它允许您打印<代码> F.GababyTurPATH()。

它告诉您它实际要查找的文件名。

input.txt
更改为
src\\input.txt
解决了问题! 我猜这是因为当前目录实际上不是src文件夹,而是它的父目录


感谢您的帮助!

input.txt
更改为
src\\input.txt
解决了问题! 我猜这是因为当前目录实际上不是src文件夹,而是它的父目录



谢谢你的帮助!

试着打印出异常。然后你就会知道错误到底是什么。我猜
FileNotFoundException
-但是是的,你永远不应该忽略异常本身。同意。可能input.txt不在正确的文件夹中。如果你不理解其他人的评论,那么他们就打算使用
catch{e.printStackTrace()..}
如果没有找到完整的错误文件,请尝试打印出异常。然后你就会知道错误的实际内容。我猜
FileNotFoundException
-但是,是的,你永远不应该忽略异常本身。同意。可能input.txt不在正确的文件夹中。如果你不理解其他人的评论,那么他们就打算使用
catch{e.printStackTrace()..}
未找到完整错误文件,是否有方法可以引用当前目录?您正在执行的操作确实引用了当前目录,但我怀疑这是您的问题。在获得堆栈跟踪之前,您并不真正知道。是否有方法可以引用当前目录?您正在执行的操作确实引用了当前目录,但是我怀疑这就是你的问题。在你得到你不知道的堆栈跟踪之前。它已经在src文件夹中了,如果txt文件在src(类路径)中,我将尝试使用完整文件路径。如果txt文件在src(类路径)中,则不需要完整路径。尝试使用e.printStackTrace()打印出异常;它说系统找不到指定的文件完整路径有效,但我更希望有一种方法参考当前目录注意:使用
斜杠(/)
而不是
反斜杠(\\)
,因为平台不同。它已经在src文件夹内,如果txt文件在src内,我将尝试使用完整文件路径完整路径(classpath)。尝试使用e.printStackTrace()打印出异常;它表示系统找不到指定的文件完整路径正常,但我更希望有一种方法引用当前目录注意:使用
斜杠(/)
而不是
反斜杠(\\)
因为平台不同。您发布的代码无法编译。
public class Main抛出异常{
是错误的,可能您是说
public static void Main(String args[])抛出异常{
?您发布的代码无法编译。
public class Main抛出异常{
是错误的,可能您所说的是
公共静态void main(字符串args[])抛出异常{