PrintStream os=System.out;
File f = new File("F:/WorkSpace/IO/homework/测试文档2.txt");
RandomAccessFile af = new RandomAccessFile(f, "rw");
System.out.println("文件的大小:" + af.length());
System.out.println("当前文件的初始位置:" + af.getFilePointer());
System.out.println("读取文件的第一个字节:" + (char) af.read());
System.out.println("当前文件的位置:" + af.getFilePointer());
System.out.println("剩余文件类容:");
String len;
while ((len=af.readLine())!= null) {
os.write((len+"\r\n").getBytes("UTF-8"));
}
Chinese garbled how to solve ?
------ Solution ---------------------------------------- ----
computer txt file is usually of GBK ( ask for the systems ) ,
So you do not turn into utf8,
jvm start to have a default character set, you use that on the line.
------ Solution ---------------------------------------- ----
System.out.println (System.getProperty ("file.encoding"));
------ Solution -------------- ------------------------------
there may be a bom head, and you open that file notepad next to the code the utf8 without bom try. Previously encountered such a problem.
------ Solution ---------------------------------------- ----
because after such default GBK is garbled, change only the utf-8, the result was not. .
seemingly readLine method is indeed a problem . Read and write Chinese characters is not.
------ Solution -------------------------------------- ------
because after such default GBK is garbled, change only the utf-8, the result was not. .
seemingly readLine method is indeed a problem . Read and write Chinese characters is not.
readLine method seems to be a problem , I use read (byte [], off, len); would not garbled. . . Good magic is God horse ~ ~ ~
looked under the source
RandomAccessFile readLine when it is read by byte , then byte converted to char, java 's char is 2 byte, ie the characters before the high and low bytes of each make up 0 . So chaotic character
public final String readLine() throws IOException {
StringBuffer input = new StringBuffer();
int c = -1;
boolean eol = false;
while (!eol) {
switch (c = read()) {
case -1:
case '\n':
eol = true;
break;
case '\r':
eol = true;
long cur = getFilePointer();
if ((read()) != '\n') {
seek(cur);
}
break;
default:
input.append((char)c);
break;
}
}
if ((c == -1) && (input.length() == 0)) {
return null;
}
return input.toString();
}
------ Solution ------------------------------------- -------
because after such default GBK is garbled, change only the utf-8, the result was not. .
seemingly readLine method is indeed a problem . Read and write Chinese characters is not.
readLine method seems to be a problem , I use read (byte [], off, len); would not garbled. . . Good magic is God horse ~ ~ ~
looked under the source
RandomAccessFile readLine when it is read by byte , then byte converted to char, java 's char is 2 byte, ie the characters before the high and low bytes of each make up 0 . So chaotic character
public final String readLine() throws IOException {
StringBuffer input = new StringBuffer();
int c = -1;
boolean eol = false;
while (!eol) {
switch (c = read()) {
case -1:
case '\n':
eol = true;
break;
case '\r':
eol = true;
long cur = getFilePointer();
if ((read()) != '\n') {
seek(cur);
}
break;
default:
input.append((char)c);
break;
}
}
if ((c == -1) && (input.length() == 0)) {
return null;
}
return input.toString();
}
write a program to test , really the case.
public class TestRandomAccessFile {
public static void main(String[] args) throws Exception {
PrintStream os=System.out;
File f = new File("D:/test2.txt");
RandomAccessFile af = new RandomAccessFile(f, "rw");
System.out.println("文件的大小:" + af.length());
//System.out.println("当前文件的初始位置:" + af.getFilePointer());
//System.out.println("读取文件的第一个字节:" + (char) af.read());
//System.out.println("当前文件的位置:" + af.getFilePointer());
System.out.println("剩余文件类容:");
String len;
while ((len=af.readLine())!= null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int i=0; i< len.length(); i++){
char c = len.charAt(i);
baos.write(c);
}
os.println(baos.toString());
baos.flush();
baos.close();
}
}
}
------ Solution ------------------------------------- -------
write more easily understood
for(int i=0; i< len.length(); i++){
byte b = (byte)len.charAt(i);
baos.write(b);
}
------ For reference only ----------------------------------- ----
turn into GBK try
------ For reference only -------------------------- -------------
because after such default GBK is garbled, change only the utf-8, the result was not. .
------ For reference only -------------------------------------- -
I output the result is GBK, to how to change utf-8?
------ For reference only -------------------------------------- -
various encoding methods have been tried , or not , that is just not the same garbled mess ~ console and file encoding is GBK, GBK Chinese character encoding is not it ? How can not display Chinese ?
------ For reference only -------------------------------------- -
may be in the process of reading has been converted to byte .
------ For reference only -------------------------------------- -
because after such default GBK is garbled, change only the utf-8, the result was not. .
seemingly readLine method is indeed a problem . Read and write Chinese characters is not.
readLine method seems to be a problem , I use read (byte [], off, len); would not garbled. . . Good magic is God Ma ~ ~ ~
------ For reference only ------------------------------ ---------
Thank god , even to verify the RandomAccessFile the readLine method ! ! Unknown Jue Li ,
I used this method is also OK .
int len=(int) af.length();
byte[] b = new byte[len];
af.read(b, 0, len);
System.out.write(b);
没有评论:
发表评论