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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 尘埃落定 中级黑马   /  2014-5-31 16:34  /  1748 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码如下,
执行结果是:
sfhlluhlalnvlkjl
h orw ftewe

原文本内容是:
asdfghkl;lpuehgl;akldnfvnlakdjfl
the sorrows of the whe

为什么只读出了偶数位的字符串?


  1. import java.io.*;

  2. public class FileReaderDemo {

  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub
  5.                
  6.                 FileReader fr = null;
  7.                 try{
  8.                         fr = new FileReader("demo.txt");//新建一个文件读取流对象,关联要读取的文件名。
  9.                        
  10.                         while(fr.read()!=-1){
  11.                                 System.out.print((char)fr.read());
  12.                         }       
  13.                 }
  14.                 //read()方法读取一个字符,每次读一个,会接着上一个往下读,若达到末尾,则返回-1.
  15.                
  16.                 catch(Exception e){
  17.                         System.out.println(e.toString());       
  18.                 }
  19.                 finally{
  20.                         try{
  21.                                 if(fr!=null)
  22.                                         fr.close();
  23.                         }
  24.                         catch(Exception e){
  25.                                 System.out.println(e.toString());
  26.                         }
  27.                 }
  28.         }

  29. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
Silent_memory + 1 赞一个!

查看全部评分

8 个回复

倒序浏览
本帖最后由 饥渴ing 于 2014-5-31 17:02 编辑

你读了2次啊.
  • while(fr.read()!=-1){  //这是你第一读的但是你没保存它
  • System.out.print((char)fr.read()); //这是你在次读的.你输出了它
  • }
  • 代码应该改为
  • int t=0;
  • while((t=fr.read())!=-1){
  •    System.out.print((char)t);
  • }
回复 使用道具 举报
饥渴ing 发表于 2014-5-31 17:00
你读了2次啊.
  • while(fr.read()!=-1){  //这是你第一读的但是你没保存它

  • 按照你的方法改正后,结果没变,还是偶数位数的字符。:o
    回复 使用道具 举报
    尘埃落定 发表于 2014-6-1 11:52
    按照你的方法改正后,结果没变,还是偶数位数的字符。

    你确定改了并且没加其他的read方法?
    回复 使用道具 举报
    饥渴ing 发表于 2014-6-1 11:57
    你确定改了并且没加其他的read方法?

    是的,和你发给我的一样。
    你试运行下看看,结果发我。
    先谢谢了。
    回复 使用道具 举报
    1. import java.io.*;

    2. public class FileReaderDemo {

    3.         public static void main(String[] args) {
    4.                 // TODO Auto-generated method stub
    5.                
    6.                 FileReader fr = null;
    7.                 try{
    8.                         fr = new FileReader("demo.txt");//新建一个文件读取流对象,关联要读取的文件名。
    9.                         int num=0;
    10.                         while((num=fr.read())!=-1){
    11.                                 System.out.print((char)fr.read());
    12.                         }        
    13.                 }
    14.                 //read()方法读取一个字符,每次读一个,会接着上一个往下读,若达到末尾,则返回-1.
    15.                
    16.                 catch(Exception e){
    17.                         System.out.println(e.toString());        
    18.                 }
    19.                 finally{
    20.                         try{
    21.                                 if(fr!=null)
    22.                                         fr.close();
    23.                         }
    24.                         catch(Exception e){
    25.                                 System.out.println(e.toString());
    26.                         }
    27.                 }
    28.         }

    29. }
    复制代码


    回复 使用道具 举报
    学习学习。。。。
    回复 使用道具 举报
      

    12.                        while(fr.read()!=-1){

    13.                                System.out.print((char)fr.read());

    两遍read方法
    回复 使用道具 举报
    Ice丶wj 发表于 2014-6-2 09:29
    12.                        while(fr.read()!=-1){

    13.                                System.out ...

    恩,恩!!
    回复 使用道具 举报
    您需要登录后才可以回帖 登录 | 加入黑马