java读取文件封装的一个类(有部分代码借鉴别人的)

package modbus.rtu.calc;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class JavaIO {
	/**
	 * 将文本文件中的内容读入到buffer中
	 * @param buffer
	 * @param filePath文件路径
	 * @throws IOException
	 */
	public static void readToBuffer(StringBuffer buffer, String filePath)
			throws IOException {
		InputStream is = new FileInputStream(filePath);
		String line; // 用来保存每行读取的内容
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		line = reader.readLine(); // 读取第一行
		while (line != null) {
			buffer.append(line); // 将读到的内容添加到 buffer 中
			buffer.append("\n");
			line = reader.readLine(); // 读取下一行
		}
		reader.close();
		is.close();
	}
	/**
	 * 读取文本文件内容
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static String[] readFile(String filePath) throws IOException {
		StringBuffer sb = new StringBuffer();
		JavaIO.readToBuffer(sb, filePath);
		String[] strings = sb.toString().split("\n");
		return strings;
	}
	/**
	 * 将科学计数法转换成普通数字,并以数组的形式输出
	 * @param strings
	 * @return
	 */
	public static Double[] pointNumber(String[] strings) {
		Double number[] = new Double[strings.length];
		for (int i = 0; i < strings.length; i++) {
			BigDecimal db = new BigDecimal(strings[i]);
			Double s = Double.parseDouble(db.toPlainString());
			number[i] = s;
		}
		return number;
	}

	/**主程序
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

但是有的时候,很小的数字显示出来的时候还是很小,不知道是java运行机制的问题还是自己代码的问题,有高手知道还望指点一下!






posted @ 2015-01-05 21:50  tomi_mint  阅读(425)  评论(0编辑  收藏  举报