【问题标题】:Issue reading and saving lines from .txt file in Java从 Java 中的 .txt 文件读取和保存行的问题
【发布时间】:2015-06-18 02:19:01
【问题描述】:

目标与问题

我要完成的任务的目标是从提供的路径中找到并读取文本文件,然后将文本文件的每一行复制到ArrayList<String> 中。这个“ApplicationFileReader”对象是通过调用接受路径的构造函数在某个控件类中创建的(它被发送到String,“TextFile.txt”[src 文件夹中的 FILLED 文本文件的名称]) .

但是,每当程序运行时,ArrayList<String> 在读取路径文件后仍然为空。该代码不会抛出IOException,这可能意味着它能够定位文件,只是可能无法实际将文本行提取到列表中。 (代码运行良好,只是没有输出)。这有什么原因吗?我是否应该放弃这种方法来使用this one 中的其他简单方法(尽管我仍然对这个问题感到好奇)。

提前致谢

代码说明

ApplicationFileReader 对象由 3 个实例组成:2 个 Strings 用于文件路径和文件的所有文本,ArrayList<String> 用于包含文本文件的每一行。默认构造函数将所有内容设置为空。参数构造函数(接受一个 String)通过验证路径和提取文本行来完成所有 3 个实例的设置。

我缺少什么吗?请发表评论。

代码:ApplicationFileReader

//TODO: Send exception to LOGGER to print to console

package model;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class ApplicationFileReader {

    public String filePathLocation;
    public String fileText;
    public ArrayList<String> fileTextLines;
    
    public ApplicationFileReader(){
        filePathLocation = "";
        fileText = "";
        fileTextLines = new ArrayList<String>();
    }
    
    public ApplicationFileReader(String filePathLocation){
        this.filePathLocation = filePathLocation;
        this.fileText = "";
        fileTextLines = new ArrayList<String>();
        extractFileText(filePathLocation);
        
        /* Testing */
        for(String s : fileTextLines)
            System.out.println(s);
    }

    public boolean extractFileText(String filePathLocation) {
        if(!validateFilePathLocation(filePathLocation))
            return false;
        try {
            Path path_filePath = Paths.get(filePathLocation);
            fileTextLines.addAll(Files.readAllLines(path_filePath, StandardCharsets.UTF_8));
            for(String eachListLine : fileTextLines){
                fileText += eachListLine;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    private boolean validateFilePathLocation(String filePathLocation) {
        try {
            File temporaryFile = new File(filePathLocation);
            if(!temporaryFile.exists() && temporaryFile.isDirectory())
                throw new IOException();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

【问题讨论】:

    标签: java arraylist io


    【解决方案1】:

    您根本没有在 extractTextFile() 方法中读取文件。

    Files.write()用于将Arraylist的内容写入文件。

    您可能想使用Files.readAllLines(path);

    【讨论】:

    • 认为这是问题所在。对API了解不多。我用fileTextLines.addAll(Files.readAllLines(path_filePath, StandardCharsets.UTF_8)); 替换了Files.write(. . .),但仍然认为是空的?
    • 是的,它确实包含一些测试行。
    • 好吧,在我摆弄了目录中的文件之后,这实际上起作用了。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2021-05-18
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多