hello all
excuse my english, I`m a beginer in java and I want to remove duplicate cities + rearrenging file`s lines elements from one 150 mb file, in my file I have that:
"68586240","685 86367","IRVING"
"68586368","685 86431","DENTON"
"68586432","685 86495","DENTON"
and I want to become
"68586240","685 86367","IRVING"
"68586368","685 86495","DENTON"
i mean
1 2 x
3 4 x
5 6 y
become
1 4 x
5 6 y
I have allready done the program but i`m stuck . can anyone help me? -
-
import java.util.regex.*;
-
import java.io.*;
-
-
public class test {
-
-
private static final String REGEX = ",";
-
private static Pattern pattern;
-
-
public static void main(String[] argv) {
-
-
pattern = Pattern.compile(REGEX);
-
-
try{
-
FileInputStream fstream = new FileInputStream("test.txt");
-
DataInputStream in = new DataInputStream(fstream);
-
BufferedReader br = new BufferedReader(new InputStreamReader(in));
-
String strLine;
-
-
int i = 1;
-
-
-
BufferedWriter make_file = new BufferedWriter(new FileWriter("file_temp.txt", true));
-
String make_test = "\"TEST\",\"TEST\",\"TEST\"";
-
make_file.write(make_test);
-
make_file.close();
-
-
-
BufferedWriter out = new BufferedWriter(new FileWriter("good.txt", true));
-
-
while ((strLine = br.readLine()) != null) {
-
-
//explode line
-
String[] items = pattern.split(strLine);
-
-
// read + memory content from temp file
-
FileReader input = new FileReader("file_temp.txt");
-
BufferedReader bufRead = new BufferedReader(input);
-
String line = "";
-
String temp = "";
-
line = bufRead.readLine();
-
while (line != null){
-
temp = temp+line;
-
line = bufRead.readLine();
-
}
-
bufRead.close();
-
-
// explode line from temp file
-
String[] items_temp = pattern.split(temp);
-
-
//delete temp file
-
new File("file_temp.txt").delete();
-
-
BufferedWriter out_temp = new BufferedWriter(new FileWriter("file_temp.txt", true));
-
out_temp.write(strLine);
-
out_temp.close();
-
-
String x1 = items_temp[2];
-
String x2 = items[2];
-
-
if (x1.equalsIgnoreCase(x2)) {
-
// ??????????
-
}
-
else {
-
out.write(items[0]+","+items_temp[1]+","+items[2]+"\n");
-
/*
-
input output correct
-
1 2 x 1 2 x 1 4 x
-
3 4 x 5 6 y 5 6 y
-
5 6 y
-
*/
-
-
}
-
-
System.out.println(i+"-"+strLine);
-
i++;
-
-
}
-
out.close();
-
in.close();
-
}
-
catch (Exception e){
-
System.err.println("Error: " + e.getMessage());
-
}
-
-
}
-
-
}
-
-
tnx
6 10304
Sorry for less time in my hand to look after your code throughly :-)
Have a look at my code .... -
BufferedReader in = new BufferedReader(new FileReader("src_file"));
-
FileWriter out = new FileWriter("tgt_file");
-
String line;
-
HashSet hs = new HashSet();
-
String op = "";
-
while(!(line=in.readLine())!=null){
-
StringTokenizer s = new StringTokenizer(line,",");
-
String city;
-
while(s.hasMoreTokes()) city = s.nextToken();
-
op += hs.add(city) ? (line+System.getProperty("line.separator")) : "";
-
//here it ignores duplicate items
-
}
-
out.write(op);
-
in.close();
-
op.close();
-
Enjoy this code.
Good Luck :-)
Kind regards,
Dmjpro.
Sorry for less time in my hand to look after your code throughly :-)
Have a look at my code .... -
BufferedReader in = new BufferedReader(new FileReader("src_file"));
-
FileWriter out = new FileWriter("tgt_file");
-
String line;
-
HashSet hs = new HashSet();
-
String op = "";
-
while(!(line=in.readLine())!=null){
-
StringTokenizer s = new StringTokenizer(line,",");
-
String city;
-
while(s.hasMoreTokes()) city = s.nextToken();
-
op += hs.add(city) ? (line+System.getProperty("line.separator")) : "";
-
//here it ignores duplicate items
-
}
-
out.write(op);
-
in.close();
-
op.close();
-
Enjoy this code.
Good Luck :-)
Kind regards,
Dmjpro.
@OP Do not use InputStreams to read text files. Use FileReader or Scanner
@dmjpro Do not use StringTokenizer
JosAH 11,448
Recognized Expert MVP
@OP: what should be done with the following file contents:
or with this?
I think the problem description should be less ambiguous and more complete
to start with before we can start thinking of a solution.
kind regards,
Jos
more info
input :
1 2 x
3 4 x
5 6 y
output :
1 2 x
5 6 y
correct:
1 4 x
5 6 y
compare the third element of line 1 with the third element of line 2.
if equal join first element from line 1 with second element from line 2 and the third comun element in ... all in new file
if not equal put line 2 in new file
compare the third element of line 2 with the third element of line 3.
if equal join first element from line 2 with second element from line 3 and the third comun element ... all in new file
if not equal put line 3 in new file
............... ..............
compare the third element of line line n-1 with the third element of line n.
if equal join first element from line n-1 with second element from line n and the third comun element ... all in new file
if not equal put line n in new file
tnx all
more info
input :
1 2 x
3 4 x
5 6 y
output :
1 2 x
5 6 y
correct:
1 4 x
5 6 y
compare the third element of line 1 with the third element of line 2.
if equal join first element from line 1 with second element from line 2 and the third comun element in ... all in new file
if not equal put line 2 in new file
compare the third element of line 2 with the third element of line 3.
if equal join first element from line 2 with second element from line 3 and the third comun element ... all in new file
if not equal put line 3 in new file
............... ..............
compare the third element of line line n-1 with the third element of line n.
if equal join first element from line n-1 with second element from line n and the third comun element ... all in new file
if not equal put line n in new file
tnx all
Did you try my code :-)
Debasis Jana
JosAH 11,448
Recognized Expert MVP
Did you try my code :-)
Debasis Jana
I noticed that you're spoonfeeding quite a bit of (incorrect) code lately; please don't do that.
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Wm |
last post by:
I have a table of users in mySQL that appears to have a lot of duplicates.
What's the best way to look at the userID and email and delete the
duplicates?
Thanx,
Wm
|
by: m|sf|t |
last post by:
All,
Is it possible to use PHP to open/read a TXT file (i.e. IP.TXT) that
contains ip addresses (1 per line), remove any duplicates found, and
re-write the file back out to IP_NEW.TXT ?
The reason is, I have ips coming from several sources, i.e. URLSCAN logs,
MySQL dbs, etc. and I can use a DISTINCT on the individual queries, but I
then combine all the results into 1 file which may contain duplicates.
Thanks.
|
by: Marlene |
last post by:
Hi All
I have the following scenario, where I have found all the duplicates
in a table, based on an order number and a part number (item).I might
have something like this:
Order PODate Rec Qty Invoice# Item
Supplier Status POReceivedDate
570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
|
by: steve smith |
last post by:
Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am using the split
function with ' ', but how do i get it to take into account
punctuation marks such as ',',',' etc?
Also I am then trying to add contents of an array of strings to an
arraylist, but only if the string isn't already there. I was using...
|
by: BarrySDCA |
last post by:
I have a database being populated by hits to a program on a server.
The problem is each client connection may require a few hits in a 1-2
second time frame. This is resulting in multiple database entries -
all exactly the same, except the event_id field, which is
auto-numbered.
I need a way to query the record w/out duplicates. That is, any
records exactly the same except event_id should only return one record.
Is this possible??
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |