Read a CSV file and Write a Json file in Java - java

I modified my code:
private static final String SAMPLE_CSV_FILE_PATH = "src/main/resources/testCSV.csv";
private static final String OUT_PUT_CSV_PATH = "src/main/resources/outCSV.csv";
public static void main(String[] args) throws IOException {
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
CSVReader csvReader = new CSVReader(reader);
List<String[]> records = csvReader.readAll();
Writer writer = Files.newBufferedWriter(Paths.get(OUT_PUT_CSV_PATH));
CSVWriter out = new CSVWriter(writer);
int i = 1;
int total = 0;
while(i < records.size()){
String[] result = records.get(i);
for(int j =1; j<= Integer.parseInt(result[1]); j++){
String pattern="00000";
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(j);
writer.append(result[0]+output+"\n");
total++;
}
i++;
}
out.flush();
out.close();
System.out.println(total);
}
Now I am using the first CSV file to generate the serial number, Something like:
NAIS00001
NAIS00002
...
NAIS00625
Then I write these serial numbers into a new CSV file. But there is only one column. 6 millions data in one column... How can I star a new column?

Your Filewriter is not writing in append mode, so your file is being overwritten each time it goes through the outer loop. It's not a problem with the file size.
Try this:
FileWriter fileWriter = new FileWriter("src/main/resources/testOutPut.json", true);
Documentation

Related

Append column in csv file at specific index using java

I have a csv file which contains some data
e.g:
Name, Age
John, 25
Joe, 26
I want to add a new column after the age. I tried a lot and here's my code
public static void main(String args[]) throws IOException {
BufferedWriter br = new BufferedWriter(new FileWriter("D:\\text1.csv",true));
StringBuilder sb = new StringBuilder();
String id[] = {"3","4"};
for (int i = 0; i < id.length; i++) {
sb.append(id[i] + "\t");
sb.append("\n");
}
br.write(sb.toString());
br.close();
}
The values of String[id] is getting append but below 1st column. Also is there any way to add a header ?

Reading data from file without manually inputing the length it the array

Please I will like to adjust this code that reads integers from a file.
I will like the code to detect the number (n) of the dataset instead of having to put in figures manually as done below (4000 )
double[] tall = new double[4000];
public class Extracto {
public static void main(String[] args) throws IOException {
File fil = new File("C:\\Users\\Desktop\\kaycee2.csv");
FileReader inputFil = new FileReader(fil);
BufferedReader in = new BufferedReader(inputFil);
double[] tall = new double[4000];
String s = in.readLine();
int i = 0;
while (s != null) {
// Skip empty lines.
s = s.trim();
if (s.length() == 0) {
continue;
}
tall[i] = Double.parseDouble(s); // This is line 19.
// System.out.println(tall[i]);
s = in.readLine();
i++;
}
I am expecting the adjusted code to obtain the data length without manually putting it in like in as shown in the code below for the 4000 length.
double[] tall = new double[4000];
As Thomas mentioned, use a list, instead of an array.
File fil = new File("C:\\Users\\Desktop\\kaycee2.csv");
FileReader inputFil = new FileReader(fil);
BufferedReader in = new BufferedReader(inputFil);
ArrayList<Double> tall = new ArrayList<>();
while(in.ready()){
String s = in.readLine().trim();
if(!s.isEmpty()){
tall.add(Double.parseDouble(s);
}
}
your codes can be further compacted if you use a list.
also do add a try-catch in the event when the String read is not a number.

Writing an ArrayList to BufferedWriter using charArray()

The code snippet splits a CSV file into multiple CSV files and writes the first column content to the child CSV files. What I observed with this code is the column header "UNIQUE ID" is only appearing in FIRST CSV file. The following CSV files only contains data without the header. In order to get header to all files I thought of using an ArrayList so that I can put the header at the first index of ArrayList and rest of data afterwards. But I failed miserably.
I require suggestion or help for how to modify the code so that all the child files should have an additional UNIQUE IDENTIFIER row along as the first row with the column data. I am pasting the code which I tried and didn't work. Child csv should look like this This is what I am getting
public static void myFunction(int lines, int files) throws FileNotFoundException, IOException {
String inputfile = "C:/Users/Downloads/CONSOLIDATED.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
String strLine = null;
for (int i = 1; i <= files; i++) {
FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_" + i + ".csv");
BufferedWriter out = new BufferedWriter(fstream1);
for (int j = 0; j < lines; j++) {
strLine = br.readLine();
if (strLine != null) {
String strar[] = strLine.split(",");
ArrayList<String> al=new ArrayList<String>();
al.add(0,"Unique Identifier");
al.add(1,strar[0]);
char c[] = al.toString().toCharArray();
out.write(c);
out.newLine();
}
}
out.close();
}
br.close();
}
Your problem is that you are not keeping the headers out of the loops. Try something reading the first line before the main loop and store the headers in the List. then, every time you create a new file, before starting the inner loop, write the header in the first line of each file.
public static void myFunction(int lines, int files) throws FileNotFoundException, IOException {
String inputfile = "C:/Users/Downloads/CONSOLIDATED.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
String strLine = br.readLine(); //here you have the headers
String[] headers=strLine.split(",");
for (int i = 1; i <= files; i++) {
FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_" + i + ".csv");
BufferedWriter out = new BufferedWriter(fstream1);
out.write(headers[0]);
for (int j = 0; j < lines; j++) {
out.newLine();
strLine = br.readLine();
if (strLine != null) {
String strar[] = strLine.split(",");
out.write(strar[0]);
}
}
out.close();
}
br.close();
}

Read & split multiple column text file into arrays

For a project I'm working on a fairly big animal dataset with up to 14 parameters of data. I was able to read it in and display it as strings using this:
public static void readIn(String file) throws IOException {
Scanner scanner = new Scanner(new File(file));
while (scanner.hasNext()) {
String[] columns = scanner.nextLine().split("/t");
String data = columns[columns.length-1];
System.out.println(data);
}
}
and displaying something like this:
04:00:01 0.11 0.04 -0.1 1047470 977.91 91.75
04:00:01 0.32 -0.03 -0.07 1047505 977.34 92.91
04:00:01 0.49 -0.03 -0.08 1047493 978.66 92.17
But I'm currently having trouble trying to split each column into separate arrays so that I can process the data (e.g. calculating means). Any idea of how I can do this? Any help would be much appreciated.
Edit: thanks, I've found out a solution that works and also lets me choose which channel it reads specifically. I've also decided to store the data as arrays within the class, here's what I have now:
public static void readChannel(String file, int channel) throws IOException
{
List<Double> dataArr = new ArrayList<>();
Scanner scanner = new Scanner(new File(file));
while (scanner.hasNext()) {
String[] columns = scanner.nextLine().split("\t");
for (int i = channel; i < columns.length; i+=(columns.length-channel)) {
dataArr.add(Double.parseDouble(columns[i]));
dataArr.toArray();
}
}
}
You can store all rows in an ArrayList and then create arrays for each column and store values in them. Sample code:
Scanner scanner = new Scanner(new File(file));
ArrayList<String> animalData = new ArrayList<String>();
while (scanner.hasNext()) {
String[] columns = scanner.nextLine().split("/t");
String data = columns[columns.length-1];
animalData.add(data);
System.out.println(data);
}
int size = animalData.size();
String[] arr1 = new String[size]; String[] arr2 = new String[size];
String[] arr3 = new String[size]; String[] arr4 = new String[size];
for(int i=0;i<size;i++)
{
String[] temp = animalData.get(i).split("\t");
arr1[i] = temp[0];
arr2[i] = temp[1];
arr3[i] = temp[2];
arr4[i] = temp[3];
}
I think you should split your problem in 2:
File reading:
Your program read each line and save it inside a instance of a class defined by you:
public class MyData {
private String time;
private double percent;
//... and so on
}
public MyData readLine( String line ) {
String[] columns = line.split("\t");
MyData md = new MyData();
md.setTime( columns[ 0 ] );
md.setPercent( Double.parseDouble(columns[ 1 ]) );
}
public void readFile( File file ) {
Scanner scanner = new Scanner(file);
List<MyData> myList = new ArrayList<>();
while (scanner.hasNext()) {
MyData md = readLine( scanner.nextLine() );
myList.add( md );
}
}
Data processing:
After you processed your file, you can create the method you need to process the data:
int sum = 0;
for ( MyData md : myList ) {
sum = sum + md.getValue();
}
I hope it help.
Following snippet will list down all values for a given index
public static void readIn(String file) throws Exception {
Scanner scanner = new Scanner(new File(file));
final Map<Integer,List<String>> resultMap = new HashMap<>();
while (scanner.hasNext()) {
String[] columns = scanner.nextLine().split("/t");
for(int i=0;i<columns.length;i++){
resultMap.computeIfAbsent(i, k -> new ArrayList<>()).add(columns[i]);
}
} resultMap.keySet().forEach(index -> System.out.println(resultMap.get(index).toString()));}

Take values from a text file and put them in a array

For now in my program i am using hard-coded values, but i want it so that the user can use any text file and get the same result.
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
public class a1_12177903
{
public static void main(String [] args) throws IOException
{
if (args[0] == null)
{
System.out.println("File not found");
}
else
{
File file = new File(args[0]);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
while (br.ready())
{
line += br.readLine();
}
String[] work = line.split(",");
double[] doubleArr = new double[work.length];
for (int i =0; i < doubleArr.length; i++)
{
doubleArr[i] = Double.parseDouble(work[i]);
}
double maxStartIndex=0;
double maxEndIndex=0;
double maxSum = 0;
double total = 0;
double maxStartIndexUntilNow = 0;
for (int currentIndex = 0; currentIndex < doubleArr.length; currentIndex++)
{
double eachArrayItem = doubleArr[currentIndex];
total += eachArrayItem;
if(total > maxSum)
{
maxSum = total;
maxStartIndex = maxStartIndexUntilNow;
maxEndIndex = currentIndex;
}
if (total < 0)
{
maxStartIndexUntilNow = currentIndex;
total = 0;
}
}
System.out.println("Max sum : "+ maxSum);
System.out.println("Max start index : "+ maxStartIndex);
System.out.println("Max end index : " +maxEndIndex);
}
}
}
I've fixed it so it takes in the name of the text file from the command line. if anyone has any ways to improve this, I'll happily accept any improvments.
You can do this with Java8 Streams, assuming each entry has it's own line
double[] doubleArr = Files.lines(pathToFile)
.mapToDouble(Double::valueOf)
.toArray();
If you were using this on production systems (rather than as an exercise) it would be worth while to create the Stream inside a Try with Resources block. This will make sure your input file is closed properly.
try(Stream<String> lines = Files.lines(path)){
doubleArr = stream.mapToDouble(Double::valueOf)
.toArray();
}
If you have a comma separated list, you will need to split them first and use a flatMap.
double[] doubleArr = Files.lines(pathToFile)
.flatMap(line->Stream.of(line.split(","))
.mapToDouble(Double::valueOf)
.toArray();
public static void main(String[] args) throws IOException {
String fileName = "";
File inputFile = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(inputFile));
// if input is in single line
StringTokenizer str = new StringTokenizer(br.readLine());
double[] intArr = new double[str.countTokens()];
for (int i = 0; i < str.countTokens(); i++) {
intArr[i] = Double.parseDouble(str.nextToken());
}
// if multiple lines in input file for a single case
String line = "";
ArrayList<Double> arryList = new ArrayList<>();
while ((line = br.readLine()) != null) {
// delimiter of your choice
for (String x : line.split(" ")) {
arryList.add(Double.parseDouble(x));
}
}
// convert arraylist to array or maybe process arrayList
}
This link may help: How to use BufferedReader. Then you will get a String containing the array.
Next you have several ways to analyze the string into an array.
Use JSONArray to parse it. For further information, search google for JSON.
Use the function split() to parse string to array. See below.
Code for way 2:
String line="10,20,50";//in fact you get this from file input.
String[] raw=line.split(",");
String[] arr=new String[raw.length];
for(int i=0;i<raw.length;++i)arr[i]=raw[i];
//now arr is what you want
Use streams if you are on JDK8. And please take care of design principles/patterns as well. It seems like a strategy/template design pattern can be applied here. I know, nobody here would ask you to focus on design guidelines.And also please take care of naming conventions. "File" as class name is not a good name.

Categories