Reputation: 887
Can we add a new column to CSV as the last column which already has let's say 3 columns with some data in it? So this new one will be added later as 4th column moreover for every row it should have random numbers.
Example,
Id Name Address Calculated
1 John U.K. 341679
2 Vj Aus 467123
3 Scott U.S. 844257
From what I understand this will require first to read csv, for loop may be to iterate to the last column and then add a new calculated column i.e Write to csv. And to add values may be the Random class of Java. But how exactly can this be done is the real question. Like a sample code would be helpful.
Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Demo1 {
public static void main(String[] args) throws IOException {
String csvFile = "C:\\MyData\\Input.csv";
String line = "";
String cvsSplitBy = ",";
String newColumn = "";
List<String> aobj = new ArrayList<String>();
/* Code to read Csv file and split */
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null)
{
String[] csvData = line.split(cvsSplitBy);
int arrayLength = csvData.length;
}
}
/* Code to generate random number */
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
}
}
Upvotes: 0
Views: 1102
Reputation: 887
Based on @Plirkee sample code and his help I made a final working code. Sharing it here so that it might be useful for someone with a similar requirement.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Demo1 {
public static String getRandomNumber() {
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) // length of the random string.
{
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
return finaldata;
}
public static void main(String[] args) throws IOException {
File sourceCsvFile = null;
File finalCsvFile = null;
// String sourceCsvFileName = "";
sourceCsvFile = new File("C:\\MyData\\Input.csv");
finalCsvFile = new File("C:\\MyData\\Input_1.csv");
String line = "";
String cvsSplitBy = ",";
BufferedWriter writer = new BufferedWriter(new FileWriter(finalCsvFile));
try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile))) // read the actual Source downloaded csv file
{
line = br.readLine(); // read only first line
String newFileLine = line + cvsSplitBy + "HashValue"; // append "," and new column <HashValue>
writer.write(newFileLine); // will be written as first line in new csv
writer.newLine(); // go to next line for writing next lines
while ((line = br.readLine()) != null) // this loop to write data for all lines except headers
{
newFileLine = line + cvsSplitBy + getRandomNumber(); // will add random numbers for each row
writer.write(newFileLine);
writer.newLine();
}
}
writer.close();
if(finalCsvFile.exists() && finalCsvFile.length() > 0)
{
System.out.println("New File with HashValue column created...");
if(sourceCsvFile.delete())
{
System.out.println("Old File deleted successfully...");
}
else
{
System.out.println("Failed to delete the Old file...");
}
}
else if (!finalCsvFile.exists())
{
System.out.println("New File with HashValue column not created...");
}
}
}
Upvotes: 0
Reputation: 3841
Great, so based on the code you provide, this could look like the following
(just to give you the idea - I write it here on the fly without testing...) :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Demo1 {
//moved your random generator here
public static String getRandomNumber() {
/* Code to generate random number */
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
return finaldata;
}
public static void main(String[] args) throws IOException {
String csvFile = "C:\\MyData\\Input.csv";
String temporaryCsvFile = "C:\\MyData\\Output_temp.csv";
String line = "";
String cvsSplitBy = ",";
String newColumn = "";
List<String> aobj = new ArrayList<String>();
/* Code to read Csv file and split */
BufferedWriter writer = new BufferedWriter(new FileWriter(
temporaryCsvFile));
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null)
{
//String[] csvData = line.split(cvsSplitBy);
//int arrayLength = csvData.length;
//actually you don't even need to split anything
String newFileLine = line + cvsSplitBy + getRandomNumber();
// ... We call newLine to insert a newline character.
writer.write(newFileLine);
writer.newLine();
}
}
writer.close();
//Now delete the old file and rename the new file
//I'll leave this to you
}
}
Upvotes: 2