java 无损读取文本文件
来源:互联网 发布:网络课程培训小结 编辑:程序博客网 时间:2024/05/01 13:50
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类
0 0
- java 无损读取文本文件
- Java读取文本文件
- Java 读取文本文件
- Java读取文本文件
- Java读取超大文本文件
- java 读取文本文件 注意
- Java读取文本文件内容
- java读取本地文本文件
- java读取文本文件数据
- Java读取超大文本文件
- java-读取文本文件
- java 读取文本文件
- java文件读取文本文件
- java读取文本文件内容
- java读取大文本文件
- java读取文本文件异常
- Java按行读取文本文件
- (原创)JAVA文本文件的读取
- Android系统内置应用更新或升级后被还原的原因
- 【SOCLAB】HUAWEI无线上网卡——拨号使用手册
- sqlite入门基础(一):sqlite3_open,sqlite3_exec,slite3_close
- .net 定时器的使用
- 采用VisualNet制作项目实际应用
- java 无损读取文本文件
- 计算机网络学习-网络层
- 求字符串的长度(用指针做形参)
- 百度地图常用功能开发JS1.4版
- Nginx源码剖析之内存池和内存管理
- dip,px,pt,sp 的区别
- ORCAD使用中常见问题汇集及答案
- 强制IE使用兼容模式
- Hadoop Streaming