Monday 11 January 2016

Java program to merge Splitted files into a single files

In this program we assume all splitted files are there in a Directory and use is supplying the path of that directory. Also user is specifying desired file name of final file

Source Code:

import java.io.*;
class Merger
{
private String path,name;
public Merger(String p, String n)
{
path=p;
name=n;
}
public void run()
{
try
{
File f= new File(path);
File arr[];
arr=f.listFiles();
FileOutputStream fos=new FileOutputStream(path+"/"+name);
for(int i=0;i<arr.length;i++)
{
FileInputStream fis=new FileInputStream(arr[i]);
int ch;
while((ch=fis.read()) != -1)
{
fos.write(ch);
}
fis.close();
}
fos.close();
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
}
}
class N
{
public static void main(String args[])
{
Console con=System.console();
System.out.println("Enter the Directory Path: ");
String s=con.readLine();
System.out.println("Enter the desired output file name with extension: ");
String l=con.readLine();
Merger f=new Merger(s,l);
f.run();
}
}

No comments:

Post a Comment