开发者社区> 问答> 正文

Idea 使用FileInputStream读取桌面txt乱码:报错

Idea 使用FileInputStream读取桌面txt乱码 

同样的问题Eclipse读取没问题。

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {

        File file = new File("/Users/James/Desktop/hhh.txt");
        FileInputStream fis = new FileInputStream(file);
        byte[] arr = new byte[(int) file.length()];
        String text = new String(arr);
        System.out.println(text);
    }
}



展开
收起
kun坤 2020-06-07 16:20:43 794 0
1 条回答
写回答
取消 提交回答
  • 你内容都已经是英文的了,还怎么乱码?

    况且你这也没有读到文件内容啊!

    ######

    引用来自“海力布”的评论

    你内容都已经是英文的了,还怎么乱码?

    况且你这也没有读到文件内容啊!

    File file = new File("/Users/James/Desktop/haha.txt");
    FileInputStream fis = new FileInputStream(file); byte[] arr = new byte[(int) file.length()];
    fis.read(arr);
    String text = new String(arr);
    System.out.println(text);

    找到问题所在了。 我只是设置了byte长度。没往数组里读文件。  少了fis.read(arr)

    ######编码转码 idea默认utf eclipse不设置是跟系统走的######

    去试试groovy 一行代码搞定 还不需要ide。。。命令行就ok了


    2020-06-07 16:20:47
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
蚂蚁金服高级开发工程师萧恺:IDEA 插件开发入门教程 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载