#io#io写出文本不换行

使用Java的FileWriter写文件,需要换行时,假设已经新建FileWriter fr,

在Windows操作系统中,fr.write("\n"); 是不行的,需要 fr.write("\r\n"); 即回车换行

而在Linux系统中,只要 fr.write("\n");
可以 通过 System.getProperty(“line.separator”); 获得换行符号。

示例代码

 public void outPutCommonFile(String outPath,JavaClassFileMapper javaClassFileMapper) {
        String resourcePath = javaClassFileMapper.getPojoTemplatePath()+'/'+javaClassFileMapper.getPojoTemplateFile();
        String outFile = outPath+'/'+javaClassFileMapper.getPackage()+'/'+javaClassFileMapper.getJavaClassName()+javaClassFileMapper.getExt();
        try(
                InputStream is = new ClassPathResource(resourcePath).getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));


                PrintWriter out = new PrintWriter(outFile,"UTF-8");
                BufferedWriter bufferedWriter = new BufferedWriter(out);
                ){
            String line = null;
            String endl = System.getProperty("line.separator");
            while ((line=reader.readLine())!=null) {
                bufferedWriter
                        .append(line)
                        .append(endl);
            }

        }catch (Exception ex) {
            log.error("出现异常了 {}",ex.getMessage());
        }
    }

你可能感兴趣的:(java)