当前位置:首页 > 开发 > 编程语言 > Java > 正文

java 无损读取文本文件

发表于: 2013-12-19   作者:hw1287789687   来源:转载   浏览:
摘要: java 如何无损读取文本文件呢? 以下是有损的 @Deprecated public static String getFullContent(File file, String charset) { BufferedReader reader = null; if (!file.exists()) { System.out.println("getFull

java 如何无损读取文本文件呢?

以下是有损

@Deprecated
	public static String getFullContent(File file, String charset) {
		BufferedReader reader = null;
		if (!file.exists()) {
			System.out.println("getFullContent: file(" + file.getAbsolutePath()
					+ ") does not exist.");
			return null;
		}
		if (charset == null) {
			charset = SystemHWUtil.CHARSET_ISO88591;
		}
		try {
			reader = getBufferReaderFromFile(file, charset);
			return getFullContent(reader);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

public static BufferedReader getBufferReaderFromFile(File file,
			String charset) throws FileNotFoundException {
		InputStream ss = new FileInputStream(file);
		InputStreamReader ireader;
		BufferedReader reader = null;
		try {
			if (charset == null) {
				ireader = new InputStreamReader(ss,
						SystemHWUtil.CHARSET_ISO88591);
			} else {
				ireader = new InputStreamReader(ss, charset);
			}
			reader = new BufferedReader(ireader);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return reader;
	}

/**
	 * have closed reader
	 * 
	 * @param reader
	 * @return
	 */
	@Deprecated
	public static String getFullContent(BufferedReader reader) {
		StringBuilder sb = new StringBuilder();
		String readedLine = null;
		try {
			while ((readedLine = reader.readLine()) != null) {
				sb.append(readedLine);
				sb.append(SystemHWUtil.CRLF);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String content = sb.toString();
		int length_CRLF = SystemHWUtil.CRLF.length();
		if (content.length() <= length_CRLF) {
			return content;
		}
		return content.substring(0, content.length() - length_CRLF);//
	}

 测试:

@Test
	public void test_getFullContent(){
		String filepath="D:\\bin\\config\\conf_passwd.properties";
		try {
			InputStream in =new FileInputStream(filepath);
			System.out.print(FileUtils.getFullContent(filepath, "UTF-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

介绍三种无损读取的方式

方式一:使用InputStreamReader,指定编码

/***
	 * 指定字符编码,无损地读取文本文件.
	 * 
	 * @param in
	 *            : 输入流,会关闭
	 * @param charset
	 *            : 字符编码
	 * @return
	 * @throws IOException
	 */
	public static String getFullContent3(InputStream in, String charset)
			throws IOException {
		StringBuffer sbuffer = new StringBuffer();
		InputStreamReader inReader;
		//设置字符编码
		inReader = new InputStreamReader(in, charset);
		char[] ch = new char[SystemHWUtil.BUFF_SIZE_1024];
		int readCount = 0;
		while ((readCount = inReader.read(ch)) != -1) {
			sbuffer.append(ch, 0, readCount);
		}
		inReader.close();
		in.close();
		return sbuffer.toString();
	}

 测试:

@Test
	public void test_getFullContent3(){
		String filepath="D:\\bin\\config\\conf_passwd.properties";
		try {
			InputStream in =new FileInputStream(filepath);
			System.out.print(FileUtils.getFullContent3(in, "UTF-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

 

方式二:先读取出字节数组,再使用String的构造方法

public static String getFullContent4(InputStream in, String charset) throws IOException{
		byte[]bytes=FileUtils.readBytes3(in);
		return new String(bytes,charset);
	}

/***
	 * Has been tested
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytes3(InputStream in) throws IOException {
		BufferedInputStream bufin = new BufferedInputStream(in);
		int buffSize = BUFFSIZE_1024;
		ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);

		// System.out.println("Available bytes:" + in.available());

		byte[] temp = new byte[buffSize];
		int size = 0;
		while ((size = bufin.read(temp)) != -1) {
			out.write(temp, 0, size);
		}
		bufin.close();
		in.close();
		byte[] content = out.toByteArray();
		out.flush();
		out.close();
		return content;
	}

 

 

方式三:使用System.arraycopy,所以效率不高,因为有拷贝操作(不推荐

public static String getFullContent2(InputStream in, String charset)
			throws IOException {
		int step = BUFFSIZE_1024;
		BufferedInputStream bis = new BufferedInputStream(in);

		// Data's byte array
		byte[] receData = new byte[step];

		// data length read from the stream
		int readLength = 0;

		// data Array offset
		int offset = 0;

		// Data array length
		int byteLength = step;

		while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {
			// Calculate the current length of the data
			offset += readLength;
			// Determine whether you need to copy data , when the remaining
			// space is less than step / 2, copy the data
			if (byteLength - offset <= step / 2) {
				byte[] tempData = new byte[receData.length + step];
				System.arraycopy(receData, 0, tempData, 0, offset);
				receData = tempData;
				byteLength = receData.length;
			}
		}

		return new String(receData, 0, offset, charset);
	}

 

总结:推荐使用方式一方式二

相关方法见附件中com.io.hw.file.util.FileUtils类

java 无损读取文本文件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文
Java中有几种类型的流? FileInputSream类 FileInputStream(String name) FileInputStream(File
最近遇到一个问题,Java读取文本文件(例如csv文件、txt文件等),遇到中文就变成乱码。读取代码如
代码如下: import java.io.*; public class ReadTxt { public static void execute() throws IOExc
要求:用java读取手机号码归属地文件 1、安徽联通.txt:内容如下 13003000000-13003009999-合肥 1300
1、java读取文本文件到mysql数据库【示例】:把手机号码归属地文件:安徽联通.txt读取到数据库schoo
1、java读取文本文件到mysql数据库【示例】:把手机号码归属地文件:安徽联通.txt读取到数据库schoo
1、需求:批量导入文件夹“手机号码归属地”下的所有文本文件,如图: 2、java源代码: package com
一,文本文件的内容。 二,工程目录 <p style="margin:10px auto; padding-top:0px; padding-bot
原文: c#分页读取GB文本文件 应用场景: a.我在做BI开发测试的时候,有可能面对source文件数GB的情况
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号