關於部落格
換個方向思考反而能更真切的看出事物的真實與本質
  • 64977

    累積人氣

  • 13

    今日人氣

    0

    訂閱人氣

[JAVA][JSP] File Read/Write 中文亂碼


處理方法很簡單,反正萬變不離其宗,但是還是記一下以備不時之需。

JSP頁的編碼為UTF8:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

讀檔:
<%
String content = "";
File file = new File("test.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);
    while (dis.available() != 0) {
        content += dis.readLine()+"rn";
    }
    fis.close();
    bis.close();
    dis.close();
    content = new String(content.getBytes("8859_1"),"BIG5");
} catch (FileNotFoundException e) {
    out.println(e.toString());
} catch (IOException e) {
    out.println(e.toString());
}
%>

寫檔:
<%
String ccontent = request.getParameter("content");
FileOutputStream fos;
DataOutputStream dos;
try {
    File file= new File("test.txt");
    fos = new FileOutputStream(file);
    byte data[] = new String(content.getBytes("8859_1")).getBytes("BIG5");
    fos.write(data);
    fos.close();
} catch (IOException e) {
    out.println(e.toString());
}
%>
相簿設定
標籤設定
相簿狀態