java 读取文件到String(解决中文乱码)

在改写V&View(维视)时用到了文件管理,需要从html文档读取字符串,可是一直出现中文乱码,一直解决不了.而且很是意外,我在本地运行代码时就能正常读取中文,当放到tomcat上时全是乱码,这也让我清醒的意识到了本地开发环境和在线调试环境一致的重要性了.我的tomcat没有设置字符串编码,默认是ISO-8859-1,而我的html是utf-8的,这就存在一个不一致的问题了。两种解决方法:

方法一,设置tomcat字符编码为utf-8,这种方法缺点很大,要是哪天重装了tomcat又忘了设置了,那就大大的bug了~所以我直接跳过,

方法二,代码中进行编码转换,我使用的是BufferedReader,中间加一个InputStreamReader进行编码转换,这下总不会乱码了吧!呵呵,上代码:

public static String readFileByLines(String fileName) {
        FileInputStream file = null;
        BufferedReader reader = null;
        InputStreamReader inputFileReader = null;
        String content = "";
        String tempString = null;
        try {
            file = new FileInputStream(fileName);
            inputFileReader = new InputStreamReader(file, "utf-8");
            reader = new BufferedReader(inputFileReader);
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                content += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return content;
    }