java - 如何合并两个类(class)?

标签 java arrays string

我有两个 .txt 文件。首先,我将 file1.txt 的内容放入字符串数组中。之后,我使用另一个类对另一个文件执行相同的操作:file2.txt。然后,我将两个字符串数组的内容相互比较(特别是字符串数组中的单词)。如何将我的两个类相互组合?

最佳答案

欢迎来到SO,

混合 DataInputStream 和 BufferedReader 是没有意义的。一个更简单的模式是执行以下操作

can I compare these contents of two txt files?

public static void main(String... args) throws IOException {
    List<String> strings1 = readFileAsList("D:\\Denemeler\\file1.txt");
    List<String> strings2 = readFileAsList("D:\\Denemeler\\file2.txt");
    compare(strings1, strings2);
}

private static void compare(List<String> strings1, List<String> strings2) {
    // TODO
}

private static List<String> readFileAsList(String name) throws IOException {
    List<String> ret = new ArrayList<String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(name));
        String strLine;
        while ((strLine = br.readLine()) != null)
            ret.add(strLine);
        return ret;
    } finally {
        if (br != null) br.close();
    }
}

关于java - 如何合并两个类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791317/

相关文章:

java - Java 中的字符串实习是在编译时完成的吗?

java - 无法使用 RMI 远程方法

java - 使用标准输入的 While 循环

java - 如何通过网络浏览器使用分段上传到S3?

c - 数组名是指针吗?

java - 如何在 Java 中将字符串添加到字符串数组?

php 类返回数组

c++ - 从 std::string 转换为 const char* 导致 valgrind 出现 'Syscall param socketcall.sendto(msg) points to unaddressable byte(s)' 错误

java - 如何在java中分割具有重复字符的字符串

java - 一一访问JSP中的标签