Element repeated elements in a .txt file

0

I have a file called let's say example.txt in which I have the following numbers.

1323
7445
1323
5345
3455
1323

And I was wondering how I could eliminate the repeated ones, in this case the number 1323 and let only one be left, so it looks like this:

7445
5345
3455
1323

Thank you very much.

    
asked by Chemi 25.08.2018 в 23:52
source

3 answers

0

Another alternative (with streams) would be to use HashSet, since it does not allow duplicates.

Main:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) throws IOException {

        String fileName = "pathtofile.txt";
        Set<String> list = new HashSet<String>();
        Path path = Paths.get(fileName);

        try (Stream<String> lines = Files.lines(path)) {
            list = lines
                    .collect(Collectors.toSet());


        } catch (IOException ex) {
          //capturar la excepción
        }

        //imprimir en consola
        list.forEach(e -> System.out.println(e));

        //Reemplazar archivo
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
        Iterator<String> it = list.iterator(); 
        while(it.hasNext()) {
            out.write(it.next());
            out.newLine();
        }
        out.close();

    }
}

Finally this would be the txt file replaced:

1323
3455
7445
5345
    
answered by 27.08.2018 / 08:29
source
0

I leave you an exercise in which I am in charge of comparing all the elements and the repeated ones equal to cadena vacía "" in such a way that the impression is like the one you ask for. The access to the file and replace it is already yours.

Example:

public class ElementosRepetidosTest {
    public static void main(String[] args) {
        String[] numsEjemplo = {"1323", "7445", "1323", "5345", "3455", "1323"};

        for (int todo = 0; todo < numsEjemplo.length; todo++) {
            for (int i = 0; i < numsEjemplo.length; i++) {
                if(i != todo){
                    if(numsEjemplo[todo].equals(numsEjemplo[i])){
                        numsEjemplo[todo] = "";
                    }
                }
            }
        }

        imprimeArray(numsEjemplo);

    }

    public static void imprimeArray(String[] array){
        for (String elem : array) {
            if (!elem.equals(""))
                System.out.println(elem);
        }
    }

}

Result:

It is comparing in the respective loops all the positions of our arrangement with each one of the elements except those that are in their same position, that is, saved by itself. And in the case that they have the same value or string in this case, we equate to empty string always the one that is in a previous index.

Greetings and I hope you have understood. ;)

    
answered by 26.08.2018 в 09:20
0

An alternative is to use Streams that Java 8 gives us.

public class Main {

    public static void main(String[] args) {

        try (Scanner sc = new Scanner(new File("/home/crack81/Desktop/file.txt"))) {

            List<String> numbers = new ArrayList<>();

            while(sc.hasNext()){
                numbers.add(sc.nextLine());
            }

            List<String> filtered = numbers.stream().distinct().collect(Collectors.toList());

            filtered.forEach(System.out::println);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
    
answered by 26.08.2018 в 23:23