int [] arr ={89,90,77,87,66,54,328,890,99};
int [] arr1 = {65,72,12,77,2,96,54,27,89};
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\a.txt"));
for (int i = 0; i < arr.length; i++) {
if(i==arr.length-1){
bw.write(arr[i]+"");
}else{
bw.write(arr[i]+",");
}
}
bw.newLine();
for (int i = 0; i < arr1.length; i++) {
if(i==arr1.length-1){
bw.write(arr1[i]+"");
}else{
bw.write(arr1[i]+",");
}
}
bw.close();
//编写程序读取此文件中的所有数字,并将重复的数字只保留一个写入另一个文件
//1读取文件
BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));
BufferedWriter bw1 = new BufferedWriter(new FileWriter("d:\\b.txt"));
TreeSet<Integer> ts = new TreeSet<>(); //除重
String s;
while ((s=br.readLine())!=null) {
String[] arr2=s.split(","); //变成字符串数组
int[] arr3 = new int[arr2.length];
for (int i = 0; i < arr3.length; i++) {
arr3[i]=Integer.parseInt(arr2[i]);//变成数字数组
ts.add(arr3[i]);
}
}
System.out.println(ts);
for (Integer in : ts) {
bw1.write(in+" ");
}
br.close();
bw1.close(); |