lonesome
lonesome

Reputation: 2542

removing redundant line from a file

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

Answers (3)

st0le
st0le

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,

  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.

Upvotes: 2

yatskevich
yatskevich

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

Jigar Joshi
Jigar Joshi

Reputation: 240938

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

Upvotes: 2

Related Questions