Tag: filereader

Java FileReader无法查找文件

我决定开始一个新问题,因此它可以严格关注FileReader错误。 这是一种方法,它接收文件名和新文件的所需输出名称。 假设输入的文件名为“hello.txt”…该方法使其类似于“/home/User/hello.txt”,它作为参数进入FileReader。 问题是我把它作为输出“/home/User/hello.txt(没有这样的文件或目录)”,即使该文件确实存在且目录结构和权限是正确的。 我只能在.class和.java文件的本地目录中引用该文件来使用该方法,所以我用google搜索绝对指定并不是一个糟糕的选项。 任何输入都有帮助! public void fileGenerator(String in, String out) { try { String current_directory = System.getProperty(“user.dir”); Scanner input = new Scanner(new FileReader(current_directory+”/”+in)); PrintWriter output = new PrintWriter(current_directory+”/”+out); while(input.hasNext()) { String line = input.nextLine(); output.println(line); output.close(); } } catch (Exception e) { System.out.println(e.getMessage()); } } 这是请求的堆栈跟踪: java.io.FileNotFoundException: /home/User/hello.txt (No such file or directory) […]

如何使用Files.lines(…)。forEach(…)读取文件?

我目前正在尝试从我拥有的纯文本文件中读取行。 我发现另一个stackoverflow( 用Java读取纯文本文件 ),你可以使用Files.lines(..)。forEach(..)但是我实际上无法弄清楚如何使用for each函数来读取line by行文本,任何人都知道在哪里寻找或如何这样做?

逐行读取文件的最快方法是每行有2组字符串?

我可以逐行读取每行包含两个字符串的最快方法是什么。 示例输入文件将是: Fastest, Way To, Read One, File Line, By Line …. can be a large file 即使字符串之间有空格,每行也总是有两组字符串,例如“By Line” 目前我正在使用 FileReader a = new FileReader(file); BufferedReader br = new BufferedReader(a); String line; line = br.readLine(); long b = System.currentTimeMillis(); while(line != null){ 是否足够有效或使用标准JAVA API有更高效的方式(请不要外部库)任何帮助表示赞赏谢谢!

如何拆分文件中的字符串并读取它们?

我有一个包含信息的文件。 看起来像: Michael 19 180 Miami George 25 176 Washington William 43 188 Seattle 我想分割线条和字符串并阅读它们。 我希望它看起来像: Michael 19 180 Miami George … 我使用了这样的代码: BufferedReader in = null; String read; int linenum; try{ in = new BufferedReader(new FileReader(“fileeditor.txt”)); } catch (FileNotFoundException e) {System.out.println(“There was a problem: ” + e);} try{ for (linenum = 0; linenum<100; linenum++){ […]

通过Eclipse运行时,Java无法找到文件

当我运行应该从Eclipse中的文件读取的Java应用程序时,我得到一个java.io.FileNotFoundException ,即使该文件位于正确的目录中。 我可以从命令行编译并运行应用程序就好了; 问题只出现在Eclipse中,有多个项目和应用程序。 是否需要在运行配置或构建路径中更改设置以使其正确查找文件?

使用Java修改文件的内容

我想用java程序删除一些文件内容,如下所示。 这是要在同一文件中替换的write方法,还是应该将其复制到另一个文件中。 但它删除了文件的所有内容。 class FileReplace { ArrayList lines = new ArrayList(); String line = null; public void doIt() { try { File f1 = new File(“d:/new folder/t1.htm”); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); while (line = br.readLine() != null) { if (line.contains(“java”)) line = line.replace(“java”, ” “); lines.add(line); } FileWriter fw […]