想问下字符流在读取文件时,相对于字节流 有哪些优势啊?

解决方案 »

  1.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     *//**
     *
     * @author Administrator
     */
    import java.io.*;
    import java.util.Scanner;
    public class NewClass {
        public static void main(String args[]){
            //先创建一个文件,然后读取并显示
            //说明一下,我这里创建文件,使用的编码方式是Unicode Big Endian(UTF-16BE)
            //而不是默认的编码方式.目的是下面好介绍读取文件时如何解码
            //对于UTF-16BE编码方式,我输出是会先写两个字节:-2,-1
            //这是一种约定。记事本在读取文本文件时,如果首两字节是:-2,-1的话,就会用
            //UTF-16BE进行解码
            try {
                FileOutputStream fos = new FileOutputStream("1.txt");
                String s="";
                s+="一二三四五六七\r\n";
                s+="1234567890\r\n";
                s+="abcdefghjklmn";
                byte head[]={-2,-1};
                fos.write(head);
                fos.write(s.getBytes("UTF-16BE"));
                fos.close();
            }catch (IOException ex) {
                ex.printStackTrace();
            }
            //第一种读取方法
            //用Scanner读取。Scanner的功能相当强大,对字符数据的解析很方便
            try{
                FileInputStream fis=new FileInputStream("1.txt");
                byte head[]=new byte[2];
                //这里先将文件的前两个字节读掉,因为这两字节是-2-1,不是文本信息
                fis.read(head);
                Scanner scan=new Scanner(fis,"UTF-16BE");
                System.out.println("================第一种读取方法===============");
                while(scan.hasNextLine()){
                    System.out.println(scan.nextLine());
                }
                scan.close();
                System.out.println("===================读取结束==================");
            }catch(IOException e){
                e.printStackTrace();
            }
            //第二种方法
            //用FileInputStream将数据读入数组并解析成String
            //在解析的时候,可以选择解码方式,我这里使用的是默认解码
            try{
                //这里先将数据读入字节数组,由于文件不大,我就一次读入了
                byte b[]=new byte[100];
                FileInputStream fis=new FileInputStream("1.txt");
                int num=fis.read(b);
                fis.close();
                String s=new String(b,2,num,"UTF-16BE");
                System.out.println("================第二种读取方法===============");
                System.out.println(s);
                System.out.println("===================读取结束==================");
            }catch(IOException e){
                e.printStackTrace();
            }
            //第三种方法,用BufferedReader读取
            //这里用InputStreamReader作为BufferedReader的参数,是因为,我读取的文件不是默认的字符编码,而是Unicode编码
            //所以在解析时需要手动设定解码方式,而字符流中,FileReader没有这种功能,InputStreamReader有
            //另外,说明一下:InputStreamReader是字节流通向字符流的桥梁
            try{
                 BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt"),"UTF-16BE"));
                 System.out.println("================第三种读取方法===============");
                 while(true){
                     String line=br.readLine();
                     if(line==null){
                         break;
                     }
                     System.out.println(line);
                 }
                 System.out.println("===================读取结束==================");
                 br.close();
            }catch(IOException e){        }
        }
    }
      

  2.   

    额,忘记使用标记了,不太美观。重贴一下:/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     *//**
     *
     * @author Administrator
     */
    import java.io.*;
    import java.util.Scanner;
    public class NewClass {
        public static void main(String args[]){
            //先创建一个文件,然后读取并显示
            //说明一下,我这里创建文件,使用的编码方式是Unicode Big Endian(UTF-16BE)
            //而不是默认的编码方式.目的是下面好介绍读取文件时如何解码
            //对于UTF-16BE编码方式,我输出是会先写两个字节:-2,-1
            //这是一种约定。记事本在读取文本文件时,如果首两字节是:-2,-1的话,就会用
            //UTF-16BE进行解码
            try {
                FileOutputStream fos = new FileOutputStream("1.txt");
                String s="";
                s+="一二三四五六七\r\n";
                s+="1234567890\r\n";
                s+="abcdefghjklmn";
                byte head[]={-2,-1};
                fos.write(head);
                fos.write(s.getBytes("UTF-16BE"));
                fos.close();
            }catch (IOException ex) {
                ex.printStackTrace();
            }
            //第一种读取方法
            //用Scanner读取。Scanner的功能相当强大,对字符数据的解析很方便
            try{
                FileInputStream fis=new FileInputStream("1.txt");
                byte head[]=new byte[2];
                //这里先将文件的前两个字节读掉,因为这两字节是-2-1,不是文本信息
                fis.read(head);
                Scanner scan=new Scanner(fis,"UTF-16BE");
                System.out.println("================第一种读取方法===============");
                while(scan.hasNextLine()){
                    System.out.println(scan.nextLine());
                }
                scan.close();
                System.out.println("===================读取结束==================");
            }catch(IOException e){
                e.printStackTrace();
            }
            //第二种方法
            //用FileInputStream将数据读入数组并解析成String
            //在解析的时候,可以选择解码方式,我这里使用的是默认解码
            try{
                //这里先将数据读入字节数组,由于文件不大,我就一次读入了
                byte b[]=new byte[100];
                FileInputStream fis=new FileInputStream("1.txt");
                int num=fis.read(b);
                fis.close();
                String s=new String(b,2,num,"UTF-16BE");
                System.out.println("================第二种读取方法===============");
                System.out.println(s);
                System.out.println("===================读取结束==================");
            }catch(IOException e){
                e.printStackTrace();
            }
            //第三种方法,用BufferedReader读取
            //这里用InputStreamReader作为BufferedReader的参数,是因为,我读取的文件不是默认的字符编码,而是Unicode编码
            //所以在解析时需要手动设定解码方式,而字符流中,FileReader没有这种功能,InputStreamReader有
            //另外,说明一下:InputStreamReader是字节流通向字符流的桥梁
            try{
                 BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt"),"UTF-16BE"));
                 System.out.println("================第三种读取方法===============");
                 while(true){
                     String line=br.readLine();
                     if(line==null){
                         break;
                     }
                     System.out.println(line);
                 }
                 System.out.println("===================读取结束==================");
                 br.close();
            }catch(IOException e){        }
        }
    }方法是很多滴,但是,如果熟悉掌握一种,也就行了。其实我很久不用BufferedReader了。我一般用FileInputStream加上Scanner。
      

  3.   

    FileReader跟FileWriter一般用于读写txt文件,1、外面加上BufferedReader.就可以使用 BufferedReader里的public String readLine()throws IOException,方法。2、如果是每个字符的读,那就用字符流。因为1个字节=1/2个字符,而中文是双字节(就是一个字符);如果用字节流读,他会每次读半个字符,就会出现乱码。
    除了这2个没啥区别。