How do I write an array to .csv in Java? - java

I'm trying to write a piece of code in Java that generates an array of data, and I would like to write that data to a CSV file in one single column. However, I'm struggling with getting the correct output. My program generates the population in the array
double[] wasp = new double[1000];
which is populated by one of several functions, for instance:
for (int i = 0; i < wasp.length; i++) {
double mu = -10 + Math.random()*20;
double sigma = 0 + Math.random()*10;
wasp[i] = nextGaussian(mu, sigma);
description = "Normal";
Param1 = Double.toString(mu);
Param2 = Double.toString(sigma);
}
and I use the following code to try to write the array to CSV:
FileWriter writer = new FileWriter("C:\\Users\\Havok\\Google Drive\\Skripsie\\R\\JavaOut.csv");
for (int j = 0; j < wasp.length; j++) {
writer.append((char) wasp[j]);
writer.append(",");
}
writer.toString();
writer.flush();
writer.close();
However, when I open the CSV file, it looks "corrupt", as if the characters weren't encoded right or something. The data also fills up much more than one column in the file.
The output I expect is a CSV file that contains a single column of real values; for instance,
1.467354
0.812738
3.595733
and so on. However, what I'm getting is a column full of something like the following:
,,,,,,￶,,,,,,,￯,ï¿¿,,￾,,,,ï¿¿,,, ,,ï¿·,,￲,￲,,ï¿·,,,,,,ï¿·,,￸,￵,,,,,,￶,,,ï¿°,￸,,ï¿»,,￾,, ,,ï¿¿,,￾,,￯,,,,￵,,ï¿»,,↓,,ï¿·,￸,,,,ï¿
What am I doing wrong? I've looked at the tutorials on Java's home site and tried to adapt other similar solutions on StackOverflow, but it seems like I'm missing something crucial.

Simply convert your double to string using String.valueOf(double d) method.
FileWriter writer = new FileWriter("C:\\Users\\Havok\\Google Drive\\Skripsie\\R\\JavaOut.csv");
for (int j = 0; j < wasp.length; j++) {
writer.append(String.valueOf(wasp[j]));
writer.append("\n");
}
writer.close();
Alternatively you can use String.format() to format your double as you wanted.

In CSV format each set of data is delimited by a new line (\n) and each column is delimited by a comma. Therefore your code should look like this if you want a single column of data.
for (int j = 0; j < wasp.length; j++) {
writer.append((char) wasp[j]);
writer.append("\n");
}

Related

Using apache commons csv: I'm trying to remove a set of records from a List<CSVRecord> but it's not removing all of them/

I have a list of CSVRecords that I parsed from a file:
https://drive.google.com/file/d/0ByASC8p2MeUKZlhTdkxETXV2U3M/view?usp=sharing
using the following code:
public static List<CSVRecord> getRecords(File file) throws Exception {
Reader in = new FileReader(file);
CSVParser parser = new CSVParser(in, CSVFormat.RFC4180);
List<CSVRecord> records = parser.getRecords();
parser.close();
records.remove(0);
return records;
}
One of the columns is p(pft), and I'm trying to remove all records where p(pft) is less than 75:
(optStratCsv is the list of csvRecords)
for (int i = 0; i < optStratCsv.size(); i++) {
if (Integer.parseInt(optStratCsv.get(i).get(14)) < 75) {
optStratCsv.remove(i);
}
}
However, while it should give me a list of about 6000 records, it's giving me a list of about 9000, so clearly I did something wrong. I'd like to figure out what I did wrong.
Cheers.
Here's an idea. Don't remove the records. Instead make a copy of the records you want and return it. So if record not p(pft) is less than 75 then copy to temp CSVRecord list. Then return temp.
Each time you remove a record from optStratCsv, the size changes in your for (int i = 0; i < optStratCsv.size(); i++). Make sense?

How to skip a part of the file then read a line?

I have a code that reads a file using buffered reader and split, said file was created via a method that automatically adds 4KB of empty space at the beginning of the file, this results in when I read the following happens:
First the Code:
BufferedReader metaRead = new BufferedReader(new FileReader(metaFile));
String metaLine = "";
String [] metaData = new String [100000];
while ((metaLine = metaRead.readLine()) != null){
metaData = metaLine.split(",");
for (int i = 0; i < metaData.length; i++){
System.out.println(metaData[i]);
}
}
This is the result, keep in mind this file already exists and contains the values:
//4096 spaces then the first actual word in the document which is --> testTable2
Name
java.lang.String
true
No Reference
Is there a way to skip the first 4096 spaces, and get straight to the actual value within the file so I can get the result regularly? Because I'll be using the metaData array later in other operations, and I'm pretty sure the spaces will mess up the number of slots within the array. Any suggestions would be appreciated.
If you're using Eclipse, the auto-completion should help.
metaRead.skip(4096);
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
You could (as mentioned) simply do:
metaRead.skip(4096);
If the whitespace always occupies that many characters, or you could just avoid lines which are empty
while ((metaLine = metaRead.readLine()) != null){
if(metaLine.trim().length() > 0){
metaData = metaLine.split(",");
for (int i = 0; i < metaData.length; i++){
System.out.println(metaData[i]);
}
}
}

Including double quotes while writing CSV using apache commons in java

I am using apache commons CSV to write csv files. I want to stick to this library. While I am writing a csv file, in the first column of generated file, it contains double quotes as quote character and other columns are generated as expected.
I really want to get rid of double quotes here. Please find below code for the same.
CSVFormat format = CSVFormat.DEFAULT;
FileWriter fw = new FileWriter("Temp.csv");
CSVPrinter printer = new CSVPrinter(fw, format);
String[] temp = new String[4];
for(int i=0; i<4; i++) {
if(i==1)
temp[0] = "#";
else
temp[0] = "";
temp[1] = "hello" + (i+1);
temp[2] = "";
temp[3] = "test";
Object[] temp1 = temp[]
printer.printRecord(temp1);
}
fw.close();
printer.close();
Temp.csv
"",hello1,,test
"#",hello2,,test
"",hello3,,test
"",hello4,,test
I don't want a quote character at the beginning of every row. I just want an empty string without quotes, same as in column 3. Can anyone help?
Mentioned in lars issue tracking, try to set the CSVFormat to the following,
final CSVFormat csvFileFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE);
This is a known issue. You can vote for it in the apache commons csv issue tracker:
https://issues.apache.org/jira/browse/CSV-63

Generating a .ov2 file with Java

I am trying to figure out how to create a .ov2 file to add POI data to a TomTom GPS device. The format of the data needs to be as follow:
An OV2 file consists of POI records. Each record has the following data format.
1 BYTE, char, POI status ('0' or '2')
4 BYTES, long, denotes length of the POI record.
4 BYTES, long, longitude * 100000
4 BYTES, long, latitude * 100000
x BYTES, string, label for POI, x =3D=3D total length =96 (1 + 3 * 4)
Terminating null byte.
I found the following PHP code that is supposed to take a .csv file, go through it line by line, split each record and then write it into a new file in the proper format. I was hoping someone would be able to help me translate this to Java. I really only need the line I marked with the '--->' arrow. I do not know PHP at all, but everything other than that one line is basic enough that I can look at it and translate it, but I do not know what the PHP functions are doing on that one line. Even if someone could explain it well enough then maybe I could figure it out in Java. If you can translate it directly, please do, but even an explanation would be helpful. Thanks.
<?php
$csv = file("File.csv");
$nbcsv = count($csv);
$file = "POI.ov2";
$fp = fopen($file, "w");
for ($i = 0; $i < $nbcsv; $i++) {
$table = split(",", chop($csv[$i]));
$lon = $table[0];
$lat = $table[1];
$des = $table[2];
--->$TT = chr(0x02).pack("V",strlen($des)+14).pack("V",round($lon*100000)).pack("V",round($lat*100000)).$des.chr(0x00);
#fwrite($fp, "$TT");
}
fclose($fp);
?>
Load a file into an array, where each element is a line from the file.
$csv = file("File.csv");
Count the number of elements in the array.
$nbcsv = count($csv);
Open output file for writing.
$file = "POI.ov2";
$fp = fopen($file, "w");
While $i < number of array items, $i++
for ($i = 0; $i < $nbcsv; $i++) {
Right trim the line (remove all whitespace), and split the string by ','. $table is an array of values from the csv line.
$table = split(",", chop($csv[$i]));
Assign component parts of the table to their own variables by numeric index.
$lon = $table[0];
$lat = $table[1];
$des = $table[2];
The tricky bit.
chr(02) is literally character code number 2.
pack is a binary processing function. It takes a format and some data.
V = unsigned long (always 32 bit, little endian byte order).
I'm sure you can work out the maths bits, but you need to convert them into little endian order 32 bit values.
. is a string concat operator.
Finally it is terminated with chr(0). Null char.
$TT = chr(0x02).
pack("V",strlen($des)+14).
pack("V",round($lon*100000)).
pack("V",round($lat*100000)).
$des.chr(0x00);
Write it out and close the file.
#fwrite($fp, "$TT");
}
fclose($fp);
The key in JAVA is to apply proper byte order ByteOrder.LITTLE_ENDIAN to the ByteBuffer.
The whole function:
private static boolean getWaypoints(ArrayList<Waypoint> geopoints, File f)
{
try{
FileOutputStream fs = new FileOutputStream(f);
for (int i=0;i<geopoints.size();i++)
{
fs.write((byte)0x02);
String desc = geopoints.get(i).getName();
int poiLength = desc.toString().length()+14;
fs.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(poiLength).array());
int lon = (int)Math.round((geopoints.get(i).getLongitudeE6()/1E6)*100000);
fs.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(lon).array());
int lat = (int)Math.round((geopoints.get(i).getLatitudeE6()/1E6)*100000);
fs.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(lat).array());
fs.write(desc.toString().getBytes());
fs.write((byte)0x00);
}
fs.close();
return true;
}
catch (Exception e)
{
return false;
}
}

Nibble Hex from Java to PHP

I'm translating one app from java to php and i'm finding some trouble.
I have a string like this 98191107990D0000EF050000789C65970BCCD75318C7CFEFFC ... in java there's this function where I pass this string as parameter:
private static byte[] decodeNibbleHex(String input)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length - 1; i += 2) {
char[] bChars = new char[2];
bChars[0] = chars[i];
bChars[1] = chars[(i + 1)];
int val = Integer.decode("0x" + new String(bChars)).intValue();
baos.write((byte)val);
}
return baos.toByteArray();
}
but... i don't know to to translate this function in PHP... i tried too many times and i'm becoming crazy! i tried with a for cycle, with this eval("\$hex = 0x" . $dati[$i].$dati[$i+1] . ";"); and this $binary_string = pack("h*" , $dati[$i].$dati[$i+1]); and many many other functions...
If someone understand Java and can help me I will appreciate it!!
Thank guys!
Take a look here:
http://www.php.net/manual/de/function.hexdec.php#100578
Is this not exactly what you whrere looking for?
If my understanding is correct of your java function, it takes the string's chars in pairs, and threats them as bytes and put them in a ByteArray. In php there's no such thing as a byte array but you can represent random binary data in everyday strings. This is my take on your function (didn't tried to compare with the java code's output).
$str= '98191107990D0000EF050000789C65970BCCD75318C7CFEFFC';
$output[] = array();
for ($i=0, $c = strlen($str) - 1; $i < $c; $i+=2) {
$output[] = chr(intval($str[$i].$str[$i+1], 16));
}
print join($output); // binary string, not really useful in ascii terminal (-:
In summary this seem to be a base16_decode() function, with base16_encode() written like it follows, you get back the input string:
function base16_encode($str) {
$byteArray = str_split($str);
foreach ($byteArray as &$byte) {
$byte = sprintf('%02x', ord($byte));
}
return join($byteArray);
}
print base16_encode(join($output)); // should print back the original input.

Categories

Resources