【问题标题】:Another java.io.FileNotFoundException (The system cannot find the file specified) thread另一个java.io.FileNotFoundException(系统找不到指定的文件)线程
【发布时间】:2016-04-01 23:07:24
【问题描述】:

我创建了一个基本的记事本文本文件(例如 text-file.txt),并尝试将此文件放在多个文件路径中以便我的代码检索,但我似乎无法让它工作。基本上,我想获取 text-file.txt 的内容并创建第二个文件,其中所有内容都大写。

这是我的代码:

package abc123;

import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class abc123
{
        public static void main (String [] args) throws IOException
        {
                Scanner in = new Scanner(System.in); 

            System.out.print("Please provide the name of your input file: ");
            String inFileName = in.nextLine();
            System.out.print("Please indicate what you'd like to name your output file: ");
            String outFileName = in.nextLine();

            FileReader reader = new FileReader(inFileName);
            PrintWriter writer = new PrintWriter(outFileName); 
            Scanner fileReader = new Scanner(reader); 

            while(fileReader.hasNext())
            {
                    String line = fileReader.nextLine();
                    line = line.toUpperCase();
                    writer.println(line);
            }

            fileReader.close();
            writer.close();

            System.out.println("The process is now complete. Please check your output file. Thank you.");
        }
}

我是 Java 新手,因此我现在可以掌握的简单解决方案(以及 cmets,一如既往)将非常有帮助。谢谢!

【问题讨论】:

  • 不知道你为什么用ios标记这个,但我假设你使用的是Windows。你有没有试过把你的文本文件放在C:\text-file.txt,然后在运行应用程序时输入C:\text-file.txt?您可能还想使用fileReader.hasNextLine() 而不是fileReader.hasNext()
  • 哈,是的,不知道“ios”是如何到达那里的。因此,我对 fileReader.hasNextLine() 进行了修改,并将我的文件放在 C:\ 并按照您的建议。现在我收到以下错误消息:“线程“主”java.io.FileNotFoundException 中的异常:C:\new-text-file.txt(访问被拒绝)”。有什么想法吗?
  • 实际上,我刚刚在 C;\ 中创建了一个文件夹,将我的文件放在那里,指定了新路径,现在程序可以运行了。耶!谢谢。

标签: java file text path


【解决方案1】:

如果文件与您的 java 类不在同一个文件夹中,您必须提供 java 完整路径才能找到该文件。确保您还输入了文件的扩展名,例如“.txt”。

【讨论】:

  • 谢谢!我包含了文件扩展名,但之前没有正确输入完整路径,现在它就像一个魅力。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多