Reading .bat files from java

1

I would like to read and edit a .bat file by java, in case I have a .bat file that I want to open it in .txt to change, the method below is what I'm using

p>
public void editarArquivo() throws SQLException, IOException{
    DirControle dir = new DirControle();
    String directory = dir.selectedDir_CB().toString().replace("[", "").replace("]", "");
    Runtime.getRuntime().exec("notepad "+directory+"\"+getPasta()+"\"+getArchive());
}                                                                                      

where directory is the path of my file getPasta () is a folder before the file and getArchive () the file itself

    
asked by anonymous 06.10.2016 / 21:16

1 answer

0

Java is indifferent to file extensions and yes, only to its content, and a Bat file is a text, so for you to write a file from in Java, you could do this:

//Essa pasta é relativa ao lugar de execucao do programa.
File file = new File("diretorio/minha-saida.bat");//Como eu disse, é so texto
BufferedWriter writer = new BufferedWriter(new FileWriter(file));

writer.write("1 1 1 1 1 \n");
writer.write("2 2 2 2 2 \n");
writer.write("3 3 3 3 3 \n");
writer.write("4 4 4 4 4 \n");
writer.write("5 5 5 5 5");

writer.flush(); //Cria o conteúdo do arquivo.
writer.close(); //Fechando conexão e escrita do arquivo.

To read, an idea would be:

FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
while((data = reader.readLine()) != null){
    System.out.println(data);
    //imprime o conteúdo, mas você poderia fazer outras
    //coisas com a variável data como salvar numa string que 
    //representa o arquivo etc.
}
fileReader.close();
reader.close();
    
08.10.2016 / 05:27