java NIO 处理 txt文件
来源:互联网 发布:mac上新建文件夹 编辑:程序博客网 时间:2024/05/16 18:31
package com.i2f.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/**
* @author angelo
* @date 2011-12-19
*/
public class TxtParser {
private String[] fileTypes = { "txt" };
public String[] getFileTypes() {
return fileTypes;
}
public String readText(File file, String charset) throws Exception {
Map<String, String> parameter = new HashMap<String, String>();
FileChannel inChannel = null;
ByteBuffer bb = ByteBuffer.allocate(512);
Charset cs = Charset.forName(charset);
StringBuffer sb = new StringBuffer();
FileInputStream inFile = null;
CharBuffer cb = null;
try {
inFile = new FileInputStream(file);
inChannel = inFile.getChannel();
while (inChannel.read(bb) != -1) {
bb.flip();
cb = cs.decode(bb);
String[] strs = cb.toString().split("=");
parameter.put(strs[0], strs[1]);
sb.append(cb);
bb.clear();
}
} finally {
if (inFile != null)
try {
inFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main(String[] s) throws Exception {
long total = 0L;
for (int i = 0; i < 1; i++) {
long start = System.currentTimeMillis();
System.out.println(new TxtParser().readText(
new File("d:/test2.txt"), "utf-8"));
total += System.currentTimeMillis() - start;
}
System.out.print("耗时:" + total);
}
}
- java NIO 处理 txt文件
- java处理txt文件,并生成文件
- PHP 处理 TXT 文件
- C++处理txt文件
- python处理txt文件
- Java NIO内存映射---上G大文件处理
- Java NIO内存映射---上G大文件处理
- Java中用内存映射NIO处理大文件
- java 利用输入输出流处理txt文件,并生成新txt文件
- Java 读取本地 UTF8 txt文件乱码处理
- JAVA程序输出到.txt文件的换行符处理
- 批量处理txt文本文件到Excel文件中去----java
- Java处理txt文件遇到的问题 (unicode编码)
- 【Java.NIO】NIO就绪处理之OP_CONNECT
- 【Java.NIO】NIO就绪处理之OP_ACCEPT
- 【Java.NIO】NIO就绪处理之OP_WRITE
- java nio 文件锁小例
- java nio 文件锁小例
- Linux故障分析
- linux中安装yum工具
- DWR之其他概念:engine.js与util.js
- asp.net上传图片
- linux系统中的软件安装
- java NIO 处理 txt文件
- 监听短信数据库的变化,标记为read_already
- 实用Android开发工具和资源精选
- 黑马程序员_Java_I/O流学习(第二天)
- 字典:格式化字符串
- build qt-everywhere-opensource-src-4.8.0
- 贡献30本经典Linux学习和开发教程和资料,都是pdf完整版的
- Goldengate DDL复制相关注意事项
- 制作可执行的JAR文件包及jar命令详解zt