public static String io(){
InputStream is = null;
StringBuffer sb = new StringBuffer();
try {
is = new BufferedInputStream(new FileInputStream(FILE_PATH));
byte[] bytes = new byte[1024];
int b;
while((b=is.read(bytes))!=-1){
for (int i = 0; i < b; i++) {
sb.append((char)bytes[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(is!=null)
try {
is.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return sb.toString();
}
public static String nio(){
RandomAccessFile rf = null;
StringBuffer sb = new StringBuffer();
try {
rf = new RandomAccessFile(FILE_PATH, "rw");
FileChannel fc = rf.getChannel();
ByteBuffer bf = ByteBuffer.allocate(1024);
while(fc.read(bf) != -1){
bf.flip();
while (bf.hasRemaining()) {
sb.append((char)bf.get());
}
bf.compact();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(rf!=null)
try {
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
JAVA IO NIO
最新推荐文章于 2022-11-25 10:36:19 发布