标题: 文件写入时的乱码问题 [打印本页] 作者: 刘文飞 时间: 2012-10-27 10:51 标题: 文件写入时的乱码问题 import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo01{
public static void main(String args[])throws Exception{
File f = new File("i:"+File.separator+"code"+File.separator+"java"+File.separator+"RandomAccess.txt");
RandomAccessFile raf = new RandomAccessFile(f,"rw");
String name = "zhangsan";
int age = 20;
raf.writeBytes(name);
raf.writeInt(age);
name = "wangwu";
age = 21;
raf.writeBytes(name);
raf.writeInt(age);
raf.close();
}
}
import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo02{
public static void main(String args[])throws Exception{
File f = new File("i:"+File.separator+"code"+File.separator+"java"+File.separator+"RandomAccess.txt");
RandomAccessFile raf = new RandomAccessFile(f,"r");
String name =null;
int age = 0;
byte[] b = new byte[8];
raf.skipBytes(12);
for(int i=0;i<b.length;i++){
b = raf.readByte();
}
name = new String(b);//将读取的Byte数组转换成字符串
age = raf.readInt();
System.out.println("name:" + name + "\n" + "age:" + age); raf.close();