看了一个代码是用 通道,字符集,缓冲的知识在屏幕上显示出一个文件内的文件
但是IO也可以用 read()把一个文件里的内容显示出来, 这两种显示方法有什么区别??
我目前知道的区别是 前者的读取速度要快,还有没其他的区别了?
感觉前者不容易记忆啊,什么时候用用它呢??两段代码如下
前者:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;public class BufferConverter { public static void main(String[] args) {
FileInputStream inData = null;
try {
inData = new FileInputStream("E:\\workspace1\\friends.dat"); } catch (FileNotFoundException e) { e.printStackTrace();
}
FileChannel inChannel = inData.getChannel();
try {
long inSize = inChannel.size();
ByteBuffer source = ByteBuffer.allocate((int) inSize);
inChannel.read(source, 0);
source.position(0);
System.out.println("Original byte data:");
for (int i = 0; source.remaining() > 0; i++) {
System.out.println(source.get() + " ");
source.position(0);
Charset ascii = Charset.forName("US-ASCII");
CharsetDecoder toAscii = ascii.newDecoder();
CharBuffer destination = toAscii.decode(source);
destination.position(0);
System.out.println("\n\nNew character data:");
for (i = 0; destination.remaining() > 0; i++) {
System.out.print(destination.get());
}
}
} catch (IOException e) { e.printStackTrace();
} }}
后者:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileReade {
public static void main(String[] args) {
int s ;
FileInputStream inData=null;
try {
inData = new FileInputStream("E:\\workspace1\\friends.dat");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((s=inData.read())!=-1){
System.out.print((char)s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
但是IO也可以用 read()把一个文件里的内容显示出来, 这两种显示方法有什么区别??
我目前知道的区别是 前者的读取速度要快,还有没其他的区别了?
感觉前者不容易记忆啊,什么时候用用它呢??两段代码如下
前者:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;public class BufferConverter { public static void main(String[] args) {
FileInputStream inData = null;
try {
inData = new FileInputStream("E:\\workspace1\\friends.dat"); } catch (FileNotFoundException e) { e.printStackTrace();
}
FileChannel inChannel = inData.getChannel();
try {
long inSize = inChannel.size();
ByteBuffer source = ByteBuffer.allocate((int) inSize);
inChannel.read(source, 0);
source.position(0);
System.out.println("Original byte data:");
for (int i = 0; source.remaining() > 0; i++) {
System.out.println(source.get() + " ");
source.position(0);
Charset ascii = Charset.forName("US-ASCII");
CharsetDecoder toAscii = ascii.newDecoder();
CharBuffer destination = toAscii.decode(source);
destination.position(0);
System.out.println("\n\nNew character data:");
for (i = 0; destination.remaining() > 0; i++) {
System.out.print(destination.get());
}
}
} catch (IOException e) { e.printStackTrace();
} }}
后者:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileReade {
public static void main(String[] args) {
int s ;
FileInputStream inData=null;
try {
inData = new FileInputStream("E:\\workspace1\\friends.dat");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((s=inData.read())!=-1){
System.out.print((char)s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货