向Java输入要继续的目录

向Java输入要继续的目录,java,filereader,Java,Filereader,我这里有个问题。每当我输入从计算机复制的目录路径以将txt文件输入到程序时,它总是说找不到该文件。我的代码有问题吗 System.out.println("insert directory file = "); FileReader file = null; try { file = new FileReader(input.next()); BufferedReader readfile = new BufferedReader(file);

我这里有个问题。每当我输入从计算机复制的目录路径以将txt文件输入到程序时,它总是说找不到该文件。我的代码有问题吗

System.out.println("insert directory file = ");
FileReader file = null;
try {
    file = new FileReader(input.next());                
    BufferedReader readfile = new BufferedReader(file);              
    StringBuffer sb = new StringBuffer();
    try {
        while ((text = readfile.readLine()) != null) {
            sb.append(text);
            sb.append("\n");
        }
        readfile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    text = sb.toString();
    //System.out.println(text);
    System.out.println("Data entered");
    System.out.println("Data length = "+text.length()+"\n");
} catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
    System.out.println("File not found. Pease insert the proper file directory.\n");
}

您的代码段在我的笔记本电脑中运行良好。所以问题可能在这里:

file=newfilereader(input.next())

在读取路径之前,是否使用扫描仪进行其他输入?试着把它改成

String path = input.next();
file = new FileReader(path);
并在发生错误时打印路径,以查看实际传递给FileReader的内容

catch (FileNotFoundException e1) {
    System.out.println("File not found. Pease insert the proper file directory.\n");
    System.out.println("Your input path: " + path);
}
以下是我的机器上的工作代码:

public static void main(String[] args) {
  String path = null;
  try (Scanner input = new Scanner(System.in)) {
    System.out.print("Input your option = ");
    int option = input.nextInt();
    switch (option) {
      case 1:
        System.out.println("insert directory file = ");
        String text = "";
        path = input.next();
        FileReader fileReader = new FileReader(path);
        BufferedReader readfile = new BufferedReader(fileReader);
        StringBuffer sb = new StringBuffer();
        try {
          while ((text = readfile.readLine()) != null) {
            sb.append(text);
            sb.append("\n");
          }
          readfile.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        text = sb.toString();
        System.out.println("Data entered");
        System.out.println("Data length = " + text.length() + "\n");
        break;
      default:
        System.out.println("There is nothing to do.");
        break;
    }
  } catch (FileNotFoundException e1) {
    System.out.println("File not found. Pease insert the proper file directory.");
    System.out.println("Your input path is : " + path);
  }
}

可以通过将e1.printStackTrace()添加到捕获中来包含异常消息输出吗。这将包含无法找到的有关文件名的信息。您还应提供粘贴到控制台的路径。我猜有复制/粘贴错误我这样写D:\\Home\\data.txt但不起作用,我的代码已经正确了吗?我已经尝试过e1.printStackTrace,但在我进入我的pathd目录后程序停止了。你是如何进入路径目录的?也许是我根据你们的代码打错了,我猜你们是通过
扫描仪输入从控制台读取的。是吗?是的,我确实在菜单中使用了scanner,案例1是插入目录我使用scanner input=new scanner(System.in)我应该更改它吗?不需要更改它。当程序要求我输入目录时,我只需输入目录
D:\\Home\\data.txt
。我认为这很有可能是打字错误,因此我建议您在出错时打印路径。我无法理解您的意思。所以基本上我的代码都没问题?这里你要我打印路径?印什么样的