I'm trying to split a file into smaller files, I'm getting 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 have this code so far, I can write some of the files, but the content is not coming correctly, here is an example of the .txt file.
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 doing 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 o serversocket is in another package in the same project.