java中文字符读写
生活随笔
收集整理的這篇文章主要介紹了
java中文字符读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package qjt.chinesefile;
import java.io.*;
import java.util.*;
/*** 以行的方式讀中文文件內容件,不會亂碼* @author J. Qiu* @since 2009.07* */
public class ChineseFileReader {private boolean isEnd;private int count;private StringBuffer line;private char[] cbuf;private FileReader fr;private StringTokenizer depart;private boolean hasString;public ChineseFileReader(String file)throws IOException{File f=new File(file);init(f);}public ChineseFileReader(File file)throws IOException{init(file);}private void init(File f)throws IOException{isEnd=false;count=0;line=new StringBuffer();cbuf=new char[1024];hasString=false;fr=new FileReader(f);int len;char[] t;while((len=fr.read(cbuf,0,cbuf.length))>0){if (len==cbuf.length)line.append(String.valueOf(cbuf));else{t=new char[len];System.arraycopy(cbuf,0,t, 0,len);line.append(String.valueOf(t));}}
// System.out.println(String.valueOf(cbuf));}public String readLine()throws IOException{if (isEnd) return null;if(!hasString){depart=new StringTokenizer(line.toString(),"\n"); hasString=true;}if(count==depart.countTokens()){isEnd=true;return null;}return depart.nextToken();}public void close() throws IOException{if (fr!=null) fr.close();}
}
package qjt.chinesefile; import java.io.*; /*** 以行的方式寫中文內容到文件,不會亂碼* @author J. Qiu* @since 2009.07* */ public class ChineseFileWriter {private FileWriter f;private FileOutputStream fos=null;public ChineseFileWriter(String file)throws IOException{File f=new File(file);init(f);}public ChineseFileWriter(File file)throws IOException{init(file);}private void init(File f) throws IOException{fos=new FileOutputStream(f);}public void println(String line) throws IOException{line+='\n';byte[] str=line.getBytes();fos.write(str);}public void print(String line) throws IOException{byte[] str=line.getBytes();fos.write(str);}public void close()throws IOException{if(fos!=null)fos.close();} }
轉載于:https://www.cnblogs.com/virtualNatural/archive/2012/09/29/2707947.html
總結
以上是生活随笔為你收集整理的java中文字符读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: I can accept failure
- 下一篇: 利用Spring框架封装的JavaMai