/** * 写文本内容到txt文件 * @param fileContent 文件内容 */ public void writeResultFile(String fileContent) { //文件存放目录 String fileDir=ConstDefineFILE_DIR; //文件名 String fileName =new SimpleDateFormat(yyyyMMddHHmmss)format(new Date())+txt; File resultFile = new File(fileDirfileName); BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { bufferedReader = new BufferedReader(new StringReader(fileContent)); bufferedWriter = new BufferedWriter(new FileWriter(resultFile)); char buf[] = new char[]; //字符缓沖区 int len; while ((len = bufferedReaderread(buf)) != ) { bufferedWriterwrite(buf len); } } catch (IOException e) { eprintStackTrace(); } finally { try { bufferedWriterflush(); bufferedReaderclose(); bufferedWriterclose(); } catch (IOException e) { eprintStackTrace(); } } } 上面这个方法就是把文本内容写到txt文件中在Window系统和Red Hat Linux 系统中都没有问题但是在AIX系统中写到txt文件后就会变成乱码 修改后的方法如下:(写文件的时候加上编码) /** * 写文本内容到txt文件 * @param fileContent 文件内容 */ public void writeResultFile(String fileContent) { //文件存放目录 String fileDir=ConstDefineFILE_DIR; //文件名 String fileName =new SimpleDateFormat(yyyyMMddHHmmsszzz)format(new Date())+txt; File resultFile = new File(fileDirfileName); BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(resultFile)GBK)); bufferedWriterwrite(fileContentfileContentlength()); } catch (IOException e) { eprintStackTrace(); } finally { try { bufferedWriterflush(); bufferedWriterclose(); } catch (IOException e) { eprintStackTrace(); } } } 修改后的方法在windowLinux和AIX系统中都没有发生乱码 如果读取文件的时候遇到乱码也可以加上编码: BufferedReader br = new BufferedReader(new InputStreamReader (new FileInputStream(new File(filePath))GBK)); |