How to convert a list of strings to a list of integers

4

I have TXT file, and it looks like this:

01 02 03;
01 02 04;
01 03 04;
02 03 04; 

Well, I load this file into ArrayList<String> as it is in the code below. How do I pass it to ArrayList<Integer> , to manipulate its contents?

package bola;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LerArquivo {

    public static ArrayList<String> lista = new ArrayList<>();

    public void leitura() throws FileNotFoundException {

        String path = "D:\Conteudo\teste.txt";
        BufferedReader br = null;
        FileReader fr = null;
        try {
            fr = new FileReader(path);
            br = new BufferedReader(fr);

            List<String> minhaList = new ArrayList<String>();           
            String line = br.readLine();
            String[] meuArray = null;

            while (line != null) {              
                line = br.readLine();
                minhaList.add(line);
                meuArray = minhaList.toArray(new String[0]);                

            }
            for(int i = 0; i < meuArray.length; i++) {
                System.out.println(meuArray[i]);
            }

        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                if (br != null)
                    br.close();
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws IOException {
        LerArquivo le = new LerArquivo();
        le.leitura();
    }
}
    
asked by anonymous 24.08.2018 / 19:51

1 answer

7

First thing: Do not use FileWriter , because this class uses the default encoding of the machine instead of having a user-defined encoding, leading to encoding incompatibility problems. There are even discussions / suggestions on Oracle's mailing lists to mark it as @Deprecated because of this. Instead, use OutputStreamWriter passing the Charset you want in the constructor.

Second, use the try-with-resources and this goes simplify your code a lot.

However, I will rewrite your code in a way that you can leave there these two tips above:

Here is the code:

package bola;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class LerArquivo {

    private static Stream<Integer> ints(String s) {
        return Stream.of(s.replace(";", "").split(" ")).map(Integer::parseInt);
    }

    public static List<Integer> leitura() throws IOException {
        String path = "D:\Conteudo\teste.txt";
        Stream<String> linhas = Files.readAllLines(new File(path).toPath()).stream();
        return linhas.flatMap(LerArquivo::ints).collect(Collectors.toList());
    }

    public static void main(String[] args) throws IOException {
        System.out.println(LerArquivo.leitura());
    }
}

Here is the output produced:

[1, 2, 3, 1, 2, 4, 1, 3, 4, 2, 3, 4]
    
24.08.2018 / 20:07