zoukankan      html  css  js  c++  java
  • BufferedReader

    Reader

      FileReader

      BufferedReader

     1 package file;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.IOException;
     7 import java.util.Arrays;
     8 
     9 public class Demo7 {
    10     public static void main(String[] args) throws IOException {
    11         readTest1();
    12     }
    13     
    14     public static void readTest1() throws IOException {
    15         File file = new File("F:\2.txt");
    16         FileReader fileReader = new FileReader(file);
    17         //建立缓冲输入字符流
    18         BufferedReader bufferedReader = new BufferedReader(fileReader);
    19         //读取数据
    20 //        int content = bufferedReader.read();
    21 //        System.out.println((char)content);
    22         String line = null;
    23         while((line = bufferedReader.readLine()) != null) {    //一次读取一行,读到文件末尾null    ,每次读取的不包含
    的
    24             System.out.println(line);
    25 //            System.out.println(Arrays.toString(line.getBytes()));
    26 //            System.out.println(Arrays.toString("aa
    ".getBytes()));    //包含换行。
    27         }
    28         //关闭资源
    29         bufferedReader.close();
    30     }
    31 }

    自己实现readLine方法,默认的是每次读取一行,但不包括 。自己实现一个包括的

     1 package file;
     2 
     3 import java.io.File;
     4 import java.io.FileReader;
     5 import java.io.IOException;
     6 
     7 public class Demo8 {
     8     public static void main(String[] args) throws IOException {
     9         File file = new File("F:\2.txt");
    10         FileReader fileReader = new FileReader(file);
    11         String line = null;
    12         while(!(line = myReadLine(fileReader)).equals("")) {
    13             System.out.println(line);
    14         }
    15     }
    16     
    17     //自己实现ReadLine
    18     public static String myReadLine(FileReader fileReader) throws IOException {
    19         //创建一个字符串缓冲类对象
    20         StringBuilder sb = new StringBuilder();
    21         int content = 0;
    22         while((content = fileReader.read()) != -1) {
    23             if(content == '
    ') {
    24                 continue;
    25             }else if(content == '
    ') {
    26                 break;
    27             }else {
    28                 sb.append((char)content);
    29             }
    30         }
    31         
    32 //        if(content == -1 ){
    33 //            return null;
    34 //        }
    35         return sb.toString();    //如果没有了,返回的是"",不是null
    36     }
    37 }
  • 相关阅读:
    字符编码 乱码问题
    Django ORM那些相关操作
    pymysql模块使用---Python连接MySQL数据库
    数据库MySQL 之 索引原理与慢查询优化
    数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
    数据库 MySQL 之 数据操作
    数据库 MySQL 之 表操作、存储引擎
    [BZOJ 4212]神牛的养成计划(Trie+可持久化Trie)
    [LuoguP4094] [HEOI2016] [TJOI2016]字符串(二分答案+后缀数组+ST表+主席树)
    [BZOJ 2865]字符串识别(后缀数组+线段树)
  • 原文地址:https://www.cnblogs.com/linst/p/5661272.html
Copyright © 2011-2022 走看看