Segmentation of File

Description

FileSegment.java file segments the file into three parts. Any files can be selected from the system and segmented into three parts with same name in the directory segement1, segement2, and segement3 respectively and the result is displayed in JTextArea.

  • Filename: FileSegment.java

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class FileSegment {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Segment;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
JTextArea textArea1,textArea2,textArea3;
String inputFile;
String filename;
public FileSegment() {
initComponents();
handlingEvents();
}

public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("FileSegment");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);
JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("Source File");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,80,200,40);
jf.add(label1);
textfield1=new javax.swing.JTextField();
textfield1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield1.setBounds(250,80,200,30);
jf.add(textfield1);
Browse=new javax.swing.JButton("Browse");
Browse.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Browse.setBounds(480,80,120,30);
jf.add(Browse);
Segment=new javax.swing.JButton("Segment");
Segment.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Segment.setBounds(620,80,120,30);
jf.add(Segment);
label1=new javax.swing.JLabel("Segment1");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(30,120,200,20);
jf.add(label1);
textArea1=new javax.swing.JTextArea();
textArea1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
JScrollPane scrollBar1=new JScrollPane(textArea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar1.setBounds(30,150,200,300);
jf.add(scrollBar1);
label1=new javax.swing.JLabel("Segment2");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(250,120,200,20);
jf.add(label1);
textArea2=new javax.swing.JTextArea();
textArea2.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textArea2.setLineWrap(true);
textArea2.setWrapStyleWord(true);
JScrollPane scrollBar2=new JScrollPane(textArea2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar2.setBounds(250,150,200,300);
jf.add(scrollBar2);

label1=new javax.swing.JLabel("Segment3");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(480,120,200,20);
jf.add(label1);
textArea3=new javax.swing.JTextArea();
textArea3.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textArea3.setLineWrap(true);
textArea3.setWrapStyleWord(true);
JScrollPane scrollBar3=new JScrollPane(textArea3,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar3.setBounds(480,150,200,300);
jf.add(scrollBar3);
}

public void handlingEvents() {
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
inputFile=fc.getSelectedFile().getPath();
filename=fc.getSelectedFile().getName();
textfield1.setText(inputFile);
}
}
});
Segment.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
File getfile=new File(inputFile);
FileInputStream in=new FileInputStream(getfile);
byte[] buffer = new byte[(int)getfile.length()];
in.read(buffer);
//FileByte fby=new FileByte();
//fby.setByte(buffer);

int totclients=3;
int totsize=buffer.length;
int excess=totsize%totclients;
int segmentsize=(totsize-excess)/totclients;
int clientcount=0;
String fseg[] = new String[totclients];
for(int i=0;i<totclients;i++)
{
byte[] segment;
int cc=0;
if(totclients==i)
{
segment=new byte[segmentsize+excess];
for(int ii=clientcount*segmentsize;ii<((clientcount+1)*segmentsize)+excess;ii++)
{
segment[cc]=buffer[ii];
cc++;
}
}
else
{
segment=new byte[segmentsize];
for(int ii=clientcount*segmentsize;ii<(clientcount+1)*segmentsize;ii++)
{
segment[cc]=buffer[ii];
cc++;
}

}
clientcount++;
String folder[] = new String[totclients];
String path1 = "Segment1/";
String path2 = "Segment2/";
String path3 = "Segment3/";
folder[0] = path1;
folder[1] = path2;
folder[2] = path3;
fseg[i]=new String(segment);
String foldername = "\n" + filename + clientcount + "\n";
File fw=new File(folder[i]+filename+clientcount);
FileOutputStream out5=new FileOutputStream(fw);
out5.write(segment);
out5.close();
}
textArea1.setText(fseg[0]);
textArea2.setText(fseg[1]);
textArea3.setText(fseg[2]);
System.out.println("File Splited to "+totclients+" Segments");
}
catch(Exception e) {
System.out.println(e);
}
}
});
}
public static void main(String args[]) {
FileSegment fs = new FileSegment();
}
}