就是一个很简单的小程序,要读入txt中的文本。程序如下: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= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));
buf.clear();
}
inFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}现在的状况就是txt读取全部是乱码。。
“???????????獯潯潯潯潯潯潯潯潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪?????????潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????攪???????????”
我想问下大家怎么修改这个程序能让它正确读出文本?
谢谢!!!
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= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));
buf.clear();
}
inFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}现在的状况就是txt读取全部是乱码。。
“???????????獯潯潯潯潯潯潯潯潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪?????????潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????攪???????????”
我想问下大家怎么修改这个程序能让它正确读出文本?
谢谢!!!
不知道行不行。用文本编译器打开。之后改一下编码方式。改成utf-8再读试试。
我没试验。不知道行不行哈
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);
}
}
}
另外下面的转码有点乱,转成CharBuffer时会有字符补齐的情况,导致再转成
byte[]时会和文件中的编码不一样。是否可以试着用ByteBuffer.array()直接转成byte[] while(inChannel.read(buf) != -1) {
str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));
这个是按字符去读