java _io_文件字符流输入

字符流读取数据不会产生乱码问题
字节流读取数据可能会因为字符集不同,每个字符对应的字符大小不同
而产生乱码
/

Read read= new FileReader(File f);或路径

操作方法:
除了流的选择改变和字节数组变成了字符数组,其他都一样

public class test{
    public static void main(String[]args)
    {
        File f =new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc");
        Reader reader=null;
        try {
        reader=new FileReader(f);

        **char[] flush =new char[1024];**
        int len=-1;
        try {
            while((**len=reader.read(flush))!=-1**)
            {//字符数组-->字符串
                **String s=new String(flush,0,len);**
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }catch(IOException e)
    {
        e.printStackTrace();
    }finally {
        try {
            if(null!=reader)
            {
                reader.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }

    }

}

}

猜你喜欢

转载自blog.51cto.com/14437184/2423170