A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 风云 中级黑马   /  2013-6-17 09:04  /  1220 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package java2;

  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. import java.io.Writer;
  9. import java.util.ArrayList;

  10. public class Test4 {
  11.                 public static void main(String[] args){
  12.                         File source = new File("源文件");
  13.                         File purpose = new File("目的文件");
  14.                         copy(source,purpose);
  15.                 }
  16.                 public static void copy(File source,File purpose){
  17.                         ArrayList<Integer> lists = new ArrayList<Integer>();
  18.                         Reader reader = null;
  19.                         try {
  20.                                 reader = new FileReader(source);
  21.                         } catch (FileNotFoundException e) {
  22.                                 // TODO Auto-generated catch block
  23.                                 e.printStackTrace();
  24.                         }
  25.                         Writer write = null;
  26.                         try {
  27.                                 write = new FileWriter(purpose);
  28.                         } catch (IOException e) {
  29.                                 // TODO Auto-generated catch block
  30.                                 e.printStackTrace();
  31.                         }
  32.                                  int num;
  33.                                  try {
  34.                                         while((num = reader.read())!=-1){
  35.                                                  lists.add(num);
  36.                                          }
  37.                                 } catch (IOException e) {
  38.                                         // TODO Auto-generated catch block
  39.                                         e.printStackTrace();
  40.                                 }
  41.                                         for(Integer cha:lists){
  42.                                                 try {
  43.                                                         write.write(cha);
  44.                                                 } catch (IOException e) {
  45.                                                         // TODO Auto-generated catch block
  46.                                                         e.printStackTrace();
  47.                                                 }
  48.                                                 try {
  49.                                                         write.flush();
  50.                                                 } catch (IOException e) {
  51.                                                         // TODO Auto-generated catch block
  52.                                                         e.printStackTrace();
  53.                                                 }
  54.                                         }
  55.                                         try {
  56.                                                 reader.close();
  57.                                         } catch (IOException e) {
  58.                                                 // TODO Auto-generated catch block
  59.                                                 e.printStackTrace();
  60.                                         }
  61.                                         try {
  62.                                                 write.close();
  63.                                         } catch (IOException e) {
  64.                                                 // TODO Auto-generated catch block
  65.                                                 e.printStackTrace();
  66.                                         }
  67.                 }
  68. }
复制代码
怎么运行的结果不是按照有序的排列的呢

评分

参与人数 1技术分 +1 收起 理由
Super_Class + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览
我记得ArrayList存储进去就是无序的,你要自己进行排序,不过这样效率太低了,还有就是你的 ArrayList的<Integer>的列表=新ArrayList <Integer>的中();这样写不好~声明集合最好拿它的父类来声明,这样就算你还另外一种集合就比较简单点,最好拿List来声明你的集合~
回复 使用道具 举报 0 1
楼主,其实只要加一句就能排序了
Collections.sort(lists);
  1. package test;

  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. import java.io.Writer;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.List;

  12. public class Demo {
  13.                 public static void main(String[] args){
  14.                         File source = new File("d:\\1.txt");
  15.                         File purpose = new File("d:\\2.txt");
  16.                         copy(source,purpose);
  17.                 }
  18.                 public static void copy(File source,File purpose){
  19.                         ArrayList<Integer> lists = new ArrayList<Integer>();
  20.                         Reader reader = null;
  21.                         try {
  22.                                 reader = new FileReader(source);
  23.                         } catch (FileNotFoundException e) {
  24.                                 // TODO Auto-generated catch block
  25.                                 e.printStackTrace();
  26.                         }
  27.                         Writer write = null;
  28.                         try {
  29.                                 write = new FileWriter(purpose);
  30.                         } catch (IOException e) {
  31.                                 // TODO Auto-generated catch block
  32.                                 e.printStackTrace();
  33.                         }
  34.                                  int num;
  35.                                  try {
  36.                                         while((num = reader.read())!=-1){
  37.                                                  lists.add(num);
  38.                                          }
  39.                                 } catch (IOException e) {
  40.                                         // TODO Auto-generated catch block
  41.                                         e.printStackTrace();
  42.                                 }
  43.                                  Collections.sort(lists);
  44.                                         for(Integer cha:lists){
  45.                                                 try {
  46.                                                         write.write(cha);
  47.                                                 } catch (IOException e) {
  48.                                                         // TODO Auto-generated catch block
  49.                                                         e.printStackTrace();
  50.                                                 }
  51.                                                 try {
  52.                                                         write.flush();
  53.                                                 } catch (IOException e) {
  54.                                                         // TODO Auto-generated catch block
  55.                                                         e.printStackTrace();
  56.                                                 }
  57.                                         }
  58.                                         try {
  59.                                                 reader.close();
  60.                                         } catch (IOException e) {
  61.                                                 // TODO Auto-generated catch block
  62.                                                 e.printStackTrace();
  63.                                         }
  64.                                         try {
  65.                                                 write.close();
  66.                                         } catch (IOException e) {
  67.                                                 // TODO Auto-generated catch block
  68.                                                 e.printStackTrace();
  69.                                         }
  70.                 }
  71. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

回复 使用道具 举报
楼主您好~帖子长时间未作出回答,我已经将您的帖子改成已解决。如果有问题的话可以私密我哦~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马