zoukankan      html  css  js  c++  java
  • 字符流

    public static void demo1() throws FileNotFoundException, IOException {
        FileReader fr = new FileReader("xxx.txt");                        
        int x = fr.read();                                                
        System.out.println(x);                                            
        char c = (char)x;//读的是一个字符,所以也得转为字符                               
        System.out.println(c);                                            
        fr.close();                                                       
    }                                                                     
    private static void demo2() throws FileNotFoundException, IOException {
        FileReader fr = new FileReader("xxx.txt");                         
        int c;                                                             
                                                                           
        while((c = fr.read()) != -1) {    //通过项目默认的码表一次读取一个字符                
            System.out.print((char)c);                                     
        }                                                                  
                                                                           
        fr.close();                                                        
    }                                                                      
    public static void main(String[] args) throws IOException {     
        FileWriter fw = new FileWriter("yyy.txt");                  
        fw.write("大家好,基础班快接近尾声了,大家要努力,要坚持ddd!!!");                  
        fw.write(97);//输出字母"a"                                               
        fw.close();                                                 
    }
    //其实底层也是通过码表的作用,把字符变成字节再输出
    /*                                                                                           
     * 字符流拷贝纯字符文件                                                                                
     */                                                                                          
    public static void demo1() throws FileNotFoundException, IOException {                       
        FileReader fr = new FileReader("xxx.txt");                                               
        FileWriter fw = new FileWriter("zzz.txt");                                               
                                                                                                 
        int c;                                                                                   
        while((c = fr.read()) != -1) {                                                           
            fw.write(c);                                                                         
        }                                                                                        
                                                                                                 
        //Writer类中有一个2k的小缓冲区,如果不关流,就会将内容写到缓冲区里,关流会将缓冲区内容刷新,再关闭                                   
        fr.close();                                                                              
        fw.close();                                                                                 
    }                                                                                            

    ###21.04_IO(什么情况下使用字符流)

    * 字符流也可以拷贝文本文件, 但不推荐使用. 因为读取时会把字节转为字符, 写出时还要把字符转回字节.(因为中间有个转换动作,但是字节流就没有这个转换)

    * 程序需要读取一段文本, 或者需要写出一段文本的时候可以使用字符流

    * 读取的时候是按照字符的大小读取的,不会出现半个中文

    * 写出的时候可以直接将字符串写出,不用转换为字节数组

    ###21.05_IO(字符流是否可以拷贝非纯文本的文件)

    * 不可以拷贝非纯文本的文件

    * 因为在读的时候会将字节转换为字符,在转换过程中,可能找不到对应的字符,就会用?代替,写出的时候会将字符转换成字节写出去

    * 如果是?,直接写出,这样写出之后的文件就乱了,看不了了  

    private static void demo4() throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("xxx.txt")); 
        BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt")); 
                                                                           
        int c;                                                             
        while((c = br.read()) != -1) {                                     
            bw.write(c);                                                   
        }                                                                  
                                                                           
        br.close();                                                        
        bw.close();                                                        
    }                                                                      
    /**                                                                       
     * Reads a line of text.  A line is considered to be terminated by any one
     * of a line feed ('
    '), a carriage return ('
    '), or a carriage return  
     * followed immediately by a linefeed.                                    
     *                                                                        
     * @return     A String containing the contents of the line, not including
     *             any line-termination characters, or null if the end of the 
     *             stream has been reached                                    
     *                                                                        
     * @exception  IOException  If an I/O error occurs                        
     *                                                                        
     * @see java.nio.file.Files#readAllLines                                  
     */                                                                       
    public String readLine() throws IOException {                             
        return readLine(false);                                               
    }                                                                         
    public static void demo1() throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
        String line;                                                      
                                                                          
        while((line = br.readLine()) != null) {                           
            System.out.println(line);                                     
        }                                                                 
                                                                          
        br.close();                                                       
    }                                                                     
    /**                                                            
     * @param args                                                 
     * 带缓冲区的流中的特殊方法                                                
     * readLine()                                                  
     * newLine();                                                  
     *                                                             
     * newLine()与
    (换行,以及回车)的区别                                  
     *               newLine()是跨平台的方法:好波,也就是这个好咯                   
     *               
    只支持的是windows系统                            
     * @throws IOException                                         
     */                                                            
    private static void demo2() throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); 
        BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt")); 
                                                                           
        String line;                                                       
        while((line = br.readLine()) != null) {                            
            bw.write(line);                                                
            //bw.newLine();                            //写出回车换行符              
            //bw.write("
    ");                     //和上面表达的意思都一样          
        }                                                                  
                                                                           
        br.close();                                                        
        bw.close();                                                        
    }                                                                      
    public class LineNumberReader extends BufferedReader;  //主要是设置获取行号
    public static void main(String[] args) throws IOException {                
        LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt"));
                                                                               
        String line;                                                           
        lnr.setLineNumber(100);//从101行开始读取,如果不设置的话就从第一行开始读                     
        while((line = lnr.readLine()) != null) {                               
            System.out.println(lnr.getLineNumber() + ":" + line);//读取行号        
        }                                                                      
                                                                               
        lnr.close();                                                           
    }                                                                          

    afasfsafa

    asvasbn

  • 相关阅读:
    [转]MySql 5.7关键字和保留字-附表
    [转]超链接标签简单的几个样式属性
    layui table 前台数字格式保留两位小数,不足补0(mysql 数据库)
    [转]Object.keys()和for in的排序问题
    [转]对form:input标签中的数字进行格式化
    [转]bootstrap table 动态列数
    [转]bootstrap的table插件动态加载表头
    [转]【MyBatis】Decimal映射到实体类出现科学计数法问题
    [转]MySQL函数大全 及用法示例
    [转]mysql update case when和where之间的注意事项
  • 原文地址:https://www.cnblogs.com/ericguoxiaofeng/p/8093704.html
Copyright © 2011-2022 走看看