Code
currently it reads the WHOLE file at once,
the text file is set up like
I'm a text:
I'm another text:
I'm just a nother text:
any help would be apprciated.
currently it reads the WHOLE file at once,
the text file is set up like
I'm a text:
I'm another text:
I'm just a nother text:
any help would be apprciated.
Do you really have to read the text file line by line (e.g. because it's an extremely huge text file)? Or do you just want to check the lines individually and look for a specific line? In this case you could read the whole file by using the Utils.FileUtils.readStringFromFile(file); function, and split the text by the newline escape sequence \n, for example:
Output:
Of course you could grab a specific line directly, e.g. if you want to access line 53, you could just access it from the lines array: String line53 = lines[52]; (it's array index 52 ofc)
However, if you really want to read every single line individually without having to load the rest of the text (this makes sense if it is a really huge text file), you can use a BufferedReader, like in the code you've posted.
its a bunch of Quotes that is in a text file. i didn't feel the need to have a database
"Merry Christmas" <- line one
"You smell like fart" <-- line two
I'm trying to make it say a random Quote after a command is issued.
and i thought having a text file it would be eaiser to add/delete quotes instead of having to re-compile the plugin every time.
thanks it looked like i was on the right path... but it jusr read the while thing at once,. that is how i used to read text files in BASIC, Java is different.
If the file is only a few hundred Kb or less, just load it entirely and keep each line in an ArrayList<String>. Because the cost of reading a file vs keeping a few Kb in memory is really an obvious choice, here.
Don’t have an account yet? Create a new account now and be part of our community!