Total members 11816 |It is currently Tue Feb 18, 2020 1:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Read and write CSV file, this code read one csv file and then for each four lines, it does a transpose, and then save the results.
java code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package commandssvn;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
* @author samy
*/
public class WriteCSV1 {

public static void main(String[] args) throws IOException {
generateCsvFile();
run();

}
public static void run() {

String csvFile = "d:/Files_Data2/Results3.csv";
String[][] arrayStrings=new String[4][6];

BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";


try {



br = new BufferedReader(new FileReader(csvFile));


boolean firstTime=false;
int i=0;
while ((line = br.readLine()) != null) {
firstTime=false;

arrayStrings[i] = line.split(cvsSplitBy);

i++;
if(i==4)
{
i=0;
generateCsvFile(arrayStrings);
}

}



} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}


private static void generateCsvFile()
{
try
{
FileWriter writer = new FileWriter("D:/xyu.csv",false);

writer.append(" ");
writer.append(',');

writer.append("Estimate");
writer.append(',');
writer.append("StdError");
writer.append(',');
writer.append("zValue");
writer.append(',');
writer.append("Prz");
writer.append('\n');


//generate whatever data you want

writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}


private static void generateCsvFile(String[][] values)
{
try
{
FileWriter writer = new FileWriter("D:/xyu.csv",true);


writer.append("Intersect");
writer.append(',');

writer.append(values[0][1]);
writer.append(',');
writer.append(values[1][1]);
writer.append(',');
writer.append(values[2][1]);
writer.append(',');
writer.append(values[3][1]);
writer.append(',');
writer.append(values[1][5]);


writer.append('\n');

writer.append("NewFeatures");
writer.append(',');

writer.append(values[0][2]);
writer.append(',');
writer.append(values[1][2]);
writer.append(',');
writer.append(values[2][2]);
writer.append(',');
writer.append(values[3][2]);
writer.append(',');
writer.append(values[1][5]);




writer.append('\n');

writer.append("Improvements");
writer.append(',');
writer.append(values[0][3]);
writer.append(',');
writer.append(values[1][3]);
writer.append(',');
writer.append(values[2][3]);
writer.append(',');
writer.append(values[3][3]);
writer.append(',');
writer.append(values[1][5]);


writer.append('\n');

writer.append("SqrCommits");
writer.append(',');
writer.append(values[0][4]);
writer.append(',');
writer.append(values[1][4]);
writer.append(',');
writer.append(values[2][4]);
writer.append(',');
writer.append(values[3][4]);
writer.append(',');
writer.append(values[1][5]);

writer.append('\n');

//generate whatever data you want

writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}

}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Why did the result becomes like this

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package commandssvn;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * * @author samy */public class WriteCSV1 { public static void main(String[] args) throws IOException { generateCsvFile(); run(); } public static void run() { String csvFile = "d:/Files_Data2/Results3.csv"; String[][] arrayStrings=new String[4][6]; BufferedReader br = null; String line = ""; String cvsSplitBy = ","; try { br = new BufferedReader(new FileReader(csvFile)); boolean firstTime=false; int i=0; while ((line = br.readLine()) != null) { firstTime=false; arrayStrings[i] = line.split(cvsSplitBy); i++; if(i==4) { i=0; generateCsvFile(arrayStrings); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static void generateCsvFile() { try { FileWriter writer = new FileWriter("D:/xyu.csv",false); writer.append(" "); writer.append(','); writer.append("Estimate"); writer.append(','); writer.append("StdError"); writer.append(','); writer.append("zValue"); writer.append(','); writer.append("Prz"); writer.append('\n'); //generate whatever data you want writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } private static void generateCsvFile(String[][] values) { try { FileWriter writer = new FileWriter("D:/xyu.csv",true); writer.append("Intersect"); writer.append(','); writer.append(values[0][1]); writer.append(','); writer.append(values[1][1]); writer.append(','); writer.append(values[2][1]); writer.append(','); writer.append(values[3][1]); writer.append(','); writer.append(values[1][5]); writer.append('\n'); writer.append("NewFeatures"); writer.append(','); writer.append(values[0][2]); writer.append(','); writer.append(values[1][2]); writer.append(','); writer.append(values[2][2]); writer.append(','); writer.append(values[3][2]); writer.append(','); writer.append(values[1][5]); writer.append('\n'); writer.append("Improvements"); writer.append(','); writer.append(values[0][3]); writer.append(','); writer.append(values[1][3]); writer.append(','); writer.append(values[2][3]); writer.append(','); writer.append(values[3][3]); writer.append(','); writer.append(values[1][5]); writer.append('\n'); writer.append("SqrCommits"); writer.append(','); writer.append(values[0][4]); writer.append(','); writer.append(values[1][4]); writer.append(','); writer.append(values[2][4]); writer.append(','); writer.append(values[3][4]); writer.append(','); writer.append(values[1][5]); writer.append('\n'); //generate whatever data you want writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } }}

what extension name should be use?



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

csv files.


_________________
Sami
PHD student - SAIL - School Of Computing
Queens' University
Canada


Author:
Site Admin
User avatar Posts: 33
Have thanks: 1 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Read and write CSV file
 Read and Write to file using ASP     -  
 File write read     -  
 Write and Read to File In Java Example     -  
 How to read and write to CSV file from python     -  
 Write to file using php     -  
 Write Vector list to File     -  
 read from file in C++     -  
 Read csv file     -  
 Read Binary File in C++     -  
 javascript read file     -  



Topic Tags

Java Files and I/O






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com