如何让 FileReader 使用文件名作为输入来读取文件,而不是直接将文件路径放入文件?所以而不是像
FileReader fr = new FileReader("C:file");
当我们调用 FileReader(filename) 时,我们将文件名作为参数放入。因此,如果我输入命令提示符:
Java FileReader input.txt
它将读取文本文件,而无需我放入 new FileReader("C:input.txt")。
最佳答案
当您在 main 方法中启动应用程序(例如 java FileReader input.txt
)时
public static void main(String[] args) {
//args[0] is input.txt
//but you still need the rest of the path e.g. C:\
FileReader fr = new FileReader("path_to_file_location" + args[0]);
关于java - 使用文件名作为 FileReader 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16996489/