有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何访问随机跳转到固定位置的二进制文件

我正在用java制作一本小词典。我的问题是我不能随机访问二进制文件中的一个单词。例如,如果我说:

  • 发音:/什么
  • 类型:动词
  • 意思:-做某事的乐趣

操纵

  • 发音:/什么
  • 键入://something
  • 意思:-//有些意思

杀死

  • 发音:/什么
  • 键入://something
  • 意思:-//有些意思

单词操纵将比其他两个占用更多字节。我想做的是让程序读取play获得它的位置,跳过三行发音、类型和意义,然后进行阅读操作并获得它的位置,以便我以后可以访问它。 但我可以,因为所有单词都有不同的字节

public class BinaryIO { 
    public static void main (String [] args)
    {
        String file_name = "person.bin";
        try {
            FileOutputStream fileO = new FileOutputStream(new File("person.bin"));
            DataOutputStream out = new DataOutputStream(fileO);
            person teddy = new person();
            teddy.name= "x";
            teddy.address= " somwhere";
            teddy.phone= " 484892";
            out.writeObject(teddy);
            out.close();
            fileO.close();
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
        } catch (IOException e) {
            System.out.println(" out failed");
        }
        System.out.println();
        System.out.println("reading :");

        try {
            FileInputStream fileI = new FileInputStream(new File("ArtistName.bin"));
            RandomAccessFile in = new RandomAccessFile(file_name, "r");

            in.seek(12);
            long len = in.length();
            String str = in.readUTF();
            System.out.println(str);

            str = in.readUTF();
            System.out.println(str);

            System.out.println("length:  " + len + "\n");
            in.close();
        }   catch (FileNotFoundException e) {
            System.out.println("file not found");
        }   catch (IOException e) {
            System.out.println(" in failed");
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    除非绝对强制使用二进制文件,否则我会使用文本文件,其中单个单词属性被放置在新行上。我将每个单词的条目读入一个新的Person对象,并将该Person对象放入一个哈希表中,使用所讨论的单词作为哈希表的键

    代码如下所示:

    Hashtable<String, Person> hashtable = new Hashtable<String, Person>();
    String Word, Pronounciation, Type, Meaning, RequiredWord;
    Person RequiredObj;
    
    try
    {
    
        BufferedReader br = new BufferedReader(new FileReader("dictionary.txt"));
        while(br.ready())
        {
            Word = br.readLine();
            Pronunciation = br.readLine();
            Type = br.readLine();
            Meaning = br.readLine();
    
            if (Word != null && Pronunciation != null &&
                Type != null && Meaning != null)
            {
                // Note: Make the Person object immutable and initialize all attributes
                // in the constructor.
                hashtable.put(Word, new Person(Word, Pronunciation, Type, Meaning));
            }
            else
            {
                break;
            }
        }
        br.close();
    
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }        
    
    // Get a Person object from the hash table.
    RequiredObj = hashtable.get(RequiredWord);
    

    如果必须使用二进制文件,则需要在文件顶部创建一个查找表,列出文件中的每个单词及其相对于文件开头的相应字节偏移量。每个单词的属性需要用某种标记来划分,或者,每个属性可以从一个整数开始,该整数表示属性占用的字节数。然而,这种实现非常混乱,如果需要快速访问单词定义,那么速度会很慢。相比之下,哈希表的速度非常快

  2. # 2 楼答案

    解决方案是为每个条目创建一个定义的char数组,并任意跳转到输入的开头,在我的例子中是在127字节之后