码迷,mamicode.com
首页 > 其他好文 > 详细

Io流Reader

时间:2018-07-27 01:24:15      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:ted   todo   返回   rate   sys   ace   字符数组   字符串   null   

package reader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) {
    
        readFile();
        
    }
    public static void readFile() {
        FileReader fr = null;
        
        try {
            fr = new FileReader("Text.txt");
            //返回的是字符的ascll码值
//            int num  =fr.read();
//            System.out.println((char)num);
//            num  =fr.read();
//            System.out.println(num);
            //
//            while(num!=-1) {
//                num  =fr.read();
//                System.out.print((char)num);
//            }
            /**
             * 用这种方式
             */
            int num = 0;
            while((num = fr.read()) != -1) {
                System.out.print((char)num);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            if(fr!=null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-gene     rated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }
}
package reader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) {
    
        readFile();
        
    }
    public static void readFile() {
        FileReader fr = null;
        
        try {
            fr = new FileReader("Text.txt");
            //创建一个字符串的数组
            /*
            char[] chs = new char[5];
            //读取五个字符放入字符数组中
            int num = fr.read(chs);
            String str = new String(chs);
            System.out.println(num);
            System.out.println(chs);
            System.out.println("-----------------------");
            num = fr.read(chs);
            
                str = new String(chs,0,num);

            System.out.println(num);
            System.out.println(str);
            num = fr.read(chs);
            str = new String(chs,0,num);
            System.out.println(num);
            System.out.println(str);
            num = fr.read(chs);
            str = new String(chs,0,num);
            System.out.println(num);
            System.out.println(str);
            */
            
            char [] cha = new char[1024];
            int num1 = -1;
            while((num1 = fr.read(cha))!= -1)
            {
                System.out.println(new String(cha,0,num1));
            }
        
        
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            if(fr!=null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }
}

 

Io流Reader

标签:ted   todo   返回   rate   sys   ace   字符数组   字符串   null   

原文地址:https://www.cnblogs.com/java-jiangtao-home/p/9374953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!