Java 搜索随机访问文件字符串显示

Java 搜索随机访问文件字符串显示,java,randomaccessfile,Java,Randomaccessfile,我在做一个项目 我需要读取并显示一条记录,其中包含学生ID和姓名。该用户已输入(按ID读取) 我使用writeUTF和ArrayList写入文件 public void readFile(字符串sID)引发异常{ studentID = sID; // take passed variable try { //open read file RandomAccessFile sFile = new RandomAccessFile("newS

我在做一个项目

我需要读取并显示一条记录,其中包含学生ID和姓名。该用户已输入(按ID读取)

我使用writeUTF和ArrayList写入文件

public void readFile(字符串sID)引发异常{

    studentID = sID;  // take passed variable


    try {
        //open read file 
        RandomAccessFile sFile = new RandomAccessFile("newStudent.dat", "r");
//**我不明白这段很长

        long pos = sFile.length();  // Get length of File 

        sFile.seek(pos); //moves pointer , I get this, but Why?

        for (int i = 0; i < sFile.length(); i++) {

            //read the file 

            String result = sFile.readUTF();

            if (result.equals(studentID)) 
一旦这是完整的,我将需要做一个读取和更换记录

我在这一点上迷失了方向,任何可能的建议。我有一些类似的帖子 我不明白我需要修复什么。 在构建JDesktopPane和JInternalFrame之前,我只需要测试一下 简单是我目前的目标


感谢您提供的任何反馈或I方向

我建议您将文件指针设置为文件的开头,然后开始循环并不断检查是否未到达文件的结尾。 下面是如何修改代码的示例:

import java.io.IOException;
import java.io.RandomAccessFile;

public class ReadUTF {

   public static void main(String[] args) {
       String studentID = "sID"; // take passed variable

       try {
           // open read file
           RandomAccessFile sFile = new RandomAccessFile("c:\\temp\\newStudent.dat", "r");

        sFile.seek(0); // moves pointer to the first location to read

        long current = 0; 
        while (current < sFile.length()) {

            // read the file
            String result = sFile.readUTF();

            if (result.equals(studentID))
                // **here I want to print whole record with the name
                System.out.print(result); // testing print

            current = sFile.getFilePointer(); // update to the next location to be read

        }
        sFile.close();

    } catch (IOException e) {
        System.out.print("test File caught ");
    }
  }
}
import java.io.IOException;
导入java.io.RandomAccessFile;
公共类ReadUTF{
公共静态void main(字符串[]args){
String studentID=“sID”;//获取传递的变量
试一试{
//打开读取文件
RandomAccessFile sFile=新的RandomAccessFile(“c:\\temp\\newStudent.dat”,“r”);
sFile.seek(0);//将指针移动到要读取的第一个位置
长电流=0;
while(当前
你能解释一下什么是有效的还是无效的吗?我想从文件中获取特定的数据,难道我不能直接将我传递的字符串与我读取的文件中的二进制数据进行比较……我将制作另一个修改记录的java文件太好了,谢谢!我没有意识到我已经接近了-呸!我快死了
import java.io.IOException;
import java.io.RandomAccessFile;

public class ReadUTF {

   public static void main(String[] args) {
       String studentID = "sID"; // take passed variable

       try {
           // open read file
           RandomAccessFile sFile = new RandomAccessFile("c:\\temp\\newStudent.dat", "r");

        sFile.seek(0); // moves pointer to the first location to read

        long current = 0; 
        while (current < sFile.length()) {

            // read the file
            String result = sFile.readUTF();

            if (result.equals(studentID))
                // **here I want to print whole record with the name
                System.out.print(result); // testing print

            current = sFile.getFilePointer(); // update to the next location to be read

        }
        sFile.close();

    } catch (IOException e) {
        System.out.print("test File caught ");
    }
  }
}