Java, Turn Each Row of a CSV into a string - java

Hello I'm a newbie in Java using BlueJ. I have a csv file that contains a load of data in a table arrangement. I'm trying to find a way to take this information and find out how many comma separated values there are in the first row then regardless of rows put each comma separated value into an array.
Does anyone have any advice on how to do this?
Thanks in advance, Harry.

CSV parsing can be tricky because of the need to support quoted values. I suggest not writing your own CSV parser, but using an existing library such as http://opencsv.sourceforge.net/.

You can use the Scanner file, to read each line of the file, something similar to:
// create a File object by giving the filepath
File file = new File("C:\\data.csv");
try {
// Create a new scanner class that will read the file
Scanner scanner = new Scanner(file);
// while the file has lines to read
while (scanner.hasNextLine()) {
// read the line to a string
String line = scanner.nextLine();
// do what you need with that line
}
// catch the exception if no file can be found
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Here is another library to handle csv file. javacsv
Code example is here

Related

how do i use a scanner to read a file object

like the question how would I use a file object taken from the File class
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/io/File.html
and use a scanner object wrapped around this to read the file,
then say a example like this is contained in the file,
Student{name=Jill Gall,age=21,gpa=2.98}
separate it into fields, skip next step as I just have to call the setters here, and then set the values
I then to save the fields as a Student Object and then save those in a array that I can return?
I'm not really looking for a solution to the entire problem here I'm just wondering as to what the syntax is for calling a scanner object wrapped around a File object to read it, I can use substring to grab the fields to separate and save them onto the new fields, and I'm pretty sure I can just use a for loop to load these onto a array that i can return, the only reason I listed out the entire problem for you guys is so I can give you guys the most details about the task as I can give here
right now my best attempt is this
Scanner n = new Scanner(System.in);
n.commandgoeshere?(filename);
how would I get the scanner to output the data so that i can take a substring and do the stuff i need to do above?
You have to provide an input stream in the constructor of the scanner, atm you are providing the system input stream (from the console) to the scanner. I think something like this is what you are looking for:
File file = new File("");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String nextLine = scanner.nextLine();
// or use regex ?
String extractedPattern = scanner.next("some pattern");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

OpenCSV reads strange text out of file

I am using AndroidStudio and my applications has to read in a CSV file which looks like this:
"Anmeldung";"1576017126809898";"1547126680978123";"";"";"Frau"
"Anmeldung";"1547126680911112";"1547126680978123";"";"";"Frau"
But as you can see in the following picture, OpenCSV reads some strange character and in my List there are senseless Strings which are not in the file it read
This is how I read the Data out of my file:
try {
FileReader filereader = new FileReader(filePath);
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withSkipLines(1)
.withCSVParser(parser)
.build();
List<String[]> allData = csvReader.readAll();
MainActivity.setAllData(allData);
}
catch (Exception e) {
e.printStackTrace();
}
Thank you
It looks like there is an encoding poblem.
Make sure to open and parse the file with the proper encoding (for example utf-8 or utf-16). Same for viewing the data.
I figured it out. It might sound strange but I took the file and replaced all ; with ;
I think the data I got were exportet with an UTF-16 Encoding or from an linux device.
tl;dr The File had the wrong encoding. And the way I opened and viewed it was correct

How to read / write into docx file using commons.io.FileUtils?

Need some quick help. I am trying to write a java program to generate a report. I have the report template in a docx file.
What I want to do is, use that docx file as template and put data in it multiple times for various records and write that to a new docx file. The main thing is I want to maintain the formatting and indentation of the contents inside the docx file. They are bullets data. And that's where the problem is.
Below is the piece of code handling the above operation,
public void readWriteDocx(HashMap<String, String> detailsMap) {
try {
File reportTemplateFile = new File("ReportTemplate.docx");
File actualReportFile = new File("ActualReport.docx");
StringBuilder preReport = new StringBuilder();
preReport.append("Some details about pre report goes here...: ");
preReport.append(System.lineSeparator());
String docxContent = "";
for (Map.Entry<String, String> entry : detailsMap.entrySet()) {
docxContent = FileUtils.readFileToString(reportTemplateFile, StandardCharsets.UTF_8);
// code to fetch and get data to insert into docxContent
docxContent = docxContent.replace("$filename", keyFilename);
docxContent = docxContent.replace("$expected", expectedFile);
docxContent = docxContent.replace("$actual", actualFile);
docxContent = docxContent.replace("$reportCount", String.valueOf(reportCount));
docxContent = docxContent.replace("$diffMessage", key);
FileUtils.writeStringToFile(actualReportFile, docxContent, StandardCharsets.UTF_8, true);
}
preReport.append(FileUtils.readFileToString(actualReportFile, StandardCharsets.UTF_8));
System.out.print(preReport.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
As you can see, I am using FileUtils read and write methods and using UTF_8 encoding. That's just a guess, I am not sure about the same. I am trying to append the newly generated docx file contents to a string builder and print the same on console, but that's secondary. Main thing is that the docx should be written properly. But no luck.
When this prints, its all weird characters and nothing is readable. When I try to open the newly generated docx file, it doesn't even open.
Any idea what should I do to get the data in proper format. I am attaching image file of how my ReportTemplate.docx looks, that I am using as a template to generate this report. I am using commons-io-2.4.jar
Please guide if you can. Thanks a lot.
You can use Apache POI for creating and editing doc docx files or docx4j. Otherwise there is no simple way to edit doc or docx files without these libraries.

How to write CSV to JSON without a header row, using CDL

I had some data the mongodb database in JSON format...I wanto to convert it into JSON format for further processing....I converted my data into CSV format but I don't want the headings in the CSV file....I Want to remove the headings from the CSV file. Hope my question is clear now.........
My CSV file has the following format.
**date,lng,time,place**
16-05-2013,77.152515,NIGHT,LosAngeles
16-05-2013,,77.883754,EVENING,NY
-----------------------------------
------------------------------------
-------------------------------------
I converted my jsondata into CSV and tried to replace the headings date,lng,time,place but I didn't find any way to do it through coding.....Can anyone guide me....
JSONObject root = new JSONObject(data.getJsonData());
JSONArray docs = root.getJSONArray("data");
File file=new File("/home/Documents/file1.csv");
String csv = CDL.toString(docs);
System.out.println("csv"+csv);
try {
FileUtils.writeStringToFile(file,csv);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The solution is to use the toString that takes a separate array of names, passing the names like so:
String csv = CDL.toString(docs.getJSONObject(0).names(), docs);
If you want to avoid a JSONException on an empty docs array you will want to check to make sure that docs is not empty before doing the above.
For more details, take a look at the documentation forCDL, in particular CDL.toString(JSONArray, JSONArray), which states:
The list of names is not included in the output.
If you take a look at the source you can get a slightly clearer idea of what is happening.
Note that if you want to change the set of columns in the output, or their order, you can pass in any JSONArray of names you want as the first parameter - the output columns will correspond to the names in that array.

Knowing delimiters for CSV file

This may be a simple question but I have not been able to find a satisfactory answer. I am writing a class in Java that needs to take in a .csv file filled with doubles in three columns. Obviously a .csv file uses commas as the delimiters, but when I try setting them with my scanner, the scanner finds nothing. Any advice?
Scanner s = null;
try {
s = new Scanner(source);
//s.useDelimiter("[\\s,\r\n]+"); //This one works if I am using a .txt file
//s.useDelimiter(", \n"); // This is what I thought would work for a .csv file
...
} catch (FileNotFoundException e) {
System.err.format("FileNotFoundException: %s%s", e);
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
A sample input would be:
12.3 11.2 27.0
0.5 97.1 18.3
etc.
Thank you for your time!
EDIT: fixed! Found the correct delimiters and realized I was using hasNextInt() instead of hasNextDouble(). /facepalm
Consider the following:
first,second,"the third",fourth,"the,fifth"
Should only be five - the last comma is in a quote block, which should not get split.
Don't reinvent the wheel. There are open source libraries to handle this behavior.
A quick google search yielded http://opencsv.sourceforge.net/ and I'm sure there's others.
If you are trying to read each individual item, try:
s.useDelimiter(",");
Then s.next() would return an item from the CSV.
Why have you got a \n in your CSV delimiter? Java doesn't have a difference between CSV and TXT files, if they have the same content.
I would think you would want
s.useDelimiter(",");
or
s.useDelimiter("[\\s]+,[\\s\r\n]*");
There are several methods to workaround:
Method 1:
use conditional statements ( if-else / switch ) in file extension.
if(ext == 'csv') {
s.useDelimiter(", \n");
} else if(ext == 'txt') {
s.useDelimiter("[\\s,\r\n]+");
}
Method 2:
as other answers suggested, use this:
s.useDelimiter(",");

Categories

Resources