温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java _io_文件字符流输入

发布时间:2020-06-27 19:54:25 来源:网络 阅读:204 作者:wx5d21d5e6e5ab1 栏目:编程语言

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

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();
        }

    }

}

}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI