How to take data from two different txt, Java

Asked

Viewed 709 times

1

Talk personal, I have a problem here, I have two . txt and I want to create a new txt joining some data of these two . txt, follows below how I get the data from . txt, can anyone give me a way to do this? I was wondering if I could play an array of dice.

        scanner = new Scanner(new FileReader("caminho"))
        .useDelimiter(",|\n");


        while (scanner.hasNext()) {
            String nome = scanner.next();
            String rg = scanner.next();
            String cpf = scanner.next();
  • what is your exactly? ta problem with reading a file? of writing the file? could be clearer?

  • What kind of union should be done? What is the criterion that determines which entries a text should match with another? Line number? Or some other control value that is written on the lines, like an index or a document number? This can be quite simple or quite complicated to do, agent will only find out if you clarify the problem.

  • I want to combine content of two. txt, I can read using Scanner and write using Filewriter, it’s more or less the same doubt but I don’t understand the http://stackoverflow.com/questions/10411382/combine-some-elements-from-2-txt-files-into-a-third-file

  • You want to simply paste a text at the end of another?

  • I have a . txt with 1,Name,RG and another with State,City,Parents , I want to create a third . txt with only the 1,Name,City

  • And what is the common factor between the first and the second text? Is there anything saying which name lives in which city?

  • There is no necessary factor between the two, I wanted to choose a name of a txt and a city of the other generating a final txt with the union of the two

  • It’s supposed to be random so?

  • In fact I needed to be able to select each "field" to mount the last

Show 4 more comments

1 answer

1


A nice way is to use Filewriter methods to create/edit the file and Filereader to read.

    public void gravar() {
            FileWriter fileWriter;
            BufferedWriter bufferedWriter;
            try {
                fileWriter = new FileWriter("configuracoes\\config.txt");
                bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write("localhost");
                bufferedWriter.close();
                fileWriter.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Ocorreu um erro:\n" + ex.getMessage());
            }
        }

public final void lerArquivo() {

        try {
            FileReader fileReader = new FileReader("configuracoes\\config.txt");
            BufferedReader br = new BufferedReader(fileReader);
            try {
                setIP(br.readLine());
                br.close();
                setUrlPorIP(getIP());

            } catch (IOException ex) {
                BancoDeDados.database.tratamentoDeErro(ex, "");
            }

        } catch (FileNotFoundException ex) {
            new File("configuracoes").mkdir();
            File f = new File("configuracoes\\config.txt");
            try {
                f.createNewFile();
                gravarLinhaDeIP();
                lerArquivoConfig();
            } catch (IOException ex1) {
                //JOptionPane.showMessageDialog(null, "O arquivo 'config.txt' não existe.\n"
                //      + "Foi tentado cria-lo mas algo saiu errado.\n"
                //    + "Tente criar o arquivo Manualmente");
            }

        }

    }

Documentación Oficial:

http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html http://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html

Browser other questions tagged

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