So for my visualization class, i need to, using the provided .csv data set: 1. o
ID: 3743218 • Letter: S
Question
So for my visualization class, i need to, using the provided .csv data set:
1. open and read the dataset.)
2. reduce the number of data items to half of the original number or smaller than 100MB.
3. save the reduced data in a new file.
I've done one, and know how to do three but I do not know how to do two. The language I am using to code is java. If you could give me detailed inscructions or examples on how to reduce the .csv size it would be greatly appricated. (more information if needed upon request)
Explanation / Answer
My understanding of the part 2 is that, if the csv size is greater than 100 mb, then write data into a new file until the size of the new file is less than 100 mb, else write half of the csv data into the new csv.
PROGRAM:
import java.io.*;
import java.util.*;
class csv_write
{
public static void main(String[] args) throws IOException
{
/* Path of the csv */
String path_of_csv = "/Users/kandy/desktop/data.csv";
/* Path of the new csv */
String path_of_new_csv = "/Users/kandy/desktop/new.csv";
File f = new File(path_of_csv);
/* For getting number of lines in the file */
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(f));
lineNumberReader.skip(Long.MAX_VALUE);
int number_of_lines = lineNumberReader.getLineNumber() + 1;
lineNumberReader.close();
/* Size of the csv in bytes */
double bytes = f.length();
double kilobytes = bytes/1024;
int megabytes = (int) (kilobytes/1024);
/* Creating an list where the items are stored and written to the new file */
List lines = new ArrayList();
/* A FileWriter to write content into the new csv */
FileWriter w = new FileWriter(path_of_new_csv);
/* Used for reading lines from csv and getting current line number */
LineNumberReader line = new LineNumberReader(new FileReader(f));
/* Checking if the csv size is greater than 100 mb */
if( megabytes>100) // If csv size is greater than 100 mb
{
/* Writing content to the new csv until it's size is less than 100 mb */
while( ( (int)(new File(path_of_new_csv).length()/1024/1024) < 100 ) )
{
/* If EOF is reached */
if(line.getLineNumber() == number_of_lines)
{
break;
}
/* If EOF is not reached */
lines.add(line.readLine());
}
}
/* If the csv is less than 100 mb */
else
{
/* Writing half of the csv into new csv */
while (line.getLineNumber() < (number_of_lines/2) )
{
lines.add(line.readLine());
}
}
/* Writing the list contents to the new csv */
for(Object s: lines) w.write(String.valueOf(s)+" ");
line.close();
w.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.