Java - What is the fastest way for reading a file [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to read a file as a string. Every line of this file contains information that I use to construct new objects.
I wonder what is the fastest way of doing that. Obviously, there are two options. The first is to read the whole file and create the objects by reading the string. The second is to read the data line by line and create a new object after every readLine(). Is there a real performance deference or I can go either way?

If there is an object every couple of lines and it won't affect anything else after or before it I would go for the line by line approach. However, if there is parsing involved with nested objects or a more complicated file structure read it all first

Related

If multiple classes contain same code for error checking should I extract it into new class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm working on a project where several classes have the same error checking in one of their methods. Should I extract this error checking into a different class, even though it's only under 10 lines of code?
As an example, the method in all the classes take in the same object and does something different with it, I'm checking initially if that object is null and then getting a list from the object to see if it's empty. I was thinking of extracting these checks because I've heard each method should only do one thing.
As a general rule of thumb, you should always extract identical functionality in your codebase and reuse them instead of copying & pasting over. If you have to change the validation logic you'll have to do a shotgun surgery if it is appearing in multiple methods.
There are some battle-tested open-source libraries available for null checking and determining if collections are empty. Look around before implementing your own. Here is an example from the Guava library.

Java large inputs and outputs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been working on a java algorithm and I want to improve it by allowing it to accept and process very large String values.
Could you suggest me any good ways of storing the input/output results. I've been thinking of writing it on a file with the readLine(), writeLine() methods? Is this a good technique ???
I recommend you to store all your string in a text file (if possible) and use BufferedReader utilities ...
here is the a link to how its done :
https://www.tutorialspoint.com/java/io/bufferedreader_readline.htm

Read A Large File Using Java NIO [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to read the content of a large file. For that I Googled on it and found so many methods and resources. But I'm still confused which is the method to read the large files (Factors need to be consider in my case are memory allocation, performance, large file )
Using FileChannel
using Files.readAllLines
using BufferedReader
Can anyone guide on this?
Your best option is to read the file lazily. Fetch each line one at a time and process.
Example:-
Stream<String> lines = Files.lines(Paths.get("C:/files", "yourfile.txt"));
Then process the lines afterwords.
From the official documentation:-
public static Stream<String> lines(Path path, Charset cs) throws IOException
Read all lines from a file as a Stream. Unlike readAllLines, this
method does not read all lines into a List, but instead populates
lazily as the stream is consumed.

how to implement a variable whose value is not lost after every run? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wish to implement a variable (in Java) whose value is either stored somewhere or is not reset every time I run the program.
It's related to a "Booking reference Number" for a flight program. I know database connectivity but make a new data base for one variable is pretty pointless. Any ideas as to what I should/can do?
Also I don't want the numbers to be random I want them in order like if the first booking ID is 100 then the next one should be 101 and so on.
Organize your data in a structure and then serialize it.When you re-run your program, look for that serialized version in the file system, if there is any, read it. Viola.!
You should write the variable to a file and then read it from the file the next time you run the program.

*ignore* How to check if an Array<String> contains an item that starts with a value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The app fetches data from a server. The data is returned in an Array. I need to check if one of the Array items starts with a value specified in the app. If it's possible without iterating because that slows down the app significantly.
If you have just an array data structure, there is no way to check what it contains without actually looking into it.
However if you use a different data structure such as a HashMap (which is built on to of an array) you can check/lookup a key like "101" in O(1) time typically. You can check map.isEmpty() in O(1) time.
In short, if it's taking too long to perform a simple operation, chances are you need to be using a different data structure (or possibly more than one)

Categories

Resources