62,620
社区成员
发帖
与我相关
我的任务
分享
while(inChannel.read(buf) != -1) {
str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));
package lesson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class c {
static String str;
static char[] charnum=new char[1024];
static int i=0;
public static void main(String[] args) {
File aFile = new File("D:/Java/example.txt");
FileInputStream inFile = null;
try {
inFile = new FileInputStream(aFile);
} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
while(inChannel.read(buf) != -1) {
str= new String(buf.array(), "gbk");
System.out.println(str);
buf.clear();
}
inFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
System.out.println(new String(str.getBytes(),"UTF-8"));
//字符集编码问题,换成系统默认的试试