逐行读取。
首先获取文件的编码格式;
读取文件内容。
log4j-1.2.17.jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.0.jar
1. 获取文件的编码格式
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class TxtCommonMethods { private static final Logger log = LoggerFactory .getLogger(TxtCommonMethods.class); /** * @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) { log.error( "Get charset of '" + filePath + "' fail:" + e.getMessage(), e); } catch (IOException e) { log.error( "Get charset of '" + filePath + "' fail:" + e.getMessage(), e); } catch (Exception e) { log.error( "Get charset of '" + filePath + "' fail:" + e.getMessage(), e); } finally { TxtIOUtils.closeStream(bis, null); TxtIOUtils.closeStream(is, null); } return charset; } }
2. 读取文件内容
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ReadLoadTxtFileRunnable implements Runnable { private static final Logger log = LoggerFactory .getLogger(ReadLoadTxtFileRunnable.class); private String filePath; public ReadLoadTxtFileRunnable(String filePath) { this.filePath = filePath; } @Override public void run() { this.read(filePath); } /** * @Title: read * @Description: 读取txt文件内容 * @param filePath * 文件绝对路径 * @return List * @author * @date 2015年12月26日 */ private void read(String filePath) { log.info("Read Whole Grid Load txt file,filePath=" + filePath); filePath = null == filePath ? null : filePath.trim(); if (null == filePath || "".equals(filePath)) { log.error("The filePath is null."); return null; } InputStream is = null; Reader reader = null; BufferedReader bufRead = null; try { is = new FileInputStream(filePath); // 判断文件的编码格式 String charset = TxtCommonMethods.getFileCharset(filePath); log.info("The charset of '" + filePath + "' is:" + charset); reader = new InputStreamReader(is, charset); bufRead = new BufferedReader(reader); String line = null; String[] arrs = null; while ((line = bufRead.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException e) { log.error("Read file '" + filePath + "' fail:" + e.getMessage(), e); } catch (UnsupportedEncodingException e) { log.error("Read file '" + filePath + "' fail:" + e.getMessage(), e); } catch (IOException e) { log.error("Read file '" + filePath + "' fail:" + e.getMessage(), e); } catch (Exception e) { log.error("Read file '" + filePath + "' fail:" + e.getMessage(), e); } finally { TxtIOUtils.closeReader(bufRead); TxtIOUtils.closeReader(reader); TxtIOUtils.closeStream(is, null); } log.info("End of Read Whole Grid Load txt file"); } }
3.TxtIOUtils.java
public class TxtIOUtils { private static final Logger log = LoggerFactory.getLogger(TxtIOUtils.class); public static void closeStream(InputStream is, OutputStream out) { if (null != out) { try { out.close(); } catch (IOException e) { log.error("Close OutputStream fail:" + e.getMessage(), e); } out = null; } if (null != is) { try { is.close(); } catch (IOException e) { log.error("Close InputStream fail:" + e.getMessage(), e); } is = null; } } public static void closeReader(Reader reader) { if (null != reader) { try { reader.close(); } catch (IOException e) { log.error("Close Reader fail:" + e.getMessage(), e); } reader = null; } } public static void closeWriter(Writer writer) { if (null != writer) { try { writer.close(); } catch (IOException e) { log.error("Close Reader fail:" + e.getMessage(), e); } writer = null; } } public static void closeChannel(Channel c) { if (null != c) { try { c.close(); } catch (IOException e) { log.error("Close Channel fail:" + e.getMessage(), e); } c = null; } } }
相关推荐
主要介绍了Java基于IO流读取文件的方法,涉及Java文件流操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
java io读取文件的技术,最近用到了大数据量文件的读取,研究下
java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有)
java 读取PDF文件中的内容 java 读取PDF文件中的内容
文件流 读写操作,对java.io.*;中文件读写的一些相关信息
Java实现文件复制,File文件读取,写入,IO流的读取写入
java file,java读取txt文档,java操作txt文档,读取文档的内容
System.out.println("读取文件内容出错"); e.printStackTrace(); } } public static void main(String argv[]){ String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt"; // "res/"; readTxt...
用java对文件操作的一些代码(包括文件读取,移动,删除,复制......)
读取txt文件,统计“java”字符串出现的次数
javaIO简单的写入读取文件 资源源于不但搜索,自由源于不但努力
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOperation { public static void...
java io 创建,读取,写入,删除文件 用io流处理文件。
对于文本文件(.txt,.java,.c,.cpp),使用字符流处理 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理 2.数据的流向:输入流、输出流 3.流的角色:节点流、处理流 节点流:直接从数据...
读取student.txt文件内容,每行作为一条数据,创建对应Student对象。 1、计算各班平均成绩,输出其信息到控制台; 2、求出年纪总分最高和各班总分最高的同学,输出其信息到控制台; 3、按科目分别创建3个单科成绩表...
3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S
关键词: java io ...现在要读取一个TXT文件,文件内容如下: ID NAME SEX RESULT 1 Jack man 65 2 Marry female 80 3 Tom man 90 …… 如此等等,要求把性别为男的数据取出来另存道一个txt文件中
NULL 博文链接:https://citywu123.iteye.com/blog/731180
javaIO字节流读取文件方式总结共4页.pdf.zip
java调用io流,InputStreamReader,BufferReader读取文件