我创建了一个基本的记事本文本文件(例如,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 新手,所以我现在可以掌握的一个简单的解决方案(和往常一样的评论)将非常有帮助。谢谢!
最佳答案
如果该文件与您的 java 类不在同一文件夹中,则必须提供 java 完整路径才能查找该文件。确保您还键入了文件的扩展名,例如“.txt”。
关于java - 另一个java.io.FileNotFoundException(系统找不到指定的文件)线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367168/