java.io 读取文件
Wednesday, 25. October 2006, 14:36:46
package javaexe.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
/**
* @author ruben
*/
public class Read {
public static void main(String[] args) {
File aFile = new File("D:/2.txt");
BufferedReader bReader = null;
FileInputStream inFile = null;
String s = null;
try{
inFile = new FileInputStream(aFile);
} catch(FileNotFoundException e) {}
try {
bReader = new BufferedReader(new InputStreamReader(inFile, "UTF-8"));
} catch(UnsupportedEncodingException e) {}
try {
while(true) {
s = bReader.readLine();
if(s == null) break;
System.out.println(s);
}
} catch(IOException e) {}
System.out.println("读取文件完成!");
System.exit(0);
}
}
/************** 另一种方法 *******************/
package javaexe.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
/**
*
* @author ruben
*/
public class Channel {
public static void main(String[] args) {
File aFile = new File("D:/2.txt");
FileInputStream inFile = null;
FileChannel inChannel = null;
ByteBuffer bb = ByteBuffer.allocateDirect(4096);
Charset cs = Charset.forName("UTF-8");
StringBuffer sb = new StringBuffer(1024);
try{
inFile = new FileInputStream(aFile);
} catch(FileNotFoundException e) {}
inChannel = inFile.getChannel();
try {
while(inChannel.read(bb) != -1) {
bb.flip();
CharBuffer cb = cs.decode(bb);
sb.append(cb.toString());
bb.clear();
}
inFile.close();
} catch(IOException e) {}
System.out.println(sb.toString());
System.out.println(sb.length());
System.out.println("读取文件完成!");
System.exit(0);
}
}
不知道怎么格式化,哎!也只有将就了!!
第一个是原始的io操作方式,感觉比较简单。
后一种是加入了通道概念的新io,能够自己操作缓冲大小,类型,并且可以得到文件大小后再决定创建怎样的缓冲,并且可以使用镜像文件,可以提供读写双向能力。
个人测试效率没有太大差异,也许是单文件没有压力的原因!在字符集转换的时候,刚开始不知道在nio中如何使用转换,后来才知道了Charset.decode()方法。还比较简单。
/**
* Sometimes , the only way to stay sane is go a little crazy .
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
/**
* @author ruben
*/
public class Read {
public static void main(String[] args) {
File aFile = new File("D:/2.txt");
BufferedReader bReader = null;
FileInputStream inFile = null;
String s = null;
try{
inFile = new FileInputStream(aFile);
} catch(FileNotFoundException e) {}
try {
bReader = new BufferedReader(new InputStreamReader(inFile, "UTF-8"));
} catch(UnsupportedEncodingException e) {}
try {
while(true) {
s = bReader.readLine();
if(s == null) break;
System.out.println(s);
}
} catch(IOException e) {}
System.out.println("读取文件完成!");
System.exit(0);
}
}
/************** 另一种方法 *******************/
package javaexe.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
/**
*
* @author ruben
*/
public class Channel {
public static void main(String[] args) {
File aFile = new File("D:/2.txt");
FileInputStream inFile = null;
FileChannel inChannel = null;
ByteBuffer bb = ByteBuffer.allocateDirect(4096);
Charset cs = Charset.forName("UTF-8");
StringBuffer sb = new StringBuffer(1024);
try{
inFile = new FileInputStream(aFile);
} catch(FileNotFoundException e) {}
inChannel = inFile.getChannel();
try {
while(inChannel.read(bb) != -1) {
bb.flip();
CharBuffer cb = cs.decode(bb);
sb.append(cb.toString());
bb.clear();
}
inFile.close();
} catch(IOException e) {}
System.out.println(sb.toString());
System.out.println(sb.length());
System.out.println("读取文件完成!");
System.exit(0);
}
}
不知道怎么格式化,哎!也只有将就了!!
第一个是原始的io操作方式,感觉比较简单。
后一种是加入了通道概念的新io,能够自己操作缓冲大小,类型,并且可以得到文件大小后再决定创建怎样的缓冲,并且可以使用镜像文件,可以提供读写双向能力。
个人测试效率没有太大差异,也许是单文件没有压力的原因!在字符集转换的时候,刚开始不知道在nio中如何使用转换,后来才知道了Charset.decode()方法。还比较简单。
/**
* Sometimes , the only way to stay sane is go a little crazy .
*/