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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范龙彬 黑马帝   /  2011-11-6 10:10  /  2203 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


myBufferReader 类中没有 newLine() 换行方法  在主函数中也没有换行语句 .   myReadLine() 方法遇到 '\n''\r'  直接跳过,那么打印结果中的换行是怎么实现的...

该贴已经同步到 范龙彬的微博

评分

参与人数 1技术分 +1 收起 理由
杨玉揆 + 1

查看全部评分

5 个回复

倒序浏览
System.out.println 会帮忙自动换行的,可参照以下源码:
public void println(String x) {
synchronized (this) {
     print(x);
     newLine(); }
    }

评分

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

查看全部评分

回复 使用道具 举报
为什么下面这段代码打出的结果 是一行的
import java.io.*;
class MyBufferReaderTest1
{
        public static void main(String[] args) throws Exception
        {
                MyBufferReader bufr= new MyBufferReader(new FileReader("123.txt"));
                String ch =null;
                while((ch=bufr.readLine())!=null)
                {
                        System.out.print(ch);
                }

                bufr.close();

        }
}
class MyBufferReader
{
        private FileReader r;
        MyBufferReader(FileReader r)
        {
                this.r =r;
        }
        public String readLine()  throws Exception
        {
                StringBuilder sb =  new StringBuilder();
                int ch =0;
                while((ch=r.read())!=-1)
                {
                        if(ch=='\r')
                                continue;
                        if(ch=='\n')
                                return sb.toString();
                        else sb.append((char)ch);

                }
                if(sb.length()!=0)
                        return sb.toString();
                return null;

        }       
        void close() throws Exception
        {
                r.close();
        }
}

2.JPG (11.18 KB, 下载次数: 10)

123.txt文件

123.txt文件

1.JPG (8.8 KB, 下载次数: 10)

打印结果

打印结果
回复 使用道具 举报
明白了 .天天都在打的System.out.println();   跟newLin()混在一起怎么就蒙了呢. 郁闷  

评分

参与人数 1技术分 +1 收起 理由
admin + 1 继续努力!

查看全部评分

回复 使用道具 举报
楼主以后仔细点
回复 使用道具 举报
在判断到‘\r\n’过后该行就解析结束然后返回,这是确实没有换行信息。但你在主函数中读取一行然后调用的是println();方法所以它会换行!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马