Merging csv file - java

Please help me in java code. I have 2 CSV FILE
a.csv contains "zip","name","place"
b.csv contains "zip","latitude","longitude"
I need to merge the following columns with the same zip and save in another csv file
output.csv file will be:
"zip","name","place","latitude","longitude"
how am i going to code this? thank you.
I have read this a.csv but i dont know how to merge.
Here's my code in reading my first csv file
public class Merge{public static void main(String[]args ) throws Exception {
String csvFile ="a.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try{
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] cFile = line.split(cvsSplitBy);
System.out.println("" + cFile[1] + " " + cFile[2] + " ");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System. out. println("Done");
}
}

I think you should write the below logic to achieve this. This is just an idea.. go ahead and implement if you get any error in any specific area. You ware welcome to get back with your questions.
Read csv 1, populate an array (array 1)
Read csv 2, populate another array (array 2)
Loop both the array check the zip and merge in 3rd array.. something like below
for(loop over array 1)
// get the zip from array1
for(loop over array 2)
// get the zip from array2
if(zip1 == zip2)
then merge and put the data in another array(array3) and break this loop
Write this array3 in a new csv file

Related

parse csv format file into java arrays for generating ldif format file

I am a rookie in java coding area,
I am trying to parse a csv format file, split with only commas(,)
this file includes users' account names and true names,
for example, it looks like: tom123,Tom Halland,kelly02,Kelly Chen,..and so on,
I want to parse these user's data into something like arrays using java,
so I can reuse this array, then to generate ldif format file and import it into LDAP server to create accounts automatically,
is there any easier way to do it? or any technical advises for me?
Thanks a lot!
I am sharing code snippet from one of my previous assignments. Basically code does following tasks:
1) Read CSV file line by line
2) Split each line into token with help of predefined characher(in case of csv it's ',')
3) Generate a string form of record in desired ldif format
4) write record to output file
String inputCSVFile = "/input_folder_path/sample.csv";
BufferedReader bufferedReader = null;
String lines = "";
String splitChar = ",";
String[] columns;
int count = 0;
try {
PrintStream printStream = new PrintStream(new FileOutputStream("/output_folder_path/e.ldif"));// Step 1
bufferedReader = new BufferedReader(new FileReader(inputCSVFile));
while ((lines = bufferedReader.readLine()) != null) {
columns = lines.split(splitChar);// Step 2
if (count > 0) {// Step 3 ,4
printStream.println("dn: cn="+columns[1]+", ou="+columns[2]+", o=Data"
+ "\ngivenName: "+columns[0]
+ "\nsn: "+columns[3]
+ "\n"
);
}
count++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Split file into multiple files

I want to cut a text file.
I want to cut the file 50 lines by 50 lines.
For example, If the file is 1010 lines, I would recover 21 files.
I know how to count the number of files, the number of lines but as soon as I write, it's doesn't work.
I use the Camel Simple (Talend) but it's Java code.
private void ExtractOrderFromBAC02(ProducerTemplate producerTemplate, InputStream content, String endpoint, String fileName, HashMap<String, Object> headers){
ArrayList<String> list = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(content));
String line;
long numSplits = 50;
int sourcesize=0;
int nof=0;
int number = 800;
try {
while((line = br.readLine()) != null){
sourcesize++;
list.add(line);
}
System.out.println("Lines in the file: " + sourcesize);
double numberFiles = (sourcesize/numSplits);
int numberFiles1=(int)numberFiles;
if(sourcesize<=50) {
nof=1;
}
else {
nof=numberFiles1+1;
}
System.out.println("No. of files to be generated :"+nof);
for (int j=1;j<=nof;j++) {
number++;
String Filename = ""+ number;
System.out.println(Filename);
StringBuilder builder = new StringBuilder();
for (String value : list) {
builder.append("/n"+value);
}
producerTemplate.sendBodyAndHeader(endpoint, builder.toString(), "CamelFileName",Filename);
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(br != null)br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
For people who don't know Camel, this line is used to send the file:
producerTemplate.sendBodyAndHeader (endpoint, line.toString (), "CamelFileName" Filename);
endpoint ==> Destination (it's ok with another code)
line.toString () ==> Values
And then the file name (it's ok with another code)
you count the lines first
while((line = br.readLine()) != null){
sourcesize++; }
and then you're at the end of the file: you read nothing
for (int i=1;i<=numSplits;i++) {
while((line = br.readLine()) != null){
You have to seek back to the start of the file before reading again.
But that's a waste of time & power because you'll read the file twice
It's better to read the file once and for all, put it in a List<String> (resizable), and proceed with your split using the lines stored in memory.
EDIT: seems that you followed my advice and stumbled on the next issue. You should have maybe asked another question, well... this creates a buffer with all the lines.
for (String value : list) {
builder.append("/n"+value);
}
You have to use indexes on the list to build small files.
for (int k=0;k<numSplits;k++) {
builder.append("/n"+list[current_line++]);
current_line being the global line counter in your file. That way you create files of 50 different lines each time :)

Converting a binary number to decimal from a file with whitespace

I am trying to convert binary numbers to decimal from a file. I am able to get the numbers to convert, however, in my text file, if I have more then one binary number in a line the code just skips it.
List<Integer> list = new ArrayList<Integer>();
File file = new File("binary.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
try {
list.add(Integer.parseInt(text,2));
}
catch (Exception ex) {
continue;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is the text file that I am using as well:
00100101
00100110 01000001
01100000
01111011
10010100 00100101
01101011
11000111 00011010
When I run my code I get: [37, 96, 123, 107]
The code skips the lines where there are two binary numbers.
I'm having trouble trying to be able to convert the integers and not use reader.readLine() in the while loop. Any help is greatly appreciated!
Split each line read by the while loop using text.split("\\s+"), and iterate the split values:
String text = null;
while ((text = reader.readLine()) != null) {
for (String value : text.split("\\s+")) {
try {
list.add(Integer.parseInt(value,2));
}
catch (Exception ex) {
continue; // should throw error: File is corrupt
}
}
}
This should do for when you have multiple values in one line.
You loop over the multiple values and add them separately.
try {
for (String s : text.split(" ") list.add(Integer.parseInt(s,2));
}
Also, like Andreas wrote it, it is not recommended to ignore Exceptions.

Remove duplicates from CSV file in eclipse

I have an assignment where i have to read a CSV file containing data with some repeated lines. How to remove the duplicate values and print only the unique values in Eclipse
The data is similar to this:-
1,Ron,1234,ABC,12
2,Harry,4125,DEF,14
3,Kent,1786,GHI,15
1,Ron,1234,ABC,12
2,Harry,4125,DEF,14
String csvFile = "csv.csv";
BufferedReader br = null;
String line = "";
HashSet<String> lines = new HashSet<>();
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
if (lines.add(line)) {
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It may help you
My suggestion is to use the following strategy:
1st step: create a HashMap http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
where you will save each line of the CSV you read. You will save in a hashmap because Hashmap will NOT accept a key that is like another. So, each line you will read, you will save in the hashmap as a KEY!
So, the logic is: Try to save the line you just read as a Key. IF it works, print that line. If it didn't work, discard the line and read the next one.
Got it?
2nd step:
Use a BufferedReader http://docs.oracle.com/javase/7/docs/api/index.html?java/io/BufferedReader.html to read line by line of the CSV.
Get each line of the CSV with the BufferedReader with readLine().
It will save the line you are reading in a String
That's it.
So, here is the overview of the entire code:
1- Read each line of the code with BufferedReader.readLine()
2- Get that string you got from readLine and try to add to your Hashmap as the Key of the hashmap: if it works, print the String. If it doesn't work, discard the string;
3- Read the next line.

How to read a file in Android

I have a text file called "high.txt". I need the data inside for my Android app. But I have absolutely no idea how to read it into an ArrayList of the Strings. I tried the normal way of doing it in Java but apparently that doesn't work in Android since it cant find the file. So how do I go about doing this? I have put it in my res folder. But how do you take the input stream that you get from opening the file within Android and read it into an ArrayList of Strings. I am stuck on that part.
Basically it would look something like this:
3. What do you do for an upcoming test?
L: make sure I know what I'm studying and really review and study for this thing. Its what Im good at. Understand the material really well.
CL: Time to study. I got this, but I really need to make sure I know it,
M: Tests can be tough, but there are tips and tricks. Focus on the important, interesting stuff. Cram in all the little details just to get past this test.
CR: -sigh- I don't like these tests. Hope I've studied enough to pass or maybe do well.
R: Screw the test. I'll study later, day before should be good.
This is for a sample question and all the lines will be stored as separate strings in the array list.
If you put the text file in your assets folder you can use code like this which I've taken and modified from one of my projects:
public static void importData(Context context) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("high.txt")));
String line;
while ((line = br.readLine()) != null) {
String[] columns = line.split(",");
Model model = new Model();
model.date = DateUtil.getCalendar(columns[0], "MM/dd/yyyy");
model.name = columns[1];
dbHelper.insertModel(model);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Within the loop you can do anything you need with the columns, what this example is doing is creating an object from each row and saving it in the database.
For this example the text file would look something like this:
15/04/2013,Bob
03/03/2013,John
21/04/2013,Steve
If you want to read file from External storage than use below method.
public void readFileFromExternal(){
String path = Environment.getExternalStorageDirectory().getPath()
+ "/AppTextFile.txt";
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String line, results = "";
while( ( line = reader.readLine() ) != null)
{
results += line;
}
reader.close();
Log.d("FILE","Data in your file : " + results);
} catch (Exception e) {
}
}
//find all files from folder /assets/txt/
String[] elements;
try {
elements = getAssets().list("txt");
} catch (IOException e) {
e.printStackTrace();
}
//for every files read text per line
for (String fileName : elements) {
Log.d("xxx", "File: " + fileName);
try {
InputStream open = getAssets().open("txt/" + fileName);
InputStreamReader inputStreamReader = new InputStreamReader(open);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = "";
while ((line = bufferedReader.readLine()) != null) {
Log.d("xxx", line);
}
} catch (IOException e) {
e.printStackTrace();
}
}

Categories