2

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?

6
  • Is this indentation some sort of art form? Commented Dec 16, 2011 at 12:10
  • Please care to format your code properly. A neat format is required to look it up. Commented Dec 16, 2011 at 12:12
  • @lonesome:) Why is aix funny? Commented Dec 16, 2011 at 12:14
  • @Lion his\her dry humor :) i tried to format it, hope it looks better now Commented Dec 16, 2011 at 12:16
  • @lonesome:) What about your edit? You just rambled on. Commented Dec 16, 2011 at 12:16

3 Answers 3

3

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); } 
Sign up to request clarification or add additional context in comments.

4 Comments

you mean first i fill the hashset by redundant file?
HashSet will contain lines you've already written to a new file. A line will appear in the new file if it differs from all other lines.
oh i got it, first read first line, then compare with the hashset, if not same,then write it to the file and add to the hashset and go on....
right! you can even don't treat the first line as special one - the first line will be added for sure because HashSet will be empty at the moment :)
2

Just read file line by line into a Set and at the end write new file from data from Set

Comments

2

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,

  1. Open the file
  2. Have a Set<String> declared and initialized (TreeSet will get you sorted results)
  3. Read everyline and add it into the Set
  4. 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)

  5. Done.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.