I wrote a small Java program that reads the data from CSV file and I have to save these values in 2 dimensional array. Now I need to write these values (not all information will be stored because the array contains many redundant data) from this array to a new CSV file. Can anyone help me with a code sample, I searched a lot but could not find an answer. My code is:
int row = 0;
int col = 0;
String[][] numbers=new String[24][24];
File file = new File("D:\\thesis\\sorted_file.csv");
if(file.exists())
{
System.out.println("file exist");
}
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;
String delims=",";
//read each line of text file
while((line = bufRdr.readLine()) != null )
{
StringTokenizer st = new StringTokenizer(line,delims);
col=0;
while (st.hasMoreTokens())
{
//get next token and store it in the array
numbers[row][col] = st.nextToken();
System.out.print("number["+row+"]["+col+"]:"+numbers[row][col]);
col++;
}
row++;
}
You can use something like this...
BufferedWriter br = new BufferedWriter(new FileWriter("myfile.csv"));
StringBuilder sb = new StringBuilder();
for (String element : numbers) {
sb.append(element);
sb.append(",");
}
br.write(sb.toString);
br.close();
Related
I have a CSV file which contains almost 10000 lines of data. I want to split that file into 10 different CSV file based on the total line count, so that each file can contain 1000 lines of data in the order first file should have 1-1000 lines, second file should have 1001-2000 lines and so on. Also, each of those 10 different CSV file should only contain the data from the first column of the parent CSV file. The code which I developed writes the same data (1.e 1-1000 lines) to all of the 10 csv files. I am unable to figure out what is the mistake in the code.
for (int j=1;j<=files;j++){
String inputfile = "C:/Users/Downloads/File.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_"+j+".csv");
BufferedWriter out = new BufferedWriter(fstream1);
String strLine = null;
for (i=i+1;i<=(j*lines);i++) { //I Have declared i as static int i = 0 and have already calculated lines and files in other part of code
strLine = br.readLine();
if (strLine!= null) {
String strar[] = strLine.split(",");
out.write(strar[0]);
if(i!=(j*lines)) {
out.newLine(); }
}
}
out.close();
Use this code
import java.io.*;
import java.util.Scanner;
public class csvfilesplit
{
public static void main(String[] args) throws IOException {
int split;
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("\n Enter The count to split each file :-----------");
int s = reader.nextInt();
File folder = new File("file/"); //*** Location of your file
int filecount = 0;
for (File fo :
folder.listFiles()) {
if (fo.isFile()) {
filecount++;}
}
System.out.println("Total source file count is :----------------------- "+filecount+"\n"); //*** Total numbr of orginal file in mentioned folder
String path = folder.getAbsolutePath();
// System.out.println("location=-----------"+path);
File[] listOfFiles = folder.listFiles();
for (int l = 0; l < listOfFiles.length; l++) {
if (listOfFiles[l].isFile()) {
System.out.println("File name Is :-------------------------- " + listOfFiles[l].getName()); //*** File name
BufferedReader bufferedReader = new BufferedReader(new FileReader(path+"/"+listOfFiles[l].getName())); // Read a souce file
String input;
int count = 0;
while((input = bufferedReader.readLine()) != null)
{
count++;
}
System.out.println("File total rows count is :-------------- "+count); //*** Number of row count in the file
split= count/s;
int n = split,z=0;
if(n!=z)
{
System.out.println("Each splitted file line count is :------ "+split+"\n"); //*** After splitted file have the rows count
FileInputStream fstream = new FileInputStream(path+"/"+listOfFiles[l].getName()); DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;
for (int j=1;j<=s;j++)
{
File dir = new File(path+"/"+"CSV_DATA_"+j);
dir.mkdir();
File filefolder = new File(path+"/"+"CSV_DATA_"+j);
String folderpath = filefolder.getAbsolutePath();
//********Destination File Location******
FileWriter fstream1 = new FileWriter(folderpath+"/"+listOfFiles[l].getName()+".csv"); //*** Splitted files and file format(.txt/csv.....)
BufferedWriter out = new BufferedWriter(fstream1);
for (int i=1;i<=n;i++)
{
strLine = br.readLine();
if (strLine!= null)
{
out.write(strLine);
if(i!=n)
{
out.newLine();
}
}
}
out.close();
}
in.close();
}
else
{// Below N number of row in this file
System.out.println("\n******************************* Mentioned this file have below - "+s+" rows ****************************** "+listOfFiles[l].getName()+" \n");}
}
}
System.out.println("\n Splitted_CSV_files stored location is : "+path);
}
}
The problem of having same lines in each of 10 csv files is because of the line below in method myFunction
BufferedReader br = new BufferedReader(new FileReader(inputfile));
The logic using variables i,j,lines works perfectly. But every time myFunction is called, br (BufferedReader for input file) is initialized again.
So br.readLine() will start reading from start. And thus having same 1000 lines in each of the 10 csv files.
Hope it helps!
Please find the Below code:-
public static void main(String[] args) throws IOException {
//first read the file
String inputfile = "C:/Users/bohrahas/Desktop/SampleCSVFile.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
//create thje first file which will have 1000 lines
File file = new File("C:/Users/bohrahas/Desktop/FileNumber_"+1+".csv");
FileWriter fstream1 = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream1);
String line="";
//count the number of line
int count=1;
int file_Number=2;
while ((line = br.readLine()) != null)
{
//if the line is divided by 1000 then create a new file with file count
if(count % 1000 == 0)
{
File newFile = new File("C:/Users/bohrahas/Desktop/FileNumber_"+file_Number+".csv");
fstream1 = new FileWriter(newFile);
file_Number++;
out = new BufferedWriter(fstream1);
}
if(line.indexOf(",")!=-1)
line=line.substring(0, line.indexOf(","));
out.write(line);
out.newLine();
count++;
}
}
Logic :-
You don't have to read the parent file for every loop. Just load it once i.e create a object once and then process the parent file.
While reading every line of parent get the whole line and just remove the columns except first column.
Till the first "," is always a first column. so remove the string after '',"
if the line traversed count is divided completely by 1000 i.e. 2000,3000,4000....etc. create a new file and create a BufferWriter for that.
Your logic is very bad here. I rewrote the whole code for you,
import java.io.*;
import java.util.Scanner;
public class FileSplit {
public static void myFunction(int lines, int files) throws FileNotFoundException, IOException{
String inputfile = "file.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile)); //reader for input file intitialized only once
String strLine = null;
for (int i=1;i<=files;i++) {
FileWriter fstream1 = new FileWriter("FileNumber_"+i+".csv"); //creating a new file writer.
BufferedWriter out = new BufferedWriter(fstream1);
for(int j=0;j<lines;j++){ //iterating the reader to read only the first few lines of the csv as defined earlier
strLine = br.readLine();
if (strLine!= null) {
String strar[] = strLine.split(",");
out.write(strar[0]); //acquring the first column
out.newLine();
}
}
out.close();
}
}
public static void main(String args[])
{
try{
int lines = 2; //set this to whatever number of lines you need in each file
int count = 0;
String inputfile = "file.csv";
File file = new File(inputfile);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) { //counting the lines in the input file
scanner.nextLine();
count++;
}
System.out.println(count);
int files=0;
if((count%lines)==0){
files= (count/lines);
}
else{
files=(count/lines)+1;
}
System.out.println(files); //number of files that shall eb created
myFunction(lines,files);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
I want to read csv file with BufferedReader and split with StringTokenizer. However there is one line in file which contains this line :
ide,12,office,,3208.83,0.18,577.5,4876
Also here is my read method;
public void readFromCSV(){
try {
File file = new File(myFile);
br = new BufferedReader(new FileReader(file));
String line = null;
while((line = br.readLine()) != null) {
st1 = new StringTokenizer(line,"\n");
while(st1.hasMoreTokens()) {
oneLineList = new ArrayList<>();
st2 = new StringTokenizer(st1.nextToken(),",");
for(int i = 0; i < 8; i++) {
oneLineList.add(st2.nextToken());
}
dataList.add(counter,oneLineList);
}
counter++;
}
br.close();
}catch(IOException ex) {
ex.printStackTrace();
}
}
In for statement, there are 8 fields in each line, and dataList is a two-dimensional array list.
I cannot read whole data because of one line contain consecutive coma, how should I do for fix this?
I'm trying to convert a CSV file into a 2D array however I'm having problems. The code seems to only print out first name in the CSV file 10 times and then gives me an out of bounds exception. E.g if Player name was Rob, it will just print out Rob over and over again.
If any more clarification needed just ask
try{
int col = 0, row = 0;
System.out.println("Reading " + fileName + " ...");
fr = new FileReader(fileName);
bf = new BufferedReader(fr);
String line = bf.readLine();
while (line != null) {
StringTokenizer st = new StringTokenizer(line,",");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
data[row][col] = st.nextToken();
col++;
}
col = 0;
row++;
}
}catch(IOException e){
System.err.println("Cannot find: "+fileName);
}
I think your code reads just one line. Continue the while loop using the following code snippet -
String line = bf.readLine();
while ((line = br.readLine()) != null) {
//place your code here
}
In the above code BufferedReader - br is now ready to read the next line and it continues until the br.readline() returns a null.
Hope it will help.
Thanks a lot.
I want to read a .txt file via a Java program.
Say this is the text file input.txt
abc, test, 1,2,3
abc
abcd
test, 1, 2, 3
Each line represents a row, each comma-separated value represents a column.
Currently my code is:
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int num = readLines(); //this function just returns the number of lines
for (int i = 0; i < num; i++){
textData[i] = br.readLine();
}
br.close();
This outputs the text file as it was shown above if I print the array. But I require to insert into the array split by comma(could be other character as well, just use comma for now). So this means the output will be such that [abc,test,1,2,3],[abc],[abcd],[test,1,2,3] in the array. How should I proceed?
Thanks for the reply. Update:
Since i got my txt file into a array list,
[abc,test,1,2,3]
[abc]
[abcd]
[test,1,2,3]
How do i find the number of elements in each line?
Create an array list of array lists:
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int lineCount = readLines();
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>(lineCount);
for (int i = 0; i < lineCount; i++) {
ArrayList<String> row = new ArrayList<String>();
String line = br.readLine();
for(String s: line.split(",")) {
row.add(s);
}
rows.add(row);
}
br.close();
I have a method that takes data from a .csv file and puts it into an array backwards
(first row goes in last array slot) however I would like the first row in the .csv file to not be in the array. How would I accomplish this? Here is my code thus far:
public static String[][] parse(String symbol) throws Exception{
String destination = "C:/"+symbol+"_table.csv";
LineNumberReader lnr = new LineNumberReader(new FileReader(new File(destination)));
lnr.skip(Long.MAX_VALUE);
String[][] stock_array = new String[lnr.getLineNumber()][3];
try{
BufferedReader br = new BufferedReader(new FileReader(destination));
String strLine = "";
StringTokenizer st = null;
int line = lnr.getLineNumber()-1;
while((strLine = br.readLine()) != null){
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens()){
stock_array[line][0] = st.nextToken();
st.nextToken();
stock_array[line][1] = st.nextToken();
stock_array[line][2] = st.nextToken();
st.nextToken();
st.nextToken();
st.nextToken();
}
line--;
}
}
catch(Exception e){
System.out.println("Error while reading csv file: " + e);
}
return stock_array;
}
You can skip the first line by just reading it in and doing nothing. Do this just before your while loop:
br.readLine();
To make sure that your array is the right size and lines get stored in the right places, you should also make these changes:
String[][] stock_array = new String[lnr.getLineNumber()-1][3];
...
int line = lnr.getLineNumber()-2;
Your code is not efficient, as far as my knowledge goes. Also, you are using linenumberreader.skip(long.max_value), which is not a correct/confirmed way to find the line count of the file. StringTokenizer is kind of deprecated way of splitting tokens. I would code it, in the following way:
public static List<String[]> parse(String symbol) throws Exception {
String destination = "C:/"+symbol+"_table.csv";
List<String[]> lines = new ArrayList<String[]>();
try{
BufferedReader br = new BufferedReader(new FileReader(destination));
int index = 0;
while((line = br.readLine()) != null){
if(index == 0) {
index++;
continue; //skip first line
}
lines.add(line.split(","));
}
if(lines != null && !lines.isEmpty()) {
Collections.reverse(lines);
}
} catch(IOException ioe){
//IOException Handling
} catch(Exception e){
//Exception Handling
}
return lines;
}