Divide a . txt into other . txt through the idenficator at the beginning of the line

Asked

Viewed 672 times

0

I am trying to split a file into other smaller files, I am receiving via serversocket a file and I want this file to be divided according to the identifier that comes at the beginning of each line, I am with this code so far, I can write some of the files, but the content is not coming properly, follows an example of the file . txt.

01#Joao Pedro Gonçalves#10000#1205325103

01#cvnbhgdfgs#10000#1205325103

01#gfdhgdfhdfh#10000#1205325103

01#Jffgjhfgvbs#10000#1205325103

01#jfjgfvcnvbves#10000#1205325103

04#LUAL DO LOBO#5351235#123635526356

03#LUAL DO LOBO#5351235#123635526356

05#LUAL DO LOBO#5351235#123635526356

06#LUAL DO LOBO#5351235#123635526356

03#LUAL DO LOBO#5351235#123635526356

07#LUAL DO LOBO#5351235#123635526356

and the code I’m making is:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package split;

/**
 *
 * @author PURE
 */
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Split {

    public static void main(String args[]) {
        try {
            // lendo o arquivo e vendo quantas linhas ele tem
            String inputfile = "caminho.txt"; //  Source File Name.
            double nol = 2000.0; //  No. of lines to be split and saved in each output file.
            File file = new File(inputfile);
            Scanner scanner = new Scanner(file);
            int count = 0;
            while (scanner.hasNextLine()) {
                scanner.nextLine();
                count++;
            }
            System.out.println("Lines in the file: " + count);     // Displays no. of lines in the input file.

            //---------------------------------------------------------------------------------------------------------
            // Actual splitting of file into smaller files
            FileInputStream fstream = new FileInputStream(inputfile);
            DataInputStream in = new DataInputStream(fstream);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine = br.readLine();


            while (strLine.startsWith("1")) {
                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 1 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);
                if (strLine != null) {
                    String vetor[] = strLine.split("#");
                    out.write(Arrays.toString(vetor) + System.getProperty("line.separator"));

                }

                out.close();

            }
            while (strLine.startsWith("2")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 2 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("3")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 3 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("4")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 4 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("5")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 5 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("6")) {
                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 6 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("7")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 7 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("8")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 8 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }
            while (strLine.startsWith("9")) {

                FileWriter fstream1 = new FileWriter("C:\\teste java\\" + 9 + ".txt", true);     // Destination File Location
                BufferedWriter out = new BufferedWriter(fstream1);

                if (strLine != null) {
                    out.write(strLine + System.getProperty("line.separator"));
                } else {
                    out.newLine();
                }
                out.close();
            }

            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

    }

}

Thank you!

@Edit serversocket is in another package in the same project.

  • The initial two digits are known, that is, will come numbers from 01 to 10 and nothing other than that or can come anything?

  • digits are known and predefined, each representing an operation performed by the person who comes soon after, in the second element after the sharp

1 answer

0


A suggestion would be:

boolean arquivo1_aberto, arquivo2_aberto, arquivo3_aberto ... = false;

abre_o_arquivo_principal
while ( tem linhas )
  linha = linha_lida;
  if ( linha comeca com 01 ) {
    if ( !arquivo_aberto )  {
      abre_arquivo1;
      arquivo1_aberto = true;
   }
    escreve_linha_no_arquivo_1;
  } else if ( linha comeca com 02 ) {
    if ( !arquivo_aberto )  {
      abre_arquivo2;
      arquivo2_aberto = true;
   }
   escreve_linha_no_arquivo_2;
 } else if ( linha comeca com 03 ) {
   if ( !arquivo_aberto )  {
      abre_arquivo3;
      arquivo3_aberto = true;
  }
   escreve_linha_no_arquivo_3
} 
...
}
fecha_o_arquivo_principal;
if ( arquivo1_aberto )
   fecha_arquivo1;
if ( arquivo2_aberto )
   fecha_arquivo2;    
...   
  • Thank you Reginaldo Rigo, I did as you described and it worked, I am able to separate perfectly the files now, a strong hug, helped too!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.