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

这样保存的文件不能全都读出来,怎么解决

保存文件

  1.                 FileWriter fw=null;
  2.                 try
  3.                 {
  4.                         fw=new FileWriter("demo.txt",true);
  5.                         fw.write("nihao\rkjdkalkkl");
  6.                 }
  7.                 catch (IOException e)
  8.                 {
  9.                         System.out.println(e.toString());
  10.                 }
  11.                 finally{
  12.                         try
  13.                         {
  14.                                 if(fw!=null)
  15.                                 fw.close();
  16.                         }
  17.                         catch (IOException e)
  18.                         {
  19.                                 System.out.println(e.toString());
  20.                         }
  21.                          
  22.                 }
复制代码
读出文件
  1. FileReader fr=null;
  2.                 try
  3.                 {
  4.                         fr=new FileReader("demo.txt");

  5.                         char[] buf=new char[1024];
  6.                         int num=0;
  7.                         while((num=fr.read(buf))!=-1)
  8.                         {
  9.                                
  10.                                 System.out.println(new String(buf,0,num));
  11.                         }
  12.                 }
  13.                 catch (IOException e)
  14.                 {
  15.                 }
  16.                 finally{
  17.                         try
  18.                         {
  19.                                 if(fr!=null)
  20.                                         fr.close();
  21.                         }
  22.                         catch (IOException e)
  23.                         {
  24.                         }
  25.                
  26.                 }
  27.         }
复制代码
只能读出  kjdkalkkl
\r  前面的没有读出,孰能解释一下

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

7 个回复

倒序浏览
呵呵,不好意思,我也不会,我是新手,希望有高手为你解答哦
回复 使用道具 举报
没有问题 都能读出啊
回复 使用道具 举报
arno942 发表于 2013-1-21 21:09
没有问题 都能读出啊

存的时候是写的
fw.write("nihao\r kjdkalkkl");


没有\r时能全读出来,有\r我这里就读不出来啊
回复 使用道具 举报
经测试确实没有楼主说的问题啊  windows或者linux都可以的
回复 使用道具 举报
黑马唐贤来 发表于 2013-1-21 22:57
同学,我也试过了,能读出来啊,没有一点问题,你在试试我的?

图片是我打印出来的结果
下面是全部程序
  1. import java.io.*;
  2. class TestFile
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 writeFile();
  7.                 readFile();
  8.         }

  9.         public static void writeFile()
  10.         {
  11.                 FileWriter fw=null;
  12.                 try
  13.                 {
  14.                         fw=new FileWriter("demo.txt",true);
  15.                         fw.write("nihao\rkjdkalkkl");
  16.                 }
  17.                 catch (IOException e)
  18.                 {
  19.                         System.out.println(e.toString());
  20.                 }
  21.                 finally{
  22.                         try
  23.                         {
  24.                                 if(fw!=null)
  25.                                 fw.close();
  26.                         }
  27.                         catch (IOException e)
  28.                         {
  29.                                 System.out.println(e.toString());
  30.                         }
  31.                          
  32.                 }
  33.         }
  34.         public static void readFile()
  35.         {
  36.                 FileReader fr=null;
  37.                 try
  38.                 {
  39.                         fr=new FileReader("demo.txt");

  40.                         char[] buf=new char[1024];
  41.                         int num=0;
  42.                         while((num=fr.read(buf))!=-1)
  43.                         {
  44.                                
  45.                                 System.out.println(new String(buf,0,num));
  46.                         }
  47.                 }
  48.                 catch (IOException e)
  49.                 {
  50.                 }
  51.                 finally{
  52.                         try
  53.                         {
  54.                                 if(fr!=null)
  55.                                         fr.close();
  56.                         }
  57.                         catch (IOException e)
  58.                         {
  59.                         }
  60.                
  61.                 }
  62.         }
  63. }
复制代码

练习.jpg (8.38 KB, 下载次数: 66)

练习.jpg
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马