Java 读取文件内容

public static String txt2String(String filePath){
        File file = new File(filePath);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String s = null;
            //使用readLine方法,一次读取一行
            while((s = bufferedReader.readLine()) != null){
                if (stringBuilder.length() == 0){
                    //第一行不用换行
                    stringBuilder.append(s);
                }else {
                    stringBuilder.append(System.lineSeparator()+s);
                }
            }
            bufferedReader.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

你可能感兴趣的:(java,开发语言)