Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:03:31+00:00 2026-06-15T15:03:31+00:00

I have a Random Access File filled with Strings (I know that they are

  • 0

I have a Random Access File filled with Strings (I know that they are not really Strings, although it will help me to explain the problem). What I want to do is to view a certain String, let’s say String #4. While it would be simple for integers and generally primitive data types as they have a fixed byte length and I can read the right bytes by suming up all the previous bytes.

I have managed to solve this problem by giving all the String a fixed length of 16 chars, so if I have the word “dog”, then this word in the RAF is “dog ” (dog + 13 spaces) and the byte length was fixed too. Again, I could easily read the right value using the following method:

static String loadOne(int n) throws IOException {
    raf = new RandomAccessFile(file, "rw");
    raf.seek((n-1)*(fix+2));
    String x = raf.readUTF();
    return x;
}

Where n is the number of the value I want to read and fix is the number of chars (and bytes) of one String.

Everything seemed fine until I used an extra ASCII character – a polish letter – in one of the Strings, because it consists of 2 bytes. The char lenght was still the same – 16, but there were 17 bytes and the whole thing fell apart.

What can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:03:32+00:00Added an answer on June 15, 2026 at 3:03 pm

    I strongly suspect you’re not using readUTF the way it’s expected to be used. Did you read exactly what it does?

    The first two bytes are read, starting from the current file pointer, as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the modified UTF-8 format and are converted into characters.

    Does that match what’s stored in your file? (You haven’t specified anything about the format of the file.)

    Given that UTF-8 is not fixed width, it sounds inappapropriate for your scenario.

    I’d suggest using 32 bytes per entry, which will always give 16 char values as UTF-16 code units. You can convert this very simply using new String(data, "UTF-16BE") and text.getBytes("UTF-16BE") (or use LE instead of BE if you want). That way you’ll have a genuinely fixed-length string, in terms of bytes, not just characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large amount of static data that needs to offer random access.
I have a random access file opened in r+b mode with records of equal
I'm working on a C# script that has to access a random file during
I have some xml files that will be read-only that I need to access
I have a Java program that opens a file using the RandomAccessFile class. I'd
I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the
We have a bunch of VB6 applications that access two different database servers (both
My program needs to read chunks from a huge binary file with random access.
I have a large set of strings that I'm using for natural language processing
Imagine that I have a long file of Rebol-formatted data, with a million lines,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.