2019-01-15 IO流之 Write(字符流的读取)

public static void main(String[] args) {
        String path="F:/1.txt";
        FileReader fr=null;
        try {
            fr=new FileReader(path);
            //方式一:一次读取一个字符,读取之后向下移一位
            int temp1=fr.read();
            System.out.println((char)temp1);
            int temp2=fr.read();
            System.out.println((char)temp2);
            
            
            //方式二:循环读取 read读取到最后会返回-1
            /*while(true) {
                int i= fr.read();
                if(i==-1) {
                    return;
                }else {
                    System.out.println((char)i);
                }
            }*/
            
            
            //方式三:读入到字符数组
            /*char[] data=new char[1024];
            int temp=fr.read(data);
            System.out.println(new String(data,0,temp));*/
            
            //方式四:方式三的优化,当文件过大时需要循环读取
            /*char[] date=new char[1024];
            int temp=0;
            while((temp=fr.read(data))!=-1) {
                System.out.println(new String(data,0,temp));
            }*/
            
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            if(fr!=null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

你可能感兴趣的:(2019-01-15 IO流之 Write(字符流的读取))