Java 如何编辑我的比较方法
我想比较我的两个txt文件的内容,并在其他file3.txt文件中写入不同的单词 我想用这种方法编写另一个txt文件。我也不知道 有编码错误 我没有结果。这是我的代码我想你必须先刷新()你的书写器,然后再关闭它Java 如何编辑我的比较方法,java,arrays,string,compare,filewriter,Java,Arrays,String,Compare,Filewriter,我想比较我的两个txt文件的内容,并在其他file3.txt文件中写入不同的单词 我想用这种方法编写另一个txt文件。我也不知道 有编码错误 我没有结果。这是我的代码我想你必须先刷新()你的书写器,然后再关闭它 private static void write(ArrayList<String> out, String fname) throws IOException { FileWriter writer = new FileWriter(new File("D:\\D
private static void write(ArrayList<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));
for (int i = 0; i < out.size(); i++) {
writer.write(out.get(i) + "\n");
}
// Flush the writer before closing it.
writer.flush();
writer.close();
}
private static void write(ArrayList out,String fname)抛出IOException{
FileWriter writer=newfilewriter(新文件(“D:\\Denemeler\\deneme3.txt”);
对于(int i=0;i
我想你必须在关闭写入程序之前刷新它
private static void write(ArrayList<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));
for (int i = 0; i < out.size(); i++) {
writer.write(out.get(i) + "\n");
}
// Flush the writer before closing it.
writer.flush();
writer.close();
}
private static void write(ArrayList out,String fname)抛出IOException{
FileWriter writer=newfilewriter(新文件(“D:\\Denemeler\\deneme3.txt”);
对于(int i=0;i
我刚刚用以下文件运行了您的程序,无法重现您的问题
deneme1
abc
def
ghi
deneme2
abc
ghi
klm
并且deneme3是使用以下内容创建的:
abc
ghi
编辑
看来你想要的是相反的行为。您的一些方法不必要地复杂,可以通过使用标准JDK的正确工具来缩短。请参见下面的简化实现示例(仅保留两个文件之间不通用的单词)此示例区分大小写:
public class TextAreaSample {
public static void main(String[] args) throws IOException {
//readAllLines does what you do in readFileAsList
List<String> strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
List<String> strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
Set<String> notInCommon = getNotInCommon(strings1, strings2);
write(notInCommon, "C:\\temp\\deneme3.txt");
}
private static void write(Collection<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
for (String s : out) {
writer.write(s + "\n");
}
writer.close();
}
private static Set<String> getNotInCommon(List<String> strings1, List<String> strings2) {
//Sets are great to get unique lists and check commonality
Set<String> onlyInFile1 = new HashSet<String>(strings1);
onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
Set<String> onlyInFile2 = new HashSet<String>(strings2);
onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
Set<String> notInCommon = new HashSet<>();
notInCommon.addAll(onlyInFile1);
notInCommon.addAll(onlyInFile2);
return notInCommon;
}
}
公共类TextAreaSample{
公共静态void main(字符串[]args)引发IOException{
//readAllLines执行readFileAsList中的操作
List strings1=Files.readAllLines(path.get(“C:/temp/deneme1.txt”)、Charset.defaultCharset();
List strings2=Files.readAllLines(path.get(“C:\\temp\\deneme2.txt”)、Charset.defaultCharset();
设置notInCommon=getNotInCommon(strings1、strings2);
写入(notInCommon,“C:\\temp\\deneme3.txt”);
}
私有静态void write(Collection out,String fname)引发IOException{
FileWriter writer=newFileWriter(新文件(“C:\\temp\\deneme3.txt”);
for(字符串s:out){
writer.write(s+“\n”);
}
writer.close();
}
私有静态集getNotInCommon(列表字符串1、列表字符串2){
//集合对于获取唯一列表和检查公共性非常有用
Set onlyninfile1=新哈希集(strings1);
onlynfile1.removeAll(strings2);//删除s1和s2中的字符串
Set onlyninfile2=新哈希集(strings2);
onlynfile2.removeAll(strings1);//删除s1和s2中的字符串
Set notInCommon=new HashSet();
notInCommon.addAll(仅限文件1);
notInCommon.addAll(仅限文件2);
返回不常见;
}
}
我刚刚用以下文件运行了您的程序,无法重现您的问题
deneme1
abc
def
ghi
deneme2
abc
ghi
klm
并且deneme3是使用以下内容创建的:
abc
ghi
编辑
看来你想要的是相反的行为。您的一些方法不必要地复杂,可以通过使用标准JDK的正确工具来缩短。请参见下面的简化实现示例(仅保留两个文件之间不通用的单词)此示例区分大小写:
public class TextAreaSample {
public static void main(String[] args) throws IOException {
//readAllLines does what you do in readFileAsList
List<String> strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
List<String> strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
Set<String> notInCommon = getNotInCommon(strings1, strings2);
write(notInCommon, "C:\\temp\\deneme3.txt");
}
private static void write(Collection<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
for (String s : out) {
writer.write(s + "\n");
}
writer.close();
}
private static Set<String> getNotInCommon(List<String> strings1, List<String> strings2) {
//Sets are great to get unique lists and check commonality
Set<String> onlyInFile1 = new HashSet<String>(strings1);
onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
Set<String> onlyInFile2 = new HashSet<String>(strings2);
onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
Set<String> notInCommon = new HashSet<>();
notInCommon.addAll(onlyInFile1);
notInCommon.addAll(onlyInFile2);
return notInCommon;
}
}
公共类TextAreaSample{
公共静态void main(字符串[]args)引发IOException{
//readAllLines执行readFileAsList中的操作
List strings1=Files.readAllLines(path.get(“C:/temp/deneme1.txt”)、Charset.defaultCharset();
List strings2=Files.readAllLines(path.get(“C:\\temp\\deneme2.txt”)、Charset.defaultCharset();
设置notInCommon=getNotInCommon(strings1、strings2);
写入(notInCommon,“C:\\temp\\deneme3.txt”);
}
私有静态void write(Collection out,String fname)引发IOException{
FileWriter writer=newFileWriter(新文件(“C:\\temp\\deneme3.txt”);
for(字符串s:out){
writer.write(s+“\n”);
}
writer.close();
}
私有静态集getNotInCommon(列表字符串1、列表字符串2){
//集合对于获取唯一列表和检查公共性非常有用
Set onlyninfile1=新哈希集(strings1);
onlynfile1.removeAll(strings2);//删除s1和s2中的字符串
Set onlyninfile2=新哈希集(strings2);
onlynfile2.removeAll(strings1);//删除s1和s2中的字符串
Set notInCommon=new HashSet();
notInCommon.addAll(仅限文件1);
notInCommon.addAll(仅限文件2);
返回不常见;
}
}
我的建议是不要试图一次性解决所有问题。
您可以通过使用一条直线简化比较方法
strings1.保留(strings2)
有关更多信息,请参见此
)
打印字符串1的内容,看看是否可以,然后解决文件编写部分。我的建议是不要试图一次解决所有问题。 您可以通过使用一条直线简化比较方法
strings1.保留(strings2)
有关更多信息,请参见此
)
然后打印字符串1的内容,看看是否可以,然后解决文件写入部分。您正在打开第三个文件
deneme3.txt
两次,中间没有关闭它。我猜第二次(在write()
中)将抛出异常,因此不会进行写入。删除第一个出现的FileWriter writer=newfilewriter(新文件(“D:\\Denemeler\\deneme3.txt”)
(在compare()
中的那一个),您应该很好。您将在没有clos的情况下打开第三个文件deneme3.txt
两次