小问题>?????
我想用以下程序读取e;/jf/wdp1 路径下的文本文件:file.txt 数据
在file.txt 里存有数据:weidongping hello
但读出来后用System.out.println("String read: "+((ByteBuffer) bytBuff.flip ()).asCharBuffer().toString());显示出来时却是一些看不懂的字.
请问这是为什么???
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class TryRead
{
public static void main(String[] args)
{
File aFile=new File("e:/jf/wdp1/file.txt");
FileInputStream inFile=null;
try{
inFile=new FileInputStream(aFile);
}catch(IOException e){System.out.println("Usage:a exception catched");System.exit(0);}
FileChannel inChannel=inFile.getChannel();
ByteBuffer bytBuff=ByteBuffer.allocate(48);
try{
while(inChannel.read(bytBuff)!=-1)
{
System.out.println("String read: "+((ByteBuffer) bytBuff.flip ()).asCharBuffer().toString());
bytBuff.clear();
}
System.out.println("EOF readed");
inFile.close();
}catch(IOException e){e.printStackTrace(System.err);System.exit(1); }
}
}