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 8987085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:42:20+00:00 2026-06-15T21:42:20+00:00

I have a big file encoded 1250. Lines are just single polish words one

  • 0

I have a big file encoded 1250. Lines are just single polish words one after another:

zając
dzieło
kiepsko
etc

I need to choose random 10 unique lines from this file in a quite fast way. I did this but when I print these words they have wrong encoding [zaj?c, dzie?o, kiepsko…], I need UTF8. So I changed my code to read bytes from file not just read lines, so my efforts ended up with this code:

public List<String> getRandomWordsFromDictionary(int number) {
    List<String> randomWords = new ArrayList<String>();
    File file = new File("file.txt");
    try {
        RandomAccessFile raf = new RandomAccessFile(file, "r");

        for(int i = 0; i < number; i++) {
            Random random = new Random();
            int startPosition;
            String word;
            do {
                startPosition = random.nextInt((int)raf.length());
                raf.seek(startPosition);
                raf.readLine();
                word = grabWordFromDictionary(raf);
            } while(checkProbability(word));
            System.out.println("Word: " + word);
            randomWords.add(word);
        }
    } catch (IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
    }
    return randomWords;
}

private String grabWordFromDictionary(RandomAccessFile raf) throws IOException {
    byte[] wordInBytes = new byte[15];
    int counter = 0;
    byte wordByte;
    char wordChar;
    String convertedWord;
    boolean stop = true;
    do {
        wordByte = raf.readByte();
        wordChar = (char)wordByte;
        if(wordChar == '\n' || wordChar == '\r' || wordChar == -1) {
            stop = false;
        } else {
            wordInBytes[counter] = wordByte;
            counter++;
        }           
    } while(stop);
    if(wordInBytes.length > 0) {
        convertedWord = new String(wordInBytes, "UTF8");
        return convertedWord;
    } else {
        return null;
    }
}

private boolean checkProbability(String word) {
    if(word.length() > MAX_LENGTH_LINE) {
        return true;
    } else {
        double randomDouble = new Random().nextDouble();
        double probability = (double) MIN_LENGTH_LINE / word.length();
        return probability <= randomDouble;         
    }
}

But something is wrong. Could you look at this code and help me? Maybe you see some obvious errors but not obvious for me? I will appreciate any help.

  • 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-15T21:42:21+00:00Added an answer on June 15, 2026 at 9:42 pm

    Your file is in 1250, so you need to decode it in 1250, not UTF-8. You can save it as UTF-8 after the decoding process though.

    Charset w1250 = Charset.forName("Windows-1250");
    convertedWord = new String(wordInBytes, w1250);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey! Let's say I have a big application in one .html file. Another file,
I have a single big text file in which I want to process each
I have a big file with thousands of lines of thousands of characters. I
I have a big file full of integers that I'm loading in. I've just
i have some problems. I have one big file that provide one error with
I have a big html file (87000+ lines) and want to delete all occurrences
I have a big file with tens of thousands of lines containing data separated
I have a big file. It includes approximately 3.000-20.000 lines. How can I get
So I have this big file of fix length lines. I want to do
I have a big file with a lot of lines that share the same

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.