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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值