This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 4 years ago.
So I need to read multiple lines of text from a file in Java, for example a txt file record of peoples names height age etc.
I then need to print certain lines from the file, and certain columns, and then sort them based on tallest/oldest etc. How would I go about doing this?
I think I need to put the data from the txt file into an array? But not quite sure.
if I understood the problem, you can create a Class called Person (os something else as you wish) with all the data that you have. than read the file using a Scanner and for each line you can instantiate a new Person and add it to an ArrayList. after you finish reading all the lines from your file, you can use Collections.sort() (you can use the one with a Comparator or use the natural ordering).
links to Collections.sort Javadoc
https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)
https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
Related
This question already has answers here:
Java : parse java source code, extract methods
(2 answers)
Closed 7 years ago.
I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:
while(scanner.hasNext()) {
String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
System.out.println(function);
}
However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?
You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.
This question already has answers here:
I want to print any text without using Print function in java?
(5 answers)
Closed 8 years ago.
I have no idea how to start writing a java code to print
a string without using any inbuilt function like println etc.
Does anyone know how to write it?
I will not paste you all the article you can read here: http://luckytoilet.wordpress.com/2010/05/21/how-system-out-println-really-works/
But read it and look the repetition of "native" word.
Then you can jump to this other post : What is a native implementation in Java?
Then, you will have the presumption that you cannot write to process standard stream (or error) without using any native function, because you need something runnable on different OS... and that's the goal of the JVM.
You can write it using PrintWriter class
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.write("Hello");
printWriter.flush();
printWriter.close();
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 9 years ago.
Improve this question
i have a text file which contains 100000 line as below
be3c152f6f6bcd5 AL9 60 51.7458349055774 -0.191050898942398 F
be3c15cd5 AL9 30 51.79055774 -0.191050898942398 M
now i have to create a design where i need to read all this and based on test condition
needs to generate the output in same file format which i read
i was thinking this to implement by
reading all lines of input file
keeping them in List<some pojo>
now this pojo will have reference to all condition and generate the o\p
my question is loop readline 1 l00000 times and generate the pojo is good or not ?Also in the final o\p folder for each test case condition i have to convert this List<pojo to text format as above what we read.
please let me know some better way.
Read line, process line, write line. No need to keep them all in memory.
It's a simple problem for parsing records.
You don't want each line in a List; you want the POJO containing the data after you tokenize each line.
Here's pseudo-code:
Open file
Loop over all lines in file
Tokenize each line and populate POJO
Add POJO to List
Close file
Perform any operations you wish on POJOs
Output POJO List in desired format
If it's just tab delimited, perhaps you can use a library that already deals with .csv files.
I assume, the test conditions only depend on one line at a time:
You should stream the data both in and out.
After processing the first line - which consists of reading and parsing - check the conditions. If the line should be kept in the output, you can now stream it to the (different) output file. If it should be deleted, you can just ignore it and skip to the next line of the input.
It sounds like a good idea to create an Element which has the different columns of the file as fields. You could then overwrite to String to generate the desired output and a Constructor which takes the String in the input Format to parse it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenating WAV files in Java
What's the simplest way to concatenate more than two WAV files in Java?
If you're asking about concatenating more than two, I'll assume that you already have a way to concatenate exactly two.
In which case you can concatenate multiple files together by simply concatenating each file in turn to the rolling total. Something like this (for four files):
concatenate(concatenate(concatenate(wav1, wav2), wav3), wav4);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create Excel file in Java
How to save output in excel format rather than in .txt file?
A great alternative to an actual excel file is a CSV file which stands for Comma Separated Values. Basically you output your data as follows:
First Name, Last Name, Birthday
Joe, Smith, 10/10/78
Matt, Jones, 2/2/90
With commas between each column, and newlines after each row. If your data has commas in it, you can enclose the data in quotes.
Excel has very good support for loading csv files, and you can even use them as a linked data source in an excel file, and then reference the data from other sheets for graphing, calculations, and pivot tables.