I am getting a file in byte[] which have comma separated values in quotes, I want to save it as CSV by using OpenCSV. I am using this to save it in CSV format.
Following is my code to convert byte[] to array and then store it in file
byte[] bytes = myByteStream.getFile();
String decoded = new String(bytes, "UTF-8");
//String lines[] = decoded.split("\\r?\\n");
FileOutputStream fos = new FileOutputStream("/home/myPC/Desktop/test.csv");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
CSVWriter writer = new CSVWriter(osw);
String[] row = {decoded};
writer.writeNext(row);
writer.close();
osw.close();
But this above code puts extra quotes around and also merge all lines in one line.
Any help on how to do this properly ?
You can prevent adding quotes to the cell values within the constructor of CSVWriter, for example:
CSVWriter writer = new CSVWriter(osw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
Regarding the whole byte array persisted as a single row. Are you sure there are newlines within the original file.
If so you might get away by doing the following:
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "ASCII"));
String line;
while ((line = reader.readLine()) != null) {
// Handle the line, ideally in a separate method
}
Got this from splitting a byte array on a particular byte
I think Apache Commons CSV is a better CSV library. From the pasted code it is not clear if you want to somehow change the contents of the file or just duplicate it: in the latter case you don't even need to parse the file as CSV records - just copy it byte-for-byte. In the former case, ie if you need to modify the content of the file, you need something like (Commons CSV used here)
CSVParser parser = CSVFormat.newFormat(',').parse(
new InputStreamReader(new ByteArrayInputStream(bytes), "UTF8"));
CSVPrinter printer = CSVFormat.newFormat(',').print(out);
for (CSVRecord record : parser) {
try {
printer.printRecord(record);
} catch (Exception e) {
throw new RuntimeException("Error at line "
+ parser.getCurrentLineNumber(), e);
}
}
parser.close();
printer.close();
Look at the Javadoc for CSVFormat
Related
I am managed to get the data out of an API and put into a CSV, but I have problems to put the data into the CSV in a loop because right now it always overwrites it in the CSV. And the next problem is that the date does not get shown in the CSV in different fields. In the CSV it looks
like this:
and I want all the data like in my console:
my code right now:
JSONArray jsonarr_1 = (JSONArray) jobj.get("infectedByRegion");
//Get data for Results array
for(int i=0;i<jsonarr_1.size();i++)
{
//Store the JSON objects in an array
//Get the index of the JSON object and print the values as per the index
JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
//Store the JSON object in JSON array as objects (For level 2 array element i.e Address Components)
String str_data1 = (String) jsonobj_1.get("region");
Long str_data2 = (Long) jsonobj_1.get("infectedCount");
Long str_data3 = (Long) jsonobj_1.get("deceasedCount");
System.out.println(str_data1);
System.out.println("Infizierte: "+str_data2);
System.out.println("Tote: "+str_data3);
System.out.println("\n");
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("C:/Users/stelz/OneDrive/Desktop/Corona Daten/28.04.2020.csv"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
String columnNamesList = "Bundesland,Infizierte,Tote";
// No need give the headers Like: id, Name on builder.append
builder.append(columnNamesList +"\n");
builder.append(str_data1+",");
builder.append(str_data2+",");
builder.append(str_data3);
builder.append('\n');
pw.write(builder.toString());
pw.close();
System.out.println("done!");
}
//Disconnect the HttpURLConnection stream
conn.disconnect();
Your CSV is probably a nice comma delimited file with UTF-8 encoding. The first line in the image from Excel is an evidence that in the file and as text the first line is correctly:
Thüringen,2170,80
But your Excel has probably national defaults for Western Europe, meaning semicolon (';') as CSV separator, and CP1252 (slight variation of the Latin1 charset) for encoding.
How to fix?
Excel is known to have a very poor support for CSV file: read it as text (notepad or notepad++) or use LibreOffice calc which allows to declare the separator and the encoding when reading a CSV file.
If you have to stick to Excel, carefully build your file for your own Excel, use ISO-8859-1 charset and semicolons:
...
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("C:/Users/stelz/OneDrive/Desktop/Corona Daten/28.04.2020.csv"),
"ISO-8859-1");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
String columnNamesList = "Bundesland;Infizierte;Tote";
// No need give the headers Like: id, Name on builder.append
builder.append(columnNamesList +"\n");
builder.append(str_data1+";");
...
It looks like you're trying to create many different CSVs? If this is not what you want and you want all the records in one CSV, then I recommend looping through the array, building a CSV with StringBuilder or some other CSV tool of which there are many, and then creating the file outside of your for loop.
I have excel spreadsheet which contain many "Chinese(zh-TW)" & "English(en-US)" word together and this data need to import to a database table with utf-8 encoding.
My first try was use Excel "Save-As" function to export data as CSV. However, the data was not able to read correctly since Excel is not allow to change the character encoding for "Save-AS" function.
After searching, I found the easy way to complete the task is following below steps:
Save the Excel sheet as "Unicode Text (.txt)" (Excel encode in UTF16 and use TAB to delimited each column.
Below is Java code snippet to read the content of this Excel unicode exported file:
File dataFile = new File("my-unicode-file-exported.txt"));
InputStreamReader isr = new InputStreamReader(new FileInputStream(dataFile), "UTF-16");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// splits by TAB
String elements = line.split("\t");
// process each element
String element_01 = elements[0];
String element_02 = elements[1];
.....
}
br.close();
I tried importing the records from a CSV file using OpenCSV. I observed that OpenCSV was actually missing out some of the entries in the last row of my file. The code is as below. I know that I can do a writeAll operation. But i need individual rows for some other operations. The number of records are around 56000 in my file.
CSVReader reader = new CSVReader(new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))));
CSVWriter writer = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path2+File.separator+fileName)))), ',');
List<String[]> fileContents = new ArrayList<String[]>();
fileContents = reader.readAll();
for(int i=0; i<fileContents.size(); i++){
String[] row = fileContents.get(i);
writer.writeNext(row);
I figured out the error. I wasnt closing the writer.
writer.close();
And the file was completely written. Don't know why it doesnt complete writing the file otherwise.
Had a similar problem as described by Abhiroop when using StatefulBeanToCsv<T> beanWriter. Thanks for documenting the error with writer.close!
Since a Writer implements Closeable which extends AutoCloseable, it should be wrapped in a try-with-resources as shown below.
For me, the advantage of StatefulBeanToCsv is that it can be used with a MappingStrategy<T>.
public void exportToCsv(List<T> beans) {
try (PrintWriter printWriter = new PrintWriter(FILE_PATH_NAME)) {
final StatefulBeanToCsv<T> beanWriter = new StatefulBeanToCsvBuilder<T>(printWriter).build();
beanWriter.write(beans);
} catch (Exception e) {
throw new IllegalStateException("export failed", e);
}
}
please check if prior to the missing values, one of the values has a comma inside (i.e. "this is a csv value, with a comma inside it").
Assuming I have a txt file located in /mypath/sampletext.txt. How do I append a new line to the beginning of the file with the following in Java while preserving the original text file's contents?:
String startStr ="--Start of File--";
Looking for a way to do this without having to create an intermediary 2nd file and make modifications only to the existing file if possible.
Read file contents first, prepend new line to that like contents = newLine + contents, then write the new conent in the same file (dont append).
well,three ways ,may help you
1.
//true: is append text to fie
FileWriter write = new FileWriter("file_path",true);
writer.write(content);
//open randomFile and "rw"
randomFile = new RandomAccessFile("file_path", "rw");
// file length
long fileLength = randomFile.length();
//point to end index
randomFile.seek(fileLength);
//write
randomFile.writeBytes(content);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent);
New answer is updated...
In this I've use few more FileIO classes & may be their one is deprecated API but If you are aware with Java FileIO classes you can easily fix it.
Here I append new line at the start of file rather than append it to the end of file..
If any issue please comment again....
Try this, I think it will help you..
try
{
//Append new line in existing file.
FileInputStream fr = new FileInputStream("onlineSoultion.txt");
DataInputStream dr = new DataInputStream(fr);
String startStr = "--Start of File--\n";
//String startStr;
while (dr.available() > 0) {
startStr += dr.readLine();
//System.out.println(startStr);
}
dr.close();
fr.close();
FileOutputStream writer = new FileOutputStream("onlineSoultion.txt");
writer.write((new String()).getBytes());
writer.close();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("onlineSoultion.txt", true)));
out.println(startStr);
out.close();
}
I am using following code to write json to my local path which i get from my html page.Again I have to construct a html page by reading content from the saved local json file.For this I have to read this saved file from local which is plain text and give as input to java file. I got confused whether to use Buffered Reader or BufferedInputStream to read that file from local path.Please help me.
java.io.BufferedWriter jsonOut = new java.io.BufferedWriter(
new java.io.OutputStreamWriter(
new java.io.FileOutputStream(uploadDir +
_req.getParameter("filename")), "ISO-8859-1"));
BufferedReader for text.
Reason: http://tutorials.jenkov.com/java-io/bufferedreader.html
You can use BufferedReader for text but you should ensure to use the proper charset in your case (otherwise it defaults to the platform charset)
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile),"ISO-8859-1"));
To read a file you can use the following code
File f = new File("your json file");
BufferedReader buf = new BufferedReader(new FileReader(f));
String line = null;
while ((line = buf.readLine()) != null) {
System.out.println("json file line " + line);
// do your changes
}