I have an assignment where i have to read a CSV file containing data with some repeated lines. How to remove the duplicate values and print only the unique values in Eclipse
The data is similar to this:-
1,Ron,1234,ABC,12
2,Harry,4125,DEF,14
3,Kent,1786,GHI,15
1,Ron,1234,ABC,12
2,Harry,4125,DEF,14
String csvFile = "csv.csv";
BufferedReader br = null;
String line = "";
HashSet<String> lines = new HashSet<>();
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
if (lines.add(line)) {
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It may help you
My suggestion is to use the following strategy:
1st step: create a HashMap http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
where you will save each line of the CSV you read. You will save in a hashmap because Hashmap will NOT accept a key that is like another. So, each line you will read, you will save in the hashmap as a KEY!
So, the logic is: Try to save the line you just read as a Key. IF it works, print that line. If it didn't work, discard the line and read the next one.
Got it?
2nd step:
Use a BufferedReader http://docs.oracle.com/javase/7/docs/api/index.html?java/io/BufferedReader.html to read line by line of the CSV.
Get each line of the CSV with the BufferedReader with readLine().
It will save the line you are reading in a String
That's it.
So, here is the overview of the entire code:
1- Read each line of the code with BufferedReader.readLine()
2- Get that string you got from readLine and try to add to your Hashmap as the Key of the hashmap: if it works, print the String. If it doesn't work, discard the string;
3- Read the next line.
Related
I have a text file that contains the following:
example.txt
#ignore
#ignore line
#ignore line again
1234567
8940116
12131415
I want to read in the example.txt file into eclipse and add the data into a hashmap. I want the list to be arranged in numerical order and I want it to ignore any comments(any text with #) in the text file. I would like to print the hashmap as follows:
output:
1234567
8940116
12131415
You don't need a hashmap for storing just Strings. Maps are for key value pairs. If you want to put each line from file into a collection use Lists. ArrayLists, LinkedList maintain insertion order. You can use any of them. If you want sorted list you can use TreeList.
BufferedReader reader;
List<String> list = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(
"example"));
String line = reader.readLine();
while (line != null) {
if(!line.startsWith("#"){
list.add(line);
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Thr purpose of a Map is to store pairs Key/Value, for a single collection you may use a List it's far more efficient, the printing part is you job whatever the type of collection is
List<String> values = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("filename"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("#")) {
values.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
for (String v : values)
System.out.println(v);
This question already has answers here:
Modify the content of a file using Java
(6 answers)
Closed 3 years ago.
I am new at java and teacher gave us a project homework. I have to implement read the file line by line, slice the lines at the comma and store the parts at a multidimensional array, change the specific part of the line (I want to change the amount).
The given file:
product1,type,amount
product2,type,amount
product3,type,amount
product4,type,amount
product5,type,amount
I tried this code but I couldn't change the specific part.
BufferedReader reader;
int j=0;
int i=0;
try {
reader = new BufferedReader(new FileReader("file.txt"));
String line = reader.readLine();
while (line != null) {
j++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
String total_length[][]=new String[j][3];
try {
reader = new BufferedReader(new FileReader("file.txt"));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
String[] item = line.split(",");
total_length[i][0]=item[0];
total_length[i][1]=item[0];
total_length[i][2]=item[0];
i++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Thanks a lot!
First, you need to read the file. There are plenty of way to do it, one of them is:
BufferedReader s = new BufferedReader(new FileReader("filename"));
Which allows you to do s.readLine() to read it line by line.
You can use a while loop to read it until the end. Note that readLine will return null if you reach the end of the file.
Then, for each line, you want to split them with the coma. You can use the split method of Strings:
line.split(",");
Putting it all together, and using a try-catch for IOException, you get:
List<String[]> result = new ArrayList<>();
try (BufferedReader s = new BufferedReader(new FileReader("filename"))) {
String line;
while ((line = s.readLine()) != null) {
result.add(line.split(","));
}
} catch (IOException e) {
// Handle IOExceptions here
}
If you really need a two dimensional array at the end, you can do:
String[][] array = new String[0][0];
array = result.toArray(array);
You then have read the file in the format you wanted, you can now modify the data that you parsed.
Sorry if this question has been asked before, but I'm new to coding and I can't find an answer online because I don't know the theory well enough to know how to describe what I'm looking for.
Basically, I want to know if theres a way I can initialize a variable/macro that I can tie to this long try statement, so instead of writing THIS every time I want to read my file
System.out.println("filler");
System.out.println("filler");
try {
FileReader reader = new FileReader("MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("filler");
System.out.println("filler");
I can just write something like..
System.out.println("filler");
System.out.println("filler");
Read1
System.out.println("filler");
System.out.println("filler");
As #king_nak suggests, use a method.
public void readFile(String path) {
try {
FileReader reader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And then you can do what you wanted:
System.out.println("filler");
readFile("MyFile.txt") // call the method
System.out.println("filler");
In Java, MACROS are called constants instead, and are initialised with the final keyword.
For having a String constant for example:
final String str = "Hello World!";
What you need here, is a good ol' fashioned Java method.
You need to declare it outside your main method, in a class of your choosing. What the following method will do, is that it will read a file and add each line of the file to a list (an ArrayList more specifically).
Each element of the ArrayList is one line of text, read from the file.
Note: This method is quite advanced, since it utilises streams to achieve what was described above. If you use this, then please spend some time to understand it first! Otherwise, I would suggest you don't use this as a beginner. (You can read the documentation for for Reading, Writing and Creating files).
public ArrayList<String> readLines (String filename){
ArrayList<String> lines = null;
// Get lines of text from file as a stream.
try (Stream<String> stream = Files.lines(Paths.get(filename))){
// convert stream to a List-type object
lines = (ArrayList<String>)stream.collect(Collectors.toList());
}
catch (IOException ioe){
System.out.println("Could not read lines of text from the file..");
ioe.printStackTrace();
}
return lines;
}
Then you can use the method like so:
ArrayList<String> lines = null; //Initialise an ArrayList to store lines of text.
System.out.println("filler");
lines = readLines("/path/myFile.txt");
System.out.println(lines.get(0)); //Print the first line of text from list
System.out.println("filler");
lines = readLines("/path/myOtherFile.txt");
for( String str : lines )
System.out.println(str); //Will print every line of text in list
Here is the link for the documentation for java.nio.Files.
I have a text file that consists of several entries such as:
one
two
three
four
five
six
The text file contains 100 lines and I want to read each 5 lines in a once. I have this code but it give null values:
BufferedReader br = null;
String sCurrentLine;
int lines = 0;
try {
br = new BufferedReader(new
FileReader("/users/MoathIbrahem/Desktop/Questions.txt"));
while(br.readLine()!= null)lines++;
for(int i = 0;i < lines;i++)
System.out.println(br.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
In this line
while(br.readLine()!= null)lines++;
you are going to read ALL of the text file.
Nothing more to read unless you re-open the File or use mark/reset
This is happening because when you read a line a reader pointer advances. When you try to read in the for loop this pointer has reached the end of the document.
I recommend you to not count the lines before. Like the user gotomanners says in his answer:
String line;
while ((line = fileReader.readLine()) != null) {
System.out.println(line);
}
Here is my code:
try {
String textLine;
FileReader fr = new FileReader("ad.txt");
BufferedReader reader = new BufferedReader(fr);
while((textLine=reader.readLine()) != null) {
textLine = reader.readLine();
jTextArea1.read(reader, "jTextArea1");
}
}
catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
And my .txt file contains the following:
contig00001 length=586 numreads=4
CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa
ggCCGCGATCCCGTCggaCGGAAAaCGCCcTGGCCCGGGAaCATACCGTTCGGGCCGCCA
AGTGTTATAGCCGGACCACTTGTCAGAACATTTCCaaTCCGAAGATGTGAGTtCGGAAGg
TAAAAGCCCGACAAGTTGCGCGgTGAATTTACCTTtACcGCACGATATGCGTCCGTATTA
AaGAAAaGTTCGAAATTATCAGTAAGGCCGACCTGAAaGCTGACCGGGAGTTCAACAAAA
TCTGCATCACCcGGgTCACGGTCGAAATTGCTGTACGCGGCGCTGAACGTAAATTCACCC
TTTcTAAGGGTGTCGCcGTCGTAAACCGTAAaCAaGCCGGTAGCGCCGCCCATCGGGCCG
CCGGTACCAACCGTCGGTGCCGTGTTTCTtGCATCATTGTCCGATCGAGCGTTCTCGTCC
GCTTGTGCAAaTCCTGCAaTAGCTAACGTGAAAACGATCAGAGCTGTTGTAAATACTCTA
TAAGCGAGATTCATCACATTCCTCcGCCGAAATAAAAAGTTAATTt
contig00002 length=554 numreads=4
TGCGCCAaCCGCGCTCTtCATAAaTGGGCACTGCTCCCGATGGCCgACTCGGGCGGTTCG
CCATGAGATCTTTGCCtACCcAGgAaCtCACcACCAAGTCTGATTGCTGTGTGTTTtCTT
CAAGTCCCTATTTCTATTCtCTTtAATGGAACCCGTAGGAAACCCGTGTAGGACGCGGGA
aCCGCACTTgAAGGGGGAGGCGCGGGGTACCGGtCCGGGAACGTACGGGTACCGGCGGGG
gAGGGGAGGGGGACCgCTCCGGGAAGGCCAGGGGACGGATTGGGGAAGGgCGGGTACCGA
AGCGGGgAAaTGGGggAaCcGGCGAGAGGGTTCCTCGCTAAGTGGGGGAAATaGGGGAAA
GGTTGACCAGTGGTtCCCcGCTCTCGTAACATGCCTCAGATAGCGCCATCCGCTGTACCT
GGtcaggtcGctggcaacttcggccgagcaggtgaacccgaaaggtgagggtcagtgtga
cacaccaaccgaacaccgacgaggcaagcgtaggagccggcgtggccgcgcccggcggcg
ctgaggactcctcg
But shows the output by skipping the first two lines.
What is the reason for this?
You don't need the while loop, or the readLine method. Just call jtextArea1.read(reader, "jTextArea1")
Edit: update following your comment. If you want to skip all lines starting with >, you will need to read the file manually and then append each line to your textArea.
So something like:
String line;
while ((line = reader.readLine()) != null)
{
if (!line.startsWith(">"))
{
jTextArea.append(line + "\n");
}
}
Use:
FileReader reader = new FileReader("filename.txt");
txtarea.read(reader, "filename.txt"); //Object of JTextArea
You need only the above two lines to read from a file and put it into JTextArea...
The problem must have been solved by the time, yet there's still no answer to the question why the first two lines are skipped.
You create reader and then read the first two lines from the file, remaining lines are loaded into jTextArea1.
Your code:
/* 1 */ while((textLine=reader.readLine())!=null){
/* 2 */ textLine = reader.readLine();
/* 3 */ jTextArea1.read(reader,"jTextArea1");
}
Line 1 reads the first line from the file. Then in the body of while you read the second line from the file at line 2. Line 3 reads the rest of the file into jTextArea1.
On the next iteration of the while loop, reader.readLine() returns null since the file is completely read.
To load text in a JTextComponent use its read method as suggested by Phill and Bhushankumar.
The second parameter to read is not used by JTextArea, so it's safe to pass null. This second parameter is usually used to store to URL of the loaded file to resolve relative references, for example links in an HTMLDocument.
textLine = reader.readLine(); is called twice...
Fixed:
try {
String textLine;
FileReader fr = new FileReader("ad.txt");
BufferedReader reader = new BufferedReader(fr);
while((textLine=reader.readLine()) != null){
// textLine = reader.readLine(); // Remove this line
jTextArea1.read(reader, "jTextArea1");
}
}
catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
Correctly is:
try {
FileReader fr = new FileReader("tablica.txt");
BufferedReader reader = new BufferedReader(fr);
do {
l.read(reader, null);
}
while ((textLine=reader.readLine()) != null)
;
}
catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}