How to select specific content from one file and write it in another?

0

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");
         }
     }
}
    
asked by dahchour ayoub 18.05.2018 в 20:40
source

3 answers

0

What could be done is a split(',') to each line that you read in the file (taking into account that the commas are only used in the file to separate the content). This function returns an arrangement with the Strings obtained, and would only access the positions from which you want to obtain their value.

In your case it would be like this:

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){

                String[] contenidos = str.split(","); //arreglo con los contenidos

                for (int i=0;i<= contenidos.length; i++) {//Recorre el arreglo

                    if(i==3) continue; //Saltamos la posición que no queremos obtener, pero seguimos ejecutando el ciclo

                    str+= contenidos[i]; //Concatenamos el "comtenido" en curso
                }
                textoLeido += 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");
        }
    }
}
    
answered by 19.05.2018 / 00:45
source
0

I have tried the corrections that you have made to me but it does not work for me :(. Now I have managed to make mine work partly because it writes to me in the file the desired content, but in the next line, it rewrites me the whole line without the split. If I remove the line bw.write (s); I do not write anything in the file, and if I return it, I return to the same problem.

public class leer {

public void read(){
 File f = new File("C:\archivoTXT\paises_del_mundo.txt");
 //int x = 105;
 try{
 FileReader fr = new FileReader(f);
 BufferedReader br = new BufferedReader(fr);
 BufferedWriter bw = new BufferedWriter(new FileWriter("C:\archivoTXT\coco.txt"));
 String s;
 do{
 s = br.readLine();
 String[] parts = s.split(",");

        String part1 = parts[0];
        String part2 = parts[1];
        String part6 = parts[5];

         bw.write(parts[0]); 
         bw.write(parts[1]);            
         bw.write(parts[5]);

         bw.newLine();


 bw.write(s);
 System.out.println(s);
 }while(s!=null);
 bw.close();

 }catch(IOException e){
    System.out.println("Archivo no encontrado");
 } 

}

    
answered by 19.05.2018 в 01:41
0

If what you want is to copy specific lines or from a specific position the content of a file to another I recommend you to use

  

RandomAccessFile raf = new RandomAccessFile (path, "rw");

to get the position from where I want to copy to another file I would like it:

  

long pointer = raf.getFilePointer ();

and to locate me in that serious position:

  

raf.seek (pointer);

and you could use the bufferwriter to write the content in the new file

    
answered by 19.05.2018 в 21:33