欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 7 nio逐行读取文件内容

程序员文章站 2022-03-02 15:27:42
...

nio逐行读取文件内容,使用 java 7.

首先,获取文件编码;

其次,读取文件内容。

 

1. 获取文件编码

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CommonsMethods {
	/**
	 * @Title: getFileCharset
	 * @Description: 判断文件的编码格式
	 * @param filePath
	 *            文件绝对路径
	 * @return String
	 * @author 
	 * @date 2015年12月26日
	 */
	public static String getFileCharset(String filePath) {

		File file = new File(filePath);

		if (!file.exists()) {
			System.out.println("File not found.");
		}
		// 默认编码格式为GBK
		String charset = "GBK";

		FileInputStream is = null;
		BufferedInputStream bis = null;

		try {
			byte[] first3Bytes = new byte[3];

			boolean checked = false;

			is = new FileInputStream(file);

			bis = new BufferedInputStream(is);

			bis.mark(0);

			int read = bis.read(first3Bytes, 0, 3);

			if (-1 == read) {
				charset = "GBK";
			} else if (first3Bytes[0] == (byte) 0xFF
					&& first3Bytes[1] == (byte) 0xFE) {
				charset = "UTF-16LE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE
					&& first3Bytes[1] == (byte) 0xFF) {
				charset = "UTF-16BE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF
					&& first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				charset = "UTF-8";
				checked = true;
			}
			bis.reset();

			if (!checked) {

				int loc = 0;

				while ((read = bis.read()) != -1) {

					loc++;

					if (read >= 0xF0) {
						break;
					}

					if (0x80 <= read && read <= 0xBF) {
						// 单独出现BF以下的,也算GBK
						break;
					}

					if (0x80 <= read && read <= 0xDF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							// GBK
							continue;
						} else {
							break;
						}
					} else if (0xE0 <= read && read <= 0xEF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							read = bis.read();
							if (0x80 <= read && read <= 0xBF) {
								charset = "UTF-8";
								break;
							} else {
								break;
							}
						} else {
							break;
						}
					}
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(bis, null);
			IOUtils.closeStream(is, null);
		}

		return charset;
	}
}

 

2. 逐行读取文件内容

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class NioReadFile {

	public static void main(String[] args) {

		String fileName = "D:\\CIME\\measure.CIME";

		String charset = CommonsMethods.getFileCharset(fileName);

		System.out.println("charset=" + charset);

		try {
			List<String> lines = Files.readAllLines(Paths.get(fileName),
					Charset.forName(charset));

			for (String line : lines) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

3. IOUtils

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.channels.Channel;

public class IOUtils {

	public static void closeStream(InputStream is, OutputStream out) {

		if (null != out) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
		}

		if (null != is) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			is = null;
		}
	}

	public static void closeReader(Reader reader) {

		if (null != reader) {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			reader = null;
		}
	}

	public static void closeWriter(Writer writer) {

		if (null != writer) {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			writer = null;
		}
	}

	public static void closeChannel(Channel c) {

		if (null != c) {
			try {
				c.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			c = null;
		}
	}
}

 

 

相关标签: nio