如何将字段存储在文本文件中或映射中,然后将字段与Java中其他文本文件中的字段进行比较?

发布于 2025-01-19 02:14:20 字数 428 浏览 4 评论 0原文

Text file1
2348384#Test 123####3983#Data 22 .... 

等等,此File1具有许多字段,这些字段被“#”字符隔开,并且文件中有许多行。

Text file  2
23,809,Test 88, Dat 33

文件2的字段被逗号分开,并且有许多行。

我需要比较文件中的文件匹配中的字段2。在两个文件中,许多字段都是相同的,所以如果两者相同,我需要编写代码以匹配吗?

是否应该将所有字段存储在文件1中的字符串中[] []

说我要比较第4行,字段9,字符串[3]

,或者我应该将个人字段存储在字符串变量中。

如何在2个文本文件中比较字符串变量。爪哇?我应该将所有行存储在列表还是哈希图中的文件中?

谢谢。

Text file1
2348384#Test 123####3983#Data 22 .... 

etc this file1 has many fields that are separted by "#" character and there are many rows in the file.

Text file  2
23,809,Test 88, Dat 33

File 2 has fields separated by comma and its has many rows.

I need to compare if fields from file match in file2. Many fields are same in the 2 files, so i need to write code to match if both are same?

Should i store all fields in file 1 into a String[]

Say i want to compare Row 4 ,field 9 , String[3]

or should i store individuals fields in a String variable..

How can i compare fields in 2 text files in java? SHould i store all lines in a file in a List or HashMap?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2025-01-26 02:14:20

你的意思是这样的吗?抱歉,如果这是混乱的代码

public final static void main(final String[] args) {
    // file1 content:
    // a###b###c#d
    // e###f###g#h
    List<String> list1 = getListFromFile(new File("\\file1.txt"));

    // file2 content:
    // a,b,c,d
    // e,f,g,h
    final List<String> list2 = getListFromFile(new File("\\file2.txt"));

    list1 = format(list1);

    if (get(list1, 2, 3).equals(get(list2, 2, 4))) {
        System.out.println("Equal!");
    } else {
        System.out.println("Not Equal!");
    }

}


final static String get(final List<String> list, final int row, final int field) {

    final String[] strs = list.get(row - 1).split(",");

    /*
     * Handle IndexOutOfBoundsException
     */

    return strs[field - 1].trim();
}

final static List<String> format(final List<String> list1) {
    final List<String> newList = new ArrayList<>();

    for (int i = 0; i < list1.size(); i++) {
        newList.add(list1.get(i).replaceAll("[#]+", ","));
    }

    return newList;
}

final static List<String> getListFromFile(final File file) {
    List<String> listOfFile = new ArrayList<>();
    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {

        String line;
        while ((line = reader.readLine()) != null) {
            listOfFile.add(line);
        }

        return listOfFile;
    } catch (final IOException e) {
        /*
         * Catch it
         */
    }
    return null;
}

Do you mean something like this? Sorry if this is messy code

public final static void main(final String[] args) {
    // file1 content:
    // a###b###c#d
    // e###f###g#h
    List<String> list1 = getListFromFile(new File("\\file1.txt"));

    // file2 content:
    // a,b,c,d
    // e,f,g,h
    final List<String> list2 = getListFromFile(new File("\\file2.txt"));

    list1 = format(list1);

    if (get(list1, 2, 3).equals(get(list2, 2, 4))) {
        System.out.println("Equal!");
    } else {
        System.out.println("Not Equal!");
    }

}


final static String get(final List<String> list, final int row, final int field) {

    final String[] strs = list.get(row - 1).split(",");

    /*
     * Handle IndexOutOfBoundsException
     */

    return strs[field - 1].trim();
}

final static List<String> format(final List<String> list1) {
    final List<String> newList = new ArrayList<>();

    for (int i = 0; i < list1.size(); i++) {
        newList.add(list1.get(i).replaceAll("[#]+", ","));
    }

    return newList;
}

final static List<String> getListFromFile(final File file) {
    List<String> listOfFile = new ArrayList<>();
    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {

        String line;
        while ((line = reader.readLine()) != null) {
            listOfFile.add(line);
        }

        return listOfFile;
    } catch (final IOException e) {
        /*
         * Catch it
         */
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文