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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

保存文件

  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 个回复

倒序浏览
呵呵,不好意思,我也不会,我是新手,希望有高手为你解答哦
回复 使用道具 举报
额  你的写入writer时用\r\n写入就换行啦、、在windows下要用\r\n换行。。
回复 使用道具 举报
没有问题 都能读出啊
回复 使用道具 举报
arno942 发表于 2013-1-21 21:09
没有问题 都能读出啊

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


没有\r时能全读出来,有\r我这里就读不出来啊
回复 使用道具 举报
经测试确实没有楼主说的问题啊  windows或者linux都可以的
回复 使用道具 举报
txl 中级黑马 2013-1-21 22:57:03
7#
同学,我也试过了,能读出来啊,没有一点问题,你在试试我的?
  1. import java.io.FileReader;
  2. import java.io.FileWriter;
  3. import java.io.IOException;


  4. public class Z  {
  5.        
  6.         public static void main(String[] args) {
  7.                 write();
  8.                 read();
  9.         }
  10.        
  11.         public static void write()
  12.         {

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

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

  65.         }
  66. }
复制代码
回复 使用道具 举报
黑马唐贤来 发表于 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, 下载次数: 38)

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