Jump to content

Reading random file line

Ovisss4

Hello there,

I would like to ask a way to read random line in a file. So the thing is that it needs to be able to read fast. In current state it's reading line by line and there is 50% of chance to read next line. But in my opinion it would take way to much time to generate the last line. And another thing file contains names and needs to generate list which multiple names. So my first idea is somehow get to some random place at file, then search left for line end and search at right for line end. But not sure how to get at random spot of file.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not really sure what you're trying to do but to answer your question many languages have a file seek method that will let you start reading at any position. You didn't say what language you're using so I can't give any specifics.

 

edit: Just saw the Java tag. I believe the RandomAccessFile class can do what you want.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not really sure what you're trying to do but to answer your question many languages have a file seek method that will let you start reading at any position. You didn't say what language you're using so I can't give any specifics.

 

edit: Just saw the Java tag. I believe the RandomAccessFile class can do what you want.

Thanks RandomAccessFile worked. But I have another problem. I didn't mentioned that the names in file is In Lithuania language and contains these characters = { ą | č | ę | ė | į | š | ų | ū | ž }. But reading byte and converting it to char (byte letter = file.readByte(); line += (char)letter; // line is a String) seems to fail and put some symbols instead of letters mostly square character.

 

edit: It seems that those characters are negative bytes. Could that mean that I need something longer than byte ?

Link to comment
Share on other sites

Link to post
Share on other sites

Use a random number generator to generate a random number. Divide it by the number of lines in the document and take the remainder. Access the line identified by the remaining number. Done.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks RandomAccessFile worked. But I have another problem. I didn't mentioned that the names in file is In Lithuania language and contains these characters = { ą | č | ę | ė | į | š | ų | ū | ž }. But reading byte and converting it to char (byte letter = file.readByte(); line += (char)letter; // line is a String) seems to fail and put some symbols instead of letters mostly square character.

 

edit: It seems that those characters are negative bytes. Could that mean that I need something longer than byte ?

That's because those letters fall outside of the ASCII table (1 byte). A char in Java is 2 bytes (ANSI) to allow a larger range of characters. If you read it to 1 byte, then you are effectively limiting yourself to ASCII. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×