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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ò壞尛孩 中级黑马   /  2014-4-19 14:28  /  3828 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ò壞尛孩 于 2014-4-26 14:36 编辑
  1. package com.itheima;
  2. /**
  3. * 5、 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b.txt中的文件内容应为“abcd…………..”这样的顺序。
  4. * @author
  5. * @date 2014/04/19
  6. */
  7. import java.io.*;
  8. import java.util.*;
  9. class t2
  10. {
  11.         public static void main(String[] args)throws Exception
  12.         {
  13.                 FileReader file_source=null;
  14.                 FileWriter file_destination=null;
  15.                 try
  16.                 {
  17.                         file_source=new FileReader("D:/a.txt");
  18.                         file_destination=new FileWriter("D:/b.txt");
  19.                         char[] buf=new char[1024];
  20.                         int len=0;
  21.                         while((len=file_source.read(buf))!=-1)
  22.                         {        
  23. //                                Sort(buf);
  24.                                 Arrays.sort(buf);
  25.                                 System.out.println(buf);
  26.                                 
  27.                                 file_destination.write(buf,0,len);
  28.                                 file_destination.flush();
  29.                         }
  30.                 }
  31.                 catch(IOException e)
  32.                 {
  33.                         e.printStackTrace();
  34.                 }
  35.                 finally
  36.                 {
  37.                         try
  38.                         {
  39.                                 if(file_source!=null)
  40.                                         file_source.close();
  41.                         }
  42.                         catch(IOException e)
  43.                         {
  44.                                 e.printStackTrace();
  45.                         }
  46.                         try
  47.                         {
  48.                                 if(file_destination!=null)
  49.                                         file_destination.close();
  50.                         }
  51.                         catch(IOException e)
  52.                         {
  53.                                 e.printStackTrace();
  54.                         }
  55.                         
  56.                 }
  57.         }
  58. /*        public static void Swap(char a,char b)
  59.         {
  60.                 char ch=a;
  61.                 a=b;
  62.                 b=ch;
  63.         }
  64.         public static void Sort(char []s)
  65.         {
  66.                 int j,k,flag;
  67.                 flag=s.length;
  68.                 while(flag>0)
  69.                 {
  70.                         k=flag;
  71.                         flag=0;
  72.                         for(j=1;j<k;j++)
  73.                                 if()
  74.                                 {
  75.                                         Swap(s[j-1],s[j]);
  76.                                         flag=j;
  77.                                 }
  78.                 }
  79.         }*/
  80. }
复制代码
在D盘下建一个a.txt,存入bcdeadferwplkou,然后运行此代码,可以执行,但是为什么b.txt中没有数据!刷新了也关闭了!

评分

参与人数 1黑马币 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

11 个回复

倒序浏览
肯定是你的路径写的不对
file_source=new FileReader("D:\\a.txt");
                        file_destination=new FileWriter("D:\\b.txt");
输入输出 的目标 改了 你就应该可以了

点评

加油  发表于 2015-5-31 16:39
回复 使用道具 举报
你错在直接对buf数组进行排序了。
定义的buf数组长度为1024,但实际文件中字符数却没有那么多,其他元素均为0,故排在前面。最后读入b.txt文件时仅截取读到的len长度的字符,故b.txt文件中写入了buf数组中的前len个字符,全为0。当然看不到数据了。
  1.                 try
  2.                 {
  3.                         file_source=new FileReader("D:/a.txt");
  4.                         file_destination=new FileWriter("D:/b.txt");
  5.                         char[] buf = new char[1024];
  6.                         char[] order = null;
  7.                         int len = 0;
  8.                         while((len = file_source.read(buf))!= -1 )
  9.                         {                                               
  10.                                 order = new String( buf, 0, len ).toCharArray();
  11.                             Arrays.sort(order);
  12.                             file_destination.write(order);
  13.                             file_destination.flush();
  14.                         }
  15.                         
  16.                 }
复制代码

点评

师妹眼光真高  发表于 2014-4-19 19:55

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

回复 使用道具 举报
Monkey·D·Chas 发表于 2014-4-19 14:46
肯定是你的路径写的不对
file_source=new FileReader("D:\\a.txt");
                        file_destin ...

可以读取文件的,\\和  /一样!
回复 使用道具 举报
呆呆沙师妹 发表于 2014-4-19 15:05
你错在直接对buf数组进行排序了。
定义的buf数组长度为1024,但实际文件中字符数却没有那么多,其他元素均 ...

你说的对!赞一个!
用字节流 好像可以控制读取的和输入的字符长度!
回复 使用道具 举报
那这个1024态大了,应该改多大就可以了?
回复 使用道具 举报
byte[] buf = new byte[fis.available()];
回复 使用道具 举报
牛!!!!
回复 使用道具 举报
  这不是那个考试题么?
回复 使用道具 举报
怎么文件是你自己创建的,不是已知的吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马