为什么用writeInt方法写入的数据读不出来 而writeChar的可以
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
File file=new File("imooc");
if(!file.exists()){
file.mkdir();
}
File f=new File("imooc\\a.txt");
if(!f.exists()){
f.createNewFile();
}
RandomAccessFile raf=new RandomAccessFile(f,"rw");//创建一个随机访问文件对象
System.out.println(raf.getFilePointer());//返回指针的位置
String str="中国";
byte[] ba=str.getBytes();
raf.write(ba);
String str2="我爱慕课网123abc";
byte[] ba3=str2.getBytes();
raf.write(ba3);
System.out.println(raf.getFilePointer());
int i=10;
raf.writeInt(i);
raf.writeChar('A');
raf.seek(0);
byte [] ba2=new byte[(int)raf.length()];
raf.read(ba2);
String ns=new String(ba2);
System.out.println(ns);
}
}我用writeInt写入的输入10在文件中显示是空格,而打印出的char字符A直接显示在第二行,并且开头有个空格,还有问下我看api里面writeBytes和writeChars都写的是“ 按字节序列将该字符串写入该文件。”请问有什么区别呢 还有这两种写入的方法要分别注明读出来呢