需求是在windows系统下能打开文本不乱码,适用的是write(byte[]);但最后一组数据显示不正常package kkk;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadomAccessFileTest {
public static void RandomToWrite(File file) {
Student[] students = new Student[4]; 
students[0] = new Student("优秀", "90");
students[1] = new Student("优良", "80");
students[2] = new Student("良好", "70");
students[3] = new Student("及格", "60");
RandomAccessFile randomAccessFile = null; // 创建RandomAccessFile对象
try {
randomAccessFile = new RandomAccessFile(file, "rw"); // 创建一个可以支持读写状态的RandomAccessFile对象
} catch (FileNotFoundException e1) {  // 捕获异常
System.out.println("文件没有找到" + e1.getMessage());  // 输出异常信息
}
try {
for (int i = 0; i < students.length; i++) // for遍历students数组
{
randomAccessFile.write(students[i].getLevel().getBytes());// 向文件中写入评分等级
randomAccessFile.write(students[i].getScore().getBytes());// 向文件中写入分数
}
randomAccessFile.close(); // 关闭randomAccessFile
} catch (IOException e1) { // 捕获异常
e1.printStackTrace();  // 输出异常信息
}
}
public static void main(String[] args) throws Exception { 
int len=0;
String strName = "";  
byte buf[] = new byte[8]; 
File file=new File("D:/a.txt");
RandomToWrite(file);
RandomAccessFile ra = new RandomAccessFile(file,"r"); 
ra.seek(0);
do{
len =ra.read(buf);  
strName = new String(buf,0,len);  
System.out.println(strName.trim());
System.out.println(ra.getFilePointer());
len=ra.read(buf);  
strName = new String(buf,0,len);  
System.out.println(strName.trim());
System.out.println(ra.getFilePointer());
}while(ra.getFilePointer()<ra.length());
ra.close();
}
}
class Student {
String level;  // 评分级别
String score;  // 分数
final static int SIZE = 8;// 创建,并初始化静态域LEN
public Student(String level, String score) {
// 初始化level
// 初始化score
setLevel(level);
setScore(score);
}
// 获取类占用的空间
public String getLevel() {
return level;
}
public void setLevel(String level) {
if (level.length() > SIZE) {
level = level.substring(0,8); // 截取字符串的子字符串
} else {
while (level.length() < SIZE)
level = level + "\u0000";
}
this.level=level;
}
public String getScore() {
return score;
}
public void setScore(String score) {
if (score.length() > SIZE) {
score = score.substring(0,8); // 截取字符串的子字符串
} else {
while (score.length() < SIZE)
score = score + "\u0000";
}
this.score=score;
}
}
运行结果为优秀
90
优良
80
良好
70


60
60

解决方案 »

  1.   

    read(byte[])方法不保证能把缓冲区填满吧推荐使用 writeUTF()和readUTF()方法
    randomAccessFile.writeUTF(students[i].getLevel());
    randomAccessFile.writeUTF(students[i].getScore());
    // randomAccessFile.write(students[i].getLevel().getBytes());// 向文件中写入评分等级
    // randomAccessFile.write(students[i].getScore().getBytes());// 向文件中写入分数do {
    strName = ra.readUTF();
    // len = ra.read(buf);
    // strName = new String(buf, 0, len);
    System.out.println(strName.trim());
    System.out.println(ra.getFilePointer());
    strName = ra.readUTF();
    // len = ra.read(buf);
    // strName = new String(buf, 0, len);
    System.out.println(strName.trim());
    System.out.println(ra.getFilePointer());
    } while (ra.getFilePointer() < ra.length());
      

  2.   

    如果要求可以用记事本打开不乱码,程序里只需要简单把read(byte[])修改成 readFully(byte[])即可,不过会有问题产生。建议:1.把所有内容组合成一个字符串,用分隔符分割。读取时,全部读取,然后分割
    2.或者每行只记录一个名字或者分数,读取时,一行名字一行分数否则的话,如果名字出现3个字,或者分数出现1位或者3位的,名字+分数未必每次都正好8字节。
      

  3.   

    换成FileReader和FileWriter就是字符的了
    字节流就用字节方法,字符流就用字符方法
      

  4.   

    你这个控制台输出有问题啊,System.out.println(ra.getFilePointer());这句话怎么没见输出
    另外do-while循环里,在len =ra.read(buf);  后面都加个判断
    if(len ==-1)
    {
       break;
    }
      

  5.   

    while((len = ra.read(buf)) >0){
    System.out.print(new String (buf , 0, len));
    }
    level = level + " ";
    score = score + " ";
      

  6.   

       level = level + " ";
    score = score + " ";
    这样做不至于文章 字符串被null分割这张读取时会有乱码