Reputation: 2542
here it is, using this function:
private static void write(String Swrite) throws IOException {
if (!StopWordRemoval.exists()) {
StopWordRemoval.createNewFile();
}
FileOutputStream fop = new FileOutputStream(file);
if (Swrite != null)
fop.write(Swrite.getBytes());
fop.flush();
fop.close();
}
my program gets string from user and write it into a file. after all users done with inputting their info, i want to remove the redundant info. if two exact lines, then one removes. first i tried the below codes but didnt worke out:
private static void Normalize(File file) throws FileNotFoundException, IOException {
String tempLine2;
BufferedReader buf = new BufferedReader(new FileReader(file));
FileOutputStream fop = new FileOutputStream(temp, true);
String tempLine = null;
tempLine = buf.readLine();
fop.write(tempLine.getBytes());
BufferedReader buf2 = new BufferedReader(new FileReader(temp));
while ((tempLine = buf.readLine()) != null) {
while ((tempLine2 = buf2.readLine()) != null) {
if (!tempLine.trim().equals(tempLine2)) {
if (tempLine != null)
for (final String s : tempLine.split(" ")) {
fop.write(s.getBytes());
fop.write(System.getProperty("line.separator").getBytes());
}
}
}
}
}
my idea in the second function was as below: writing first line into a new file, then comparing second line with it, if different then write, then comparing third line with both...but it seems my function sucks. any help?
Upvotes: 2
Views: 281
Reputation: 33545
Ok, your approach can be better. I think this might be homework, so I'm not going to post any code...
For the Normalize
function,
Set<String>
declared and initialized (TreeSet will get you sorted results)Overwrite that file with the entries of the Set as each line.
(Explanation: Close the FileInputStream, and Create a new PrintStream(sameFile);
which will essentially delete the previous contents and then start out.println(eachLine)
, finally close the file)
Done.
Upvotes: 2
Reputation: 2093
Create a Set
of lines. Consider this pseudo-code:
Set<String> uniqueLines = new HashSet<String>();
String line = readLine();
if (!uniqueLines.contains(line)) {
write_to_file(line);
uniqueLines.add(line);
}
Upvotes: 3
Reputation: 240938
Just read file line by line into a Set
and at the end write new file from data from Set
Upvotes: 2