java - 使用文件名作为 FileReader 的输入

标签 java filereader

如何让 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/

相关文章:

javascript - TinyMCE/文件读取器 : missing line breaks after reading text file

java - 如何将值插入到xml中?

java - Ivy - 在 IDE 中工作时包含依赖项

java - 尝试在独立模式下执行 Acceleo 模块时遇到问题

javascript - jquery如何获取文件扩展名

java - 在内存、处理器、时间方面,Java 中 BufferInputReader、LineNumberReader 和 Stream 之间最好的文件读取器是什么

java - getString() 中出现错误

java - MINIMAX算法如何从树底向上进行BFS?

Java,从文件中读取两种不同类型的变量并稍后将它们用作对象

javascript - Base64 图像到 imagecreatefromstring() 丢失数据