How to read data from txt files using Java?

Asked

Viewed 1,522 times

5

Is there any mechanism to store the data of a file in a Linkedlist .txt? For example, how would this list have as elements the strings "Caio", "Pedro" and "Luiza"?

import java.io.*;

public class teste {
    public static void main ( String args [] ) throws IOException {
        LinkedList<String> linkedlist = new LinkedList<String>(); /*lista de Strings*/

        File arquivo = new File("C:\\NetBeans\\teste.txt");
        arquivo.createNewFile();
        FileWriter fw = new FileWriter(arquivo, true);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("Caio Pedro Luiza");
        bw.close();
        fw.close();
    }
}
  • What is your real question? read the file . txt and then store in Linked?

  • Yeah, that’s right, that’s right

  • 2

    Have you ever done it? It’s easier to guide you

  • Okay, I’ve edited my question

  • These names are separated by Comma, space or are in different lines?

  • Are separated by space

  • 1

    I removed the [linkedlist] tag, because in my opinion your problem isn’t about processing linked lists, you’re just putting the strings inside a list (which didn’t even need to be a linked list).

  • I recommend reading this page here: https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

Show 3 more comments

2 answers

6

Poirot, you can do it this way:

public class Testes {

    public static void main(String[] args) throws IOException {
        File arquivo = new File("E:\\teste.txt");
        arquivo.createNewFile();
        FileWriter fw = new FileWriter(arquivo, true);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("Caio Pedro Luiza");
        bw.close();
        fw.close();

        LinkedList<String> listaNomes = new LinkedList<String>();
        Scanner in = new Scanner(new FileReader("E:\\teste.txt"));

        while (in.hasNextLine()) 
        {
            String line = in.nextLine();
            System.out.println(line);
            String array[] = line.split(" ");

            for (String i : array) 
            {
                listaNomes.add(i);
            }
            System.out.println("Conteudo lista" + listaNomes);
        }

    }

}

Exit:

[Caio, Pedro, Luiza]

Note: Don’t need to be a LinkedList, could be any other kind of lista even a array common.

  • got it! Thanks, man

4


An alternative is to use the Fileutils made available on Apache Commons IO:

import java.util.Arrays;
import org.apache.commons.io.FileUtils;
(...)

// é fundamental definir o charset do arquivo
String conteudo = FileUtils.readFileToString("E:\\teste.txt", "UTF-8");
List<String> listaNomes = Arrays.asList(conteudo.split(" "));
System.out.println("Conteudo lista" + listaNomes);

If possible, separate the names in the file per line instead of space (then you can use full names). It would look like this:

List<String> listaNomes = FileUtils.readLines("E:\\teste.txt", "UTF-8");
System.out.println("Conteudo lista" + listaNomes);

Unless you have some restriction on adding dependencies to your project.

Browser other questions tagged

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