J2ME 技术的学习与实践者

[导入]字段输出流FieldOutputStream


网站: JavaEye  作者: iwinyeah  链接:http://iwinyeah.javaeye.com/blog/174644  发表时间: 2008年03月21日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

我的FieldOutputStream继承了DataOutputStream,这样就可以只更改很少量的代码就实现了既支持原生格式又支持文本方式输出了,稍候一段时间手机理财将可以实现备份和恢复(文本格式)功能了.
package util;
/**
 * --------------------------------------------------
 * 字段输出流
 * --------------------------------------------------
 * 从DataOutputStream继承
 * 主要增加了向输出流写入文本格式的数据字段的能力
 * 文本格式流将由TAB分隔字段,回车换行符分隔记录
 * --------------------------------------------------
 * 
 * @author iwinyeah 李永超
 * @version 1.0.0
 * */

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FieldOutputStream extends DataOutputStream {
	public final static int BIN_MODE = 0;

	public final static int TXT_MODE = 1;

	private final static byte[] fieldSplit = {'\t'};

	private final static byte[] recordSplit = {'\r','\n'};

	private int mode;
	
	private boolean nextEnd = false;

	public FieldOutputStream(OutputStream out, int mode) {
		super(out);
		this.mode = mode;
	}

	// 接着写入的是否最后一个字段
	// 写第一个字段前以参数false调用它
	// 写最后一个字段前以参数false调用它
	public void setNextEnd(boolean end){
		nextEnd = end;
	}
	
	public void putBoolean(boolean value) throws IOException {
		if (mode == 0) {
			writeBoolean(value);
		} else {
			out.write(value ? '1' : '0');
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

	public void putByte(byte value) throws IOException {
		if (mode == 0) {
			writeByte(value);
		} else {
			out.write(String.valueOf(value).getBytes("UTF-8"));
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

	public void putShort(short value) throws IOException {
		if (mode == 0) {
			writeShort(value);
		} else {
			out.write(String.valueOf(value).getBytes("UTF-8"));
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

	public void putInt(int value) throws IOException {
		if (mode == 0) {
			writeInt(value);
		} else {
			out.write(String.valueOf(value).getBytes("UTF-8"));
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

	public void putLong(long value) throws IOException {
		if (mode == 0) {
			writeLong(value);
		} else {
			out.write(String.valueOf(value).getBytes("UTF-8"));
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

	public void putString(String value) throws IOException {
		if (mode == 0) {
			if (value == null) {
				writeByte(0);
			} else {
				writeByte(1);
				writeUTF(value);
			}
		} else {
			if(value != null){
				byte[] b = value.getBytes("UTF-8");
				for(int i = 0; i < b.length; i++){
					if(b[i] == '\n'){
						out.write('\\');
						out.write('n');
					}
					else if(b[i] == '\r'){
						out.write('\\');
						out.write('r');
					}
					else if(b[i] == '\t'){
						out.write('\\');
						out.write('t');}
					else if(b[i] == '\b'){
						out.write('\\');
						out.write('b');}
					else if(b[i] == '\f'){
						out.write('\\');
						out.write('f');
					}else{
						out.write(b[i]);
					}
				}				
			}
			out.write(nextEnd ? recordSplit : fieldSplit);
		}
	}

}


读回请参看另一篇:字段输入流FieldInputStream.http://iwinyeah.javaeye.com/admin/blogs/174645
本文的讨论也很精彩,浏览讨论>>


JavaEye推荐




文章来源:http://iwinyeah.javaeye.com/blog/174644

posted on 2008-03-21 22:16 iwinyeah 阅读(201) 评论(0)  编辑  收藏 所属分类: Java 基础知识


只有注册用户登录后才能发表评论。


网站导航: