zoukankan      html  css  js  c++  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();
    	}
    }
    

      

  • 相关阅读:
    java 事件监听机制组成
    关于父进程和子进程的关系(UAC 绕过思路)
    Fort.js – 时尚、现代的进度提示效果
    Hive学习之函数DDL和Show、Describe语句
    js完美的div拖拽实例代码
    SSH2框架实现注冊发短信验证码实例
    再看C#中的托付和事件
    RGB(FFFFFF)转255:255:255
    单一目的聚集操作
    智慧城市,在中国的北海边再画一个圈——大连“中国首届智慧城市协同创新峰会”请你带好笔
  • 原文地址:https://www.cnblogs.com/virtualNatural/p/2707947.html
Copyright © 2011-2022 走看看