我有两个 .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/