Share & learn

Through Innovative Digital Library

How to split a text file in Java?

Write a code in Java to split a single text file into multiple text files.
 



Answers

Posted by Paul on April 27th 2018 11:47 AM

Split text file in multiple files : Here is a code to split a single text file in multiple files based on number of lines. First you need to read file. Once file is read, count number of lines from the file. Now depending on how many lines you want in a file split file. Here is the code :

// Java program to split a file in multiple files.
import java.io.*;
import java.util.Scanner;

public class 
SplitFileDemo {

    public static 
void main(String[] argsthrows IOException {
        try {

            
// read file and get no. of files to be generated
            
String inputfile "D:/test.txt";

            
// No. of lines to be split and saved in each
            
double nol 2.0;
            
            
File file = new File(inputfile);
            
Scanner scanner = new Scanner(file);
            
            
int count 0;
            while (
scanner.hasNextLine()) {
                
scanner.nextLine();
                
count++;
            }

            
// display no. of lines in the input file.
            
System.out.println("Lines in the file: " count);

            
double temp = (count nol);
            
int temp1 = (int) temp;
            
int nof 0;
            if (
temp1 == temp) {
                
nof temp1;
            } else {
                
nof temp1 1;
            }
            
System.out.println("No. of files to be generated :" nof);

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

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

            for (
int j 1<= nofj++) {

                
// location of new file
                
FileWriter fstream1 = new FileWriter("D:/" ".txt");
                
BufferedWriter out = new BufferedWriter(fstream1);
                for (
int i 1<= noli++) {
                    
strLine br.readLine();
                    if (
strLine != null) {
                        
out.write(strLine);
                        if (
!= nol) {
                            
out.newLine();
                        }
                    }
                }
                
out.close();
            }

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

    }
}

Posted by Paul on April 27th 2018 11:49 AM

Here are newly generated files.

Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
AVNN

related questions

Most liked questions

Most answered questions

Login
AVNN Refresh
Register
AVNN Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
AVNN Refresh