You are on page 1of 5

ROLL NUMBER : 21BCM075

COURSE CODE : CSI0403


ASSIGNMENT : 1
Write a java program "FileSplitter" (command line based or GUI) that accepts a
file name and number of lines(n) after which the file should be split. The file
should be valid existing file and should have an extenstion ".txt". After splitting
the multiple files should be generated, each of which should be of n lines
consecutively. e.g. if file students.txt is of 23 lines and n=5 then the file
students.txt should be split into 5 sub files namely students_1.txt,
students_2.txt and so on, containing first five, next five lines and so on. Handle
the appropriate exceptions. Once the file is split display the message of total
number of files geneated after splitting. Can this program be implemented using
multithreading? Give your views on it. The submission should be in single pdf
containing code with comments, output, content of text file, content of first
splitted sub file, your views of multi-threading feasibility.

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class a_1 extends Frame implements ActionListener {


    a_1() {

        Button b1 = new Button("SUBMIT");


        TextField t1 = new TextField();
        TextField t2 = new TextField();
        Label l1 = new Label();
        l1.setText("ENTER FILE NAME ");
        Label l2 = new Label();
        l2.setText("ENTER HOW MANY LINES YOU WANT TO SPLIT ");
        TextArea q1 = new TextArea();

        add(b1);
        add(t1);
        add(t2);
        add(l1);
        add(l2);
        add(q1);

        b1.setBounds(370, 200, 80, 30);

        t1.setBounds(150, 100, 290, 30);


        l1.setBounds(30, 100, 130, 25);

        t2.setBounds(310, 150, 130, 30);


        l2.setBounds(30, 150, 380, 25);

        q1.setBounds(30, 300, 300, 100);

        setSize(500, 500);
        setTitle(" 21BCM075 FILE SPLITTER");
        setLayout(null);
        setBackground(Color.yellow);
        b1.setBackground(Color.orange);
        setVisible(true);
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String path = t1.getText();
                    BufferedReader r = new BufferedReader(new
FileReader(path));
                    String file = "i";
                    int poll = 1;
                    int entire = 0;

                    int n = Integer.parseInt(t2.getText());

                    while (true) {
                        PrintWriter h = new PrintWriter(new File("student_" +
poll + ".txt"));
                        for (int i = 0; i < n; i++) {
                            file = r.readLine();
                            if (file == null) {
                                break;
                            } else {

                                h.write(file);
                                h.write("\n");
                                h.flush();
                            }
                            entire++;
                        }
                        if (file == null) {
                            break;
                        }
                        poll++;
                        h.close();
                    }
                    r.close();

                    q1.setText("\nTotal Number of Lines in Given File :" +


entire
                            + "\nTotal Number of files generated after
splitting : " + poll +
                            "\n****Files Splitted Successfully*****");

                } catch (Exception z) {
                    System.out.println(z);
                }
            }
        });
    }

    public static void main(String[] args) {


        new a_1();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}

OUTPUT :
VIEWS ON MULTITHREADING :

We can do this program through multithreading. But I would not, as this


implied, have each thread read from the beginning of the file, ignoring
lines until they come to their portion of the input file. This is highly
inefficient. As we imply, the reader has to read all of the prior lines if the
file is going to be divided up into chunks by lines. This means a whole
bunch of duplicate read IO, which will result in a much slower application.

You might also like