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. start with // (java comments) and pass the rest (it will be a java code) to the second file and finally compile that code.
Most of the program I can do, but my doubt is in 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 what if the line contains two bars (in the case the comment in java) should be in or out of the while while the file until there is nothing else to read, and which command I can use within the if that is able to separate just the lines with comments and create a new string with it?
Many thanks to everyone