黑马程序员技术交流社区
标题:
问题
[打印本页]
作者:
风云
时间:
2013-6-17 09:04
标题:
问题
package java2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
public class Test4 {
public static void main(String[] args){
File source = new File("源文件");
File purpose = new File("目的文件");
copy(source,purpose);
}
public static void copy(File source,File purpose){
ArrayList<Integer> lists = new ArrayList<Integer>();
Reader reader = null;
try {
reader = new FileReader(source);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Writer write = null;
try {
write = new FileWriter(purpose);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int num;
try {
while((num = reader.read())!=-1){
lists.add(num);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Integer cha:lists){
try {
write.write(cha);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
write.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
复制代码
怎么运行的结果不是按照有序的排列的呢
作者:
′х.|鰉❤耔
时间:
2013-6-17 09:10
我记得ArrayList存储进去就是无序的,你要自己进行排序,不过这样效率太低了,还有就是你的 ArrayList的<Integer>的列表=新ArrayList <Integer>的中();这样写不好~声明集合最好拿它的父类来声明,这样就算你还另外一种集合就比较简单点,最好拿List来声明你的集合~
作者:
First
时间:
2013-6-17 11:28
楼主,其实只要加一句就能排序了
Collections.sort(lists);
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args){
File source = new File("d:\\1.txt");
File purpose = new File("d:\\2.txt");
copy(source,purpose);
}
public static void copy(File source,File purpose){
ArrayList<Integer> lists = new ArrayList<Integer>();
Reader reader = null;
try {
reader = new FileReader(source);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Writer write = null;
try {
write = new FileWriter(purpose);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int num;
try {
while((num = reader.read())!=-1){
lists.add(num);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Collections.sort(lists);
for(Integer cha:lists){
try {
write.write(cha);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
write.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
复制代码
作者:
孙百鑫
时间:
2013-6-22 01:42
楼主您好~帖子长时间未作出回答,我已经将您的帖子改成已解决。如果有问题的话可以私密我哦~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2