發文作者:marky0511 | 五月 26, 2007

簡單的 File Reader and Writer (with Character Encoding)

用 InputStream 與 OutputStream 來做 檔案的讀與寫 (包含檔案本身的文字編碼)

範例如下 ~


private static void readWriteFile(String absulotePathFileName,String newAbsulotePathFileName) throws IOException{

InputStream fileIn=new FileInputStream(absulotePathFileName);
Reader fileReader=
new InputStreamReader(fileIn,“utf-8″);
int i=-1;
StringBuffer txtContent=
new StringBuffer();
while((i=fileReader.read())!=-1){
txtContent.append((
char)i);
}
fileReader.close();
fileIn.close();

File file=new File(SysConf.get(newAbsulotePathFileName);
if(!file.exists()){
file.createNewFile();
}
OutputStream fileOut=
new FileOutputStream(file,true);
Writer fileWriter=
new OutputStreamWriter(fileOut,“utf-8″);
fileWriter.write(txtContent.toString());
fileWriter.close();
fileOut.close();

}


發表迴響

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

連結到 %s

分類

Follow

Get every new post delivered to your Inbox.