熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

寫txt文件在AIX5.3系統中亂碼

2022-06-13   來源: Java核心技術 

  /**

  * 寫文本內容到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));


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25687.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.