How to convert a list of Strings to an integer list

Asked

Viewed 402 times

4

I have TXT file, and it lies like this:

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

Well, I upload this file on ArrayList<String> as it is in the code below. How do I pass it to ArrayList<Integer>, to manipulate your content?

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();
    }
}

1 answer

7

First thing: Don’t use FileWriter, because this class uses the standard machine encoding rather than having a user-defined encoding, leading to encoding incompatibility problems. There are even discussions/suggestions on the Oracle mailing lists to mark it as @Deprecated because of this. In her place, use OutputStreamWriter passing the Charset desired in the builder.

Second, use the Try-with-Resources and this will greatly simplify your code.

However, I will rewrite your code in a way that you can let go of these two tips above:

Here’s 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]
  • thanks for the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.