How to merge data from two text file - java

I have two related text files shown for example in data1.txt and data2.txt. I want to merge the two files to create result.txt. Any idea how to go about this?
data1.txt
books, 3
Shelf, 5
groceries,6
books, 1
Shelf, 2
data2.txt
books,2
shelf,3
groceries,1
result.txt
books, 3, 2
Shelf, 5,3
groceries,6,1
books, 1,2
Shelf, 2, 3

this is a example for you.first you need to add values to 2d list from data2 text file.and then when line is null in file2 you can get mapping value relative to it's text from that list .so i have a method which will return back the mapping value for a String .code is little long than i thought .i post only relevant methods here.This is link to complete class file
public void marged(){
try {
BufferedReader br1 = null;
BufferedReader br2 = null;
String line1;
String line2;
ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
br1 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data1.txt"));
br2 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data2.txt"));
while ((line1 = br1.readLine()) != null) {
String[] split1 = line1.split(",");
String line1word = split1[0].trim();
String line1val = split1[1].trim();
line2 = br2.readLine();
if (line2 != null) {
String[] split2 = line2.trim().split(",");
String line2word = split2[0].trim();
String line2val = split2[1].trim();
ArrayList<String> list = new ArrayList();
list.add(line2word);
list.add(line2val);
arrayList.add(list);
if (line1word.equalsIgnoreCase(line2word)) {
String ok = line1word + "," + line1val + "," + line2val;
System.out.println(ok);
}
} else {
String ok = line1word + "," + line1val + "," + doesexist(arrayList, line1word);
System.out.println(ok);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
this is the method return mapping value
public String doesexist(ArrayList<ArrayList<String>> arrayList, String s) {
for (int i = 0; i < arrayList.size(); i++) {
String get = arrayList.get(i).get(0);
if (get.trim().equalsIgnoreCase(s.trim())) {
return arrayList.get(i).get(1);
}
}
return "-1";
}
output>>
books,3,2
Shelf,5,3
groceries,6,1
books,1,2
Shelf,2,3

Simply add files into an array of File object then read it using loop.
File []files = new Files[amountOfFiles];
//initialize array elements
for(File f:files)
{
//read each file and store it into string variable
}
//finally write the string variable into result.txt file.

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class SOQ21
{
public SOQ21()
{
merge();
}
public void merge()
{
try
{
String firstfile = "data1.txt";
FileReader fr1 = new FileReader(firstfile);
BufferedReader bfr1 = new BufferedReader(fr1);
String secondfile = "data2.txt";
FileReader fr2 = new FileReader(secondfile);
BufferedReader bfr2 = new BufferedReader(fr2);
/*
^^^ Right here is how you get the files and accompanying BufferedReaders
to handle them
*/
//next, using the readLine() method from the Java API, read each line
//for the first file
//then, separate by taking the words into an ArrayList and storing the
//numbers as Strings in a String[] of equal length of the ArrayList
//Do the same for the second file
//Then, if the word of ArrayList 1 matches the word of ArrayList 2,
//append the String numbers from String[] 2 to String[] 1
//DONE! :)
}
catch(FileNotFoundException ex)
{
//handle how you want
}
}
public static void main(String[] args)
{
SOQ21 soq = new SOQ21();
}
}
The comments I made should answer most of your questions. Lastly, I would pay special attention to the exceptions, I'm not entirely sure how you wanted to deal with that, but make sure you fill it with SOMETHING!

Related

Method reads csv input stream differently than csv file

I was tasked with writing a method that would take in a csv file and persist its data into the appropriate space in the database. The function I wrote does so successfully when the csv data is input directly. However, when using cURL and inputting a whole csv file, it does not read the new line delimiters. In effect, the csv then becomes one row with x number of columns, where x is the number of cells in the file. I have tried changing the csv format (e.g. using carriage return vs line feed) but nothing seems to work. Attached is the code that runs through csv, it takes in an InputStream csvData:
CSVReader reader = new CSVReader(new InputStreamReader(csvData));
String[] line;
int bookNum = 1, lineNum = 2; // skip headers
while ((line = reader.readNext()) != null) {
// map line
String productCode = line[0];
String author = line[1];
String description = line[2];
Integer edition;
try {
edition = Integer.parseInt(line[3].replaceAll("[^\\d.]", ""));
} catch (NumberFormatException nfe) {
edition = null;
}
String copyright = line[4];
String publisher = line[5];
BigDecimal listPrice = !line[6].equals("") ? new BigDecimal(line[6]) : null;
// do stuff with data...
if (bookNum == 1) System.out.println("1 book has been processed");
else System.out.println(bookNum + " books have been processed");
++bookNum;
++lineNum;
}
I think there is no way to achieve what you want as the reader.readNext()
itself return array represents the columns of your CSV file, and the columns in the array indexed by the order of the columns in your CSV file and you can manipulate the array by previously knowing the columns header so you can get your data correct.
Based on this:
line[0] will be your first column / line[1] will be second column and So on.
update adding some code, please try this code instead of yours and tell us what is the result
InputStream inputStream = Files.newInputStream(Paths.get(ClassLoader.getSystemResource("csv/test.csv").toURI()));
List<String[]> list = new ArrayList<>();
CSVReader csvReader = new CSVReader(new
InputStreamReader(inputStream));
String[] line;
while ((line = csvReader.readNext()) != null) {
System.out.println(line[0]+" --- "+line[1]);
list.add(line);
}
inputStream.close();
csvReader.close();
return list;
my file in the class path with the same project and I assumed the data will be as folllow:
colA | colB
A | B
C | D
D | E
the output as follow:
colA --- colB
A --- B
C --- D
E --- F
This works for me. I think the issue is how your input csvData is being formed. Does this work with when you try?
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
public class TestCsv {
private static final String TEST_CSV = "ISBN,Author,Title,Edition,Copyright,Publisher,Value,Grade\n"
+ "1781435460553,1ALTEN,1WORKING WITH AUDIO (PB),12,,1CENGAGE L,0.01,1\n"
+ "2781435460553,2ALTEN,1WORKING WITH AUDIO (PB),22,,2CENGAGE L,0.02,2\r\n"
+ "3781435460553,3ALTEN,1WORKING WITH AUDIO (PB),32,,3CENGAGE L,0.03,3\r\n";
public static void main(String[] args) throws CsvValidationException, UnsupportedEncodingException, IOException {
System.out.println("String Test");
parseCsv(new ByteArrayInputStream(TEST_CSV.getBytes("UTF-8"))); // from String
System.out.println("\n\n---------------------\nFrom File Test");
String fileName = "/tmp/test.csv";
try (FileInputStream fis = new FileInputStream(fileName)) {
parseCsv(fis);
}
}
public final static void parseCsv(InputStream csvData) throws CsvValidationException, IOException {
CSVReader reader = new CSVReader(new InputStreamReader(csvData));
String[] line;
int bookNum = 1, lineNum = 2; // skip headers
while ((line = reader.readNext()) != null) {
// map line
String productCode = line[0];
String author = line[1];
String description = line[2];
Integer edition;
try {
edition = Integer.parseInt(line[3].replaceAll("[^\\d.]", ""));
} catch (NumberFormatException nfe) {
edition = null;
}
String copyright = line[4];
String publisher = line[5];
String listPrice = line[6];
// do stuff with data...
if (bookNum == 1) {
System.out.println("1 book has been processed");
} else {
System.out.println(bookNum + " books have been processed");
}
++bookNum;
++lineNum;
}
}
}
This also works for me so not quite sure what the issue you are having:
System.out.println("standard input");
try {
parseCsv(System.in);
} catch (IOException e) {
// If nothing is passed in
e.printStackTrace();
}

Compare user string input to a string from a textfile

I'm trying to do a simple login from a textfile. I've used different ways of reading the text from the file to a String line(BufferedReader and Scanner). I am able to get the line into a string, but it doesn't want to compare the 2 strings and match when I use an if statement(.equals()) or even if I use .equalsIgnoreCase(). When I print the 2 strings to be compared they are the same. but my if statement doesn't seem to return true?
This was the last coding i tried (I thought maybe if I put it into an array it would compare true, but still nothing).
Iv'e looked and saw similar questions to comparing strings from textfile, but never saw a problem with the if statement to return true
import java.io.*;
import java.text.*;
import java.lang.*;
public class tes
{
public static void main(String[] args)throws Exception
{
String logline = "JMX^1234";
ArrayList<String> lines = new ArrayList<String>();
FileReader fr = new FileReader("/home/jmx/Desktop/javap/Bank/jm.txt");
BufferedReader br = new BufferedReader(fr);
String rline = br.readLine();
while(rline != null)
{
lines.add(rline);
rline = br.readLine();
}
String[] users = new String[lines.size()];
lines.toArray(users);
for(int i = 0; i < users.length; i++)
{
if(logline.equals(users[i]))
{
System.out.println("Matched");
}
}
System.out.println("Login line: " + logline);
System.out.println("Text Line: " + users[0]);
br.close();
fr.close();
}
}
I've tried to execute your code and everything worked as expected. I received "matched". Maybe it's some kind of encoding issue. Try to compare length and if it is ok, try to leave only one line in the file and try this code:
String logline = "JMX^1234";
ArrayList<String> lines = new ArrayList<String>();
FileReader fr = new FileReader("/home/jmx/Desktop/javap/Bank/jm.txt");
BufferedReader br = new BufferedReader(fr);
String rline = br.readLine();
while(rline != null)
{
lines.add(rline);
rline = br.readLine();
}
String[] users = new String[lines.size()];
lines.toArray(users);
for (char ch : users[0].toCharArray()) {
System.out.print((int)ch);
}
System.out.println();
for (char ch : logline.toCharArray()) {
System.out.print((int)ch);
}
System.out.println();
for(int i = 0; i < users.length; i++)
{
if(logline.equals(users[i]))
{
System.out.println("Matched");
}
}
System.out.println("Login line: " + logline);
System.out.println("Text Line: " + users[0]);
br.close();
fr.close();
It should return equal lines of numbers like this:
7477889449505152
7477889449505152
Matched
Login line: JMX^1234
Text Line: JMX^1234
Also try to check out this answer: https://stackoverflow.com/a/4210732/6226118

Trying to concatenate a series of strings via array

having a little bit of an issue. I am looping through a file where by I want to filter out a series of texts and concatenate them at the end of each loop, which then ultimately end up ordering i.e. during the loop phase it does the following:
String A = "A /n"
String A = "A /n U /n"
String A = "A /n U /n B /n"
etc...
The output will be
A
U
B
however i want it to be
A
B
U
I have so far done the following:
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<String> order = new ArrayList<>();
String directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_ordered.txt"));
PrintWriter out = new PrintWriter(directory + "_orderesqsd.txt");
String otherStates = "";
while (fileIn.hasNextLine() == true) {
lines.add(fileIn.nextLine());
System.out.println("Organising...");
}
Collections.sort(lines);
for (String output : lines) {
if (output.contains("[EVENT=agentStateEvent]")) {
out.println(output + "\n");
out.println(otherStates + "\n");
otherStates = "";
}
else {
otherStates += output+ "\n";
}
out.close();
}
Now this does output fine, however, with regards to the "otherStates", i want to get this in a numeric order, and the best way I know is using Collections, however this is for arrays. I am unsure how to go about modifying the "otherStates" part of the code to cater for an array that concatanetates the string and then be able to order them accordingly. Any ideas
Hard to give a correct solution without input file data . Just try the below code. At the very least it should give you some ideas on how to solve the issue
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<String> order = new ArrayList<>();
String directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_ordered.txt"));
PrintWriter out = new PrintWriter(directory + "_orderesqsd.txt");
String otherStates = "";
ArrayList<String> otherStates_duplicate = new ArrayList<>();
String ordered_new_string.;
while (fileIn.hasNextLine() == true) {
lines.add(fileIn.nextLine());
System.out.println("Organising...");
}
Collections.sort(lines);
for (String output : lines) {
if (output.contains("[EVENT=agentStateEvent]")) {
out.println(output + "\n");
out.println(otherStates + "\n");
otherStates = "";
}
else {
otherStates += output+ "\n";
otherStates_duplicate.add(output);
}
Collections.sort(otherStates_duplicate); // Now this should have a sorted list
//if you need a string instead of an arraylist use code below in addition
for(String s:otherStates_duplicate){
ordered_new_string += s + "\n";
}
/*
I have not printed or stored the string ordered_new_string as it is not
clear to me what you want. print/write to a file and check
if ordered_new_string is what your required
*/
out.close();
}

Trying to alter a text file in java

Here is my code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Filter{
Message myMessage;
Scanner input;
Scanner input2;
String sender;
String subject;
String emailMIN;
String line;
String line2;
ArrayList<String> blacklist = new ArrayList<String>();
ArrayList<String> keywords = new ArrayList<String>();
ArrayList<String> subjectWords = new ArrayList<String>();
ArrayList<String> emails = new ArrayList<String>();
//String[] lines;
File SpamMessage;
File inFile;
File inFile2;
File tempFile;
String[] lines;
public Filter(Message m,String blacklistFile, String keywordFile, String Spam)throws IOException{
inFile = new File(blacklistFile);
inFile2 = new File(keywordFile);
input = new Scanner (inFile);
input2 = new Scanner (inFile2);
myMessage =m;
SpamMessage=new File(Spam);
}
public void filter() throws IOException{
PrintWriter output = new PrintWriter(SpamMessage);
while(input.hasNextLine()){
line = input.nextLine();
//System.out.println(line);
if(line!=null)
blacklist.add(line);
}
while(input2.hasNextLine()){
line2 = input2.nextLine();
//System.out.println(line2);
if(line!=null)
keywords.add(line2);
}
emails=myMessage.getEmails();
// System.out.println(emails.size() + emails.get(1));
for(int i = 0; i < emails.size(); i++){
// boolean isSpam = false;
lines = emails.get(i).split("\n");
// System.out.println(lines[5] + lines[7]);
sender = lines[2].substring(lines[2].indexOf('<'), lines[2].indexOf('>'));
//` System.out.println(sender);
emailMIN = lines[6].substring(lines[6].indexOf('<'), lines[6].indexOf('>'));
// System.out.println(emailMIN);
for(int j =0; j<lines.length; j++)
{
if(j==2)
{
for(String blacklist2: blacklist)
{
// System.out.println(blacklist2);
if(lines[j].contains(blacklist2))
{
output.println(emailMIN);
}
// output.close();
}
}
if(j==5 || j>=7)
{
// System.out.println(keywords.size());
for(String keywords2: keywords)
{
// System.out.println(keywords2);
if(lines[j].contains(keywords2))
{
output.println(emailMIN);
}
// output.close();
}
}
//addKeywords();
}
}
output.close();
addKeywords();
}
public void addKeywords() throws IOException
{
tempFile = new File("tempFile.txt");
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
for(int i=0; i<lines.length; i++)
{
if(i==5){
String[] words = lines[i].split(" ");
for(String word: words){
if(word.length()>=6){
subjectWords.add(word +"\n");
//System.out.println(subjectWords);
}
}
keywords.addAll(subjectWords);
pw.println(keywords);
}
}
pw.close();
if (!inFile2.delete()) {
//System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile2)){
//System.out.println("Could not rename file");
}
}
}
I'm trying to update the list of words in the keywords txt file right now it does update it but it puts it in the format [generic, pharmacy, little, inside]
Which is wrong because then if I run my code again it is searching if the file contains [generic, pharmacy, little, inside] and I need it to search for every word not the plus a comma or brace. So basically I want it to copy the words in a list format like this
generic
pharmacy
little
inside
That way it searches for each individual word. I figured out how to do this part. Now, how do I add the senders to a different text file? Also is there a way to modify this so it doesn't add the same keywords twice? Thanks
It is because you are writing an array to the file which causes the toString method of it to be called. Write every single item instead.
Instead of pw.println(keywords); 
Do:
for (String keyword : keywords)
{ 
pw.println(keyword.trim());
}
Or, if every word contains \n already, this should work
for (String keyword : keywords)
{ 
pw.print(keyword);
}
Instead of doing:
pw.println(keywords);
you should instead loop through the array and add each line individually.
for(int i = 0; i < keywords.length; i++) {
pw.println(keywords[i]);
}
That was because you are printing an ArrayList object. In your code, keywords is instance of the List and which would you give you an output of [aa,bb] . More over you would get duplicate words since these list instance are class variables, and printed inside a loop
keywords.addAll(subjectWords);
pw.println(keywords);
Either you can loop around keywords outside the for loop or print the word before adding to list.

Removing a string from a array list Java

I have a array list the contains data from a text file.
The text file is structured like this
1,2,Name,2,itemName
My code:
String Cid = "1";
String Tid = "1";
//Help
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
FileWriter writer = new FileWriter(iFile);
String dataRow = yourfile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine();
else{
System.out.print(dataRow);
writer.append(dataArray[0]+", ");
writer.append(dataArray[1]+", ");
writer.append(dataArray[2]+", ");
writer.append(dataArray[3]+", ");
writer.append(dataArray[4]);
writer.append(System.getProperty("line.separator"));
dataRow = yourfile.readLine();
}
}
writer.flush();
writer.close();
}
I want to be able to remove the record where the Name id and Item id match.
everything I have read about removing items from array lists only talks about removing by item position. Any help would be much appreciated.
String Cid = "1";
String Tid = "1";
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
BufferedReader yourfile2 = new BufferedReader(new FileReader(iFile));
int total=0;
String rec=yourfile2.readLine();
while (rec != null){ // count total records (rows)
total++;
rec=yourfile2.readLine();
}
String dataRow = yourfile.readLine();
String[] allTemp[]=new String[total][]; //create array of an array with size of the total record/row
int counter=0;
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine(); // skip current row if match found
else{
allTemp[counter]=dataArray; //if match not found, store the array into another array
dataRow = yourfile.readLine();
counter++; //index for allTemp array. note that counter start from zero and no increment if the current row is skipped
}
}
FileWriter writer = new FileWriter(iFile); //create new file which will replace the records file. here, all desired records from file already stored in allTemp array
for (String[] arr : allTemp){
//check nullity of array inside the array(record).
if(arr!=null){
for(int i=0;i<arr.length;i++){
writer.append(arr[i]);
if(i<arr.length-1) //add "," in every column except in the last column
writer.append(",");
}
writer.append(System.getProperty("line.separator"));
}
}
writer.flush();
writer.close();
Update:you can delete String[] temp; and temp = new String[dataArray.length]; since it was never used actually
I think you need to iterate over each element in your array list and for each String make a java.util.StringTokenizer with "," as the delimiter (I'm assuming there are no commas in Name or itemName).
Then get the 2nd and 4th tokens and compare them. If then match then remove that item.
It's probably most efficient if you use a for loop that starts at the end fo the ArrayList and moves to the 0th element, removing items by index as you find them.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Neat {
public static void main(String... string) throws FileNotFoundException {
File file = new File("c:/AnyFile.txt");
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String text = fileScanner.nextLine();
String[] data = text.split(",");
int recordId = Integer.parseInt(data[0]);
int nameId = Integer.parseInt(data[1]);
String name = data[2];
int itemId = Integer.parseInt(data[3]);
String itemName = data[4];
if (nameId == itemId) {
removeLineFromFile(file, text);
}
}
}
public static void removeLineFromFile(File file, String lineToRemove) {
try {
File inFile = file;
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
// Construct the new file that will later be renamed to the original
// filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
// Read from the original file and write to the new
// unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
// Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Just get the stuff you want

Categories

Resources