Saving result in txt file

Asked

Viewed 590 times

2

Guys how would I save the output from the program to txt file ? in the example I call cmd and ask you to accomplish something, wanted to save the result in a text file.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {

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



    String line;
    Process saida;

    //executa o processo e armazena a referência em 'Saida'

    saida = Runtime.getRuntime().exec("cmd /c ipconfig");



    //pega o retorno do processo

    BufferedReader stdInput = new BufferedReader(new 
            InputStreamReader(saida.getInputStream()));

    //printa o retorno
    while ((line = stdInput.readLine()) != null) {
        System.out.println(line);

    }
    stdInput.close();

}
}
  • http://www.avajava.com/tutorials/lessons/how-do-i-write-a-string-to-a-file.html

  • http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java

2 answers

2

Here’s a very simple way, based on Docs.


Include these Imports:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

Create this path:

Path path = Paths.get("/endereco/do/seu/arquivo.txt");

And change your own System.out.println(line); for:

Files.write(path, line.getBytes(), StandardOpenOption.APPEND);

To create a file, instead of using an existing one, add right after the creation of path:

try {
    Files.createFile(path);
} catch (Exception e) {
    System.err.println(e);
}
  • Mano is the following happens that I shape that you indicated me works , but it does not create a txt file , in case there is a file already and it will only overwrite on top of it

  • @Lucasluan I edited my answer.

  • Mano worked well he creates the file even if there is no file he creates, and if I want to show reading the file ? could put a print inside while ? or not ?

  • You are creating a file, and not reading, right? If you want to leave the println inside there are no problems. Reading the file are another five hundred... :p

  • If the answer worked for you, please mark it as 'accepted'... :)

  • ta mano , vlw for help

Show 1 more comment

1

I won’t have time to edit the code but I’ll explain how it works and you can adapt to your need.

In this case I created functions that create the txt file (which in this case I call wallet), and also has a function that verifies the existence of the same what you should do is call the check function, and if it does not exist you call the function that will create a.

    public boolean verifCarteira(String carteira) {
        File file = new File(carteira);

        return file.exists();

    }

    public boolean criaCarteira(String Carteira) throws IOException {
        new File(Carteira).createNewFile();
        File file = new File(Carteira);
        return file.exists();
    }
    
    public boolean verifRegistro(String registro) {
        File file = new File(registro + "registro.txt");

        return file.exists();

    }

    public void criaRegistro(String registro) throws IOException {
        new File(registro + "registro.txt").createNewFile();
        File file = new File(registro + "registro.txt");
        if(file.exists()){
            System.err.println("Registro criado");
        }
    }

Browser other questions tagged

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