【问题标题】:get data read from file.data获取从 file.data 读取的数据
【发布时间】:2015-05-23 10:24:17
【问题描述】:

我不确定我需要做什么来比较这两个元素,我知道我需要读取数据文件并且我有一个类可以做到这一点。

/**
 * Helper methods to manage file content.
 */
public class FileUtilities {

    /**
     * Reads a comma separated file from the classpath.
     */
    public List<List<String>> readFileFromClasspath(String file)
            throws FileNotFoundException, IOException {
        InputStream is = getClass().getClassLoader().getResourceAsStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        return readFileFromBufferedReader(bufferedReader);
    }

    /**
     * Reads a comma separated file from the filesystem.
     */
    public List<List<String>> readFileFromFilesystem(String file)
            throws FileNotFoundException, IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        return readFileFromBufferedReader(bufferedReader);
    }

    /**
     * Reads a comma separacted file from a Reader.
     * <ul>
     * <li>Lines started with '#' are ignored.</li>
     * <li>Spaces before and after the comma are ignored.</li>
     * <li>Fields can be surrounded by quotes.
     */
    private List<List<String>> readFileFromBufferedReader(
            BufferedReader bufferedReader) throws FileNotFoundException,
            IOException {
        List<List<String>> fileRows = new ArrayList<List<String>>();
        String line = bufferedReader.readLine();
        while (line != null && line.length() > 0) {
            if (line.charAt(0) != '#') {
                List<String> rowValues = new ArrayList<String>();
                String[] tokens = line
                        .split(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
                for (String token : tokens) {
                    String processedToken = stripQutoes(token);
                    rowValues.add(processedToken);
                }
                fileRows.add(rowValues);
            }
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        return fileRows;
    }

    private String stripQutoes(String token) {
        String tokenWithoutSpaces = token.trim();
        if (tokenWithoutSpaces.length() > 0) {
            if (tokenWithoutSpaces.charAt(0) == '"'
                    && tokenWithoutSpaces
                            .charAt(tokenWithoutSpaces.length() - 1) == '"') {
                return tokenWithoutSpaces.substring(1,
                        tokenWithoutSpaces.length() - 1);
            }
        }
        return tokenWithoutSpaces;
    }
}

另外,当我读取数据文件时,我没有错误,所以我想我读取正确,此外,我已经使用 Eclipse Luna 4.4.0 实现了将 java 与数据库正确连接,并且PostgreSQL 9.4.

这是我拥有的主要课程的一部分:

public class Exercise1UpdateOrInsertDataFromFile {
    private FileUtilities fileUtilities;

    public Exercise1UpdateOrInsertDataFromFile() {
        super();
        fileUtilities = new FileUtilities();
    }

    public static void main(String[] args) {
        Exercise1UpdateOrInsertDataFromFile app = new Exercise1UpdateOrInsertDataFromFile();
        app.run();
    }

    private void run() {

        List<List<String>> fileContents = null;
        try {
            fileContents = fileUtilities.readFileFromClasspath("exercise1.data");
        } catch (FileNotFoundException e) {
            System.err.println("ERROR: File not found");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("ERROR: I/O error");
            e.printStackTrace();
        }
        if (fileContents == null) {
            return;
        }
        //at this point we asume that we read from file exercise1.data

        DBAccessor dbaccessor = new DBAccessor();
        dbaccessor.init();
        Connection conn = dbaccessor.getConnection(); 

编辑

我已经用另一个更准确和最简单的答案替换了我的答案。

我需要一个关于如何获取例如从数据文件中读取的第一行的示例,如果我知道如何获取数据,也许我会做下一部分。我想我应该以某种方式使用 fileUtilities 但不知道是什么。

这是file.data的内容:

# Doc_Nmber, Pat_Number, Visit_Date, Price
26902,6574405,30/03/2011,315
26507,6392432,14/03/2010,322
35356,6574405,15/02/2011,475
35252,9062865,07/07/2011,140

我需要更新的表具有相同的内容。

【问题讨论】:

  • 目前还不清楚您的练习应该做什么,以及这些文件和表格中的数据。也许你应该给出你练习的确切措辞。
  • 我添加文件内容。我无法发布所有练习,因为代码量很大。这是一个工作簿问题,所有类都是预定义的,我只对数据库进行了正确的连接,现在我需要如何管理读取的数据。
  • 基本上,每一行中通常有一些字段被认为是它的“键”。它们的每一种组合都是独一无二的。您使用这些键从数据库中检索数据。这是我可以给你的唯一提示,而无需了解练习实际上应该做什么。
  • 是的,我知道我需要使用主键来比较表和数据,但我不知道如何获取这些数据,例如读取第一行的正确指令是什么数据文件?
  • 反过来——你从文件中串行读取数据。您根据主键从数据库中读取它。

标签: java eclipse jdbc postgresql-9.4


【解决方案1】:

我认为我有解决方案,很简单:

fileContent.get(index)

我在System.out.println 中使用它并且它有效,现在我需要检查我是否可以使用它来与数据库表进行比较(这是我在编辑之前的问题),如果有人知道更好的方法或知道我'不知怎的,我错了,告诉我一些,如果不是,明天我会接受我的正确答案。

谢谢!

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多