Results 1 to 2 of 2

Thread: I have a question about Java and reading files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    I have a question about Java and reading files

    Normal ways of accessing files like reading data via the DataInputStream( which then contains a FileInputStream), only allows sequential reading from the file with things like readDouble(), or readInt(). Nowhere is there a way to read data from an arbitrary location like readInt(int offset). Nor is there a seek(int offset) function to set an pointer for the next read operation. This will cause trouble when reading from any but the simplest of file types. You see, many file types have fields that contain offsets to other structures in the file. These can't be read sequentially, because after reading an offset, you need to seek to that offset and then start reading from there. But you can't do that it seems in Java, at least not when using DataInputStream. What would be the correct way (if it's at all possible in Java) to do this in Java?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: I have a question about Java and reading files

    While I have read a lot of Java code, I've never used the language myself so I decided to drop your question into ChatGPT GPT-4. This is the answer it gave:-

    In Java, you can use RandomAccessFile for this purpose. The RandomAccessFile class in Java allows you to move around a file and read from or write to it at arbitrary locations. You can use seek(long pos) method to set the file-pointer offset, measured from the beginning of the file, at which the next read or write occurs.

    Here is a simple example:
    Along with this sample code:-
    Code:
    try {
        RandomAccessFile file = new RandomAccessFile("file.txt", "r");
    
        file.seek(50); // Seek to the 50th byte of the file
    
        int data = file.readInt(); // Read an int from that location
    
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    And this epilogue:-

    In this example, we open a file for reading ("r"), seek to the 50th byte, then read an integer. Note that an IOException may be thrown if something goes wrong, so we have to handle it.

    Keep in mind that the seek method uses a long for the position, which means it can handle very large files. Also, remember that it's always a good practice to close the file after you're done with it, which you can do with the close method.
    Never having used Java myself, I can't speak to the accuracy of this answer but I do use GPT-4 a lot and it's quite reliable. I expect that 95% of what it said is correct. Hopefully this at least gives you a direction to explore.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width