I have to read a file and then select specific content, write it in another file. The file that I have to read has several lines and each line has this format: "content1", "content2", "content3", "content4", "123"
I have to select the content 1,2,3
and the index 5
that is numeric and write it in another file.
I have implemented the bufferedRead()
and bufferWrite()
, but I do not know how to select the content and write it.
I leave my code:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
public class leerContenido {
public String leerPaises(String ruta){
String textoLeido = "";
try{
BufferedReader br = new BufferedReader(new FileReader(ruta));
String str;
String temp = "";
while((str = br.readLine()) !=null){
temp = temp + str;
}
textoLeido = temp;
}catch(IOException e){
System.out.println("Archivo no encontrado");
}
return textoLeido;
}
public void escribir(String ruta){
leerContenido lc = new leerContenido();
String str1 = lc.leerPaises("C:\archivoTXT\paises_del_mundo.txt");
String s1= "";
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(ruta));
bw.write(str1);
bw.newLine();
bw.close();
}catch(IOException e){
System.out.println("Archivo no escrito");
}
}
}