2
Hello ,I’m a beginner in java and I need to create a program that creates a txt file with a pre-defined content , read it and divide the contents of this file into two different txt files , the contents being passed to the first file all lines starting with // (java comments) and pass the rest (will be a java code) to the second file and finally commits this code.
Most of the program I can do, but my doubt is at the part where I need to pass the content to a different file.
public static void main(String[] args) throws IOException {
//conteudo
String conteudo = "arquivo inicial\nlinha2"; //conteudo inicial
String conteudo1 = null; //o que vai ser separado para o arquivo1(comentarios)
String conteudo2 = null; //o que vai ser separado para o arquivo2(codigo a ser compilado)
//cria os 3 arquivos (inicial , txt dos comentarios e txt do codigo
File arquivo = new File("arquivo.txt");
File arquivo1 = new File ("arquivo1.txt");
File arquivo2 = new File("arquivo2.txt");
//prepara pra escrever no arquivo inicial
FileWriter fw = new FileWriter(arquivo.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
//escreve e fecha o arquivo
bw.write(conteudo);
bw.close();
//le o arquivo linha por linha
FileReader ler = new FileReader("arquivo.txt");
BufferedReader reader = new BufferedReader(ler);
String linha;
while( (linha = reader.readLine()) != null) {
System.out.println(linha); //printa linha por linha do arquivo inicial
if (linha.contains("//")) { //se o arquivo conter // , ele separa para outro arquivo
}
else {
}
}
}
My doubts are: if the line contains two bars (in case the comment in java) it should stay inside or outside while reading the file until there is nothing else to read , and what command I can use inside if that is able to separate only the comment lines and create a new string with it?
Thank you all very much
I understood perfectly, and I was already thinking of putting in a Try/catch block to treat the exceptions , but because it is a personal question I preferred to leave so to save time, but thank you very much for the help.
– vick