File Splitter

Discussion in 'Java' started by sallu, Jan 18, 2005.

  1. sallu

    sallu New Member

    Joined:
    Oct 21, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Here is a file splitter made in java . try it with files. Don't try it with directory. It will not respond in the way u want.
    Splitter class
    Code:
    import java.io.*;
    
    class Splitter
    {
    	public static void main(String[] args) throws IOException
    	{
    		takeInput(args);
    	}
    
    	static void takeInput(String[] args) throws IOException
    	{
    		String str;
    		String assembledFileName;
    		int splitLength;
    
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
    		try
    		{
    			if(args.length==0)
    			{
    				System.out.println("Enter the filename:");
    				str=br.readLine();
    				System.out.println("Enter the name of the file after assembling(without extension):");
    				assembledFileName=br.readLine();
    				System.out.println("Enter the split size:");
    				splitLength=Integer.parseInt(br.readLine());
    
    
    				SplitFiles ob1=new SplitFiles(str,splitLength);
    
    				SplitListOnly ob2=new SplitListOnly(str,assembledFileName);
    				ob2.batchFileMaker();
    			}
    			else if(args.length>1)
    			{
    				System.out.println("Invalid arguments.");
    				System.exit(1);
    			}
    		}
    		catch(NumberFormatException e)
    		{
    			System.out.println("Problem in number format...");
    			e.printStackTrace();
    		}
    	}
    }
    
    SplitListOnly class
    Code:
    import java.io.*;
    import java.util.*;
    
    class SplitListOnly
    {
    	File f1;
    	File f2;
    	String[] s;
    	String str;
    	String assembledFileName;
    	SplitListOnly(String fileName,String assembledFileName) throws IOException
    	{
    		try
    		{
    			f1=new File(fileName);
    			f2=new File(f1.getParent());
    			this.assembledFileName=assembledFileName;
    			FilenameFilter only=new OnlyExt("spt",f1.getName());
    
    			s=f2.list(only);
    		}
    		catch(Exception e)
    		{
    			System.out.println("Exception occurred in SplitListOnly(String fileName,String assembledFileName) constructor.");
    			e.printStackTrace();
    		}
    	}
    
    	void batchFileMaker()
    	{
    		try
    		{
    			FileWriter fw=new FileWriter(f2.getPath()+"\\"+"Assemble.bat");
    			fw.write("@echo off\n");
    			fw.write("copy /B ");
    			for(int i=0;i<s.length;i++)
    			{
    				str=new String(f1.getPath()+"."+(i+1)+".spt");
    				fw.write(str);
    				if((i+1)<s.length)
    					fw.write("+");
    
    			}
    			str=(f1.getName()).substring((f1.getName()).indexOf(".")+1,(f1.getName()).length());
    			fw.write(" "+f2.getPath()+"\\"+assembledFileName+"."+str);
    			fw.write("\necho The files have been assembelled successfully..........");
    			fw.write("\npause");
    			fw.close();
    		}
    		catch(Exception e)
    		{
    			System.out.println("Exception occurred in void batchFileMaker() method.");
    			e.printStackTrace();
    		}
    	}
    }
    
    SplitFiles class to split the files
    Code:
    import java.io.*;
    
    
    class SplitFiles
    {
    	FileInputStream fin;
    	FileOutputStream fout;
    	int len;
    	int splitlen;
    	String str;
    
    	SplitFiles(String fileName,int splitlength)
    	{
    		try
    		{
    			fin=new FileInputStream(fileName);
    			str=fileName;
    			len=0;
    			splitlen=splitlength;
    			Split();
    
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("File not found.");
    		}
    		catch(IOException e)
    		{
    			System.out.println("IOException generated");
    		}
    	}
    	void Split()
    	{
    
    		try
    		{
    			int i=0;
    			FileInputStream fin=new FileInputStream(str);
    			int c=fin.read();
    			while(c!=-1)
    			{
    				FileOutputStream fw=new FileOutputStream(str+"."+(i+1)+".spt");
    				while(c!=-1 && len<splitlen)
    				{
    					fw.write(c);
    					c=fin.read();
    					len++;
    				}
    				len=0;
    				fw.close();
    				i++;
    			}
    
    		}
    		catch(Exception e)
    		{
    			System.out.println("gg");
    			e.printStackTrace();
    		}
    	}
    
    }
    OnlyExt filters the file from folders and other objects like Shortcuts / links / Special folders.
    Code:
    import java.io.*;
    
    public class OnlyExt implements FilenameFilter
    {
    	String ext,fileName;
    
    	OnlyExt(String ext,String fileName)
    	{
    		try
    		{
    			this.ext="."+ext;
    			this.fileName=fileName;
    		}
    		catch(Exception e)
    		{
    			System.out.println("Exception occurred in OnlyExt(String ext,String fileName) +construcctor.");
    		}
    	}
    	public boolean accept(File dir,String name)
    	{
    		return name.endsWith(ext) && name.startsWith(fileName);
    
    	}
    
    }
    
    Included in the attachment the class files as well.
     

    Attached Files:

  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have posted the thread in queries and expert comment section. I have moved it to the Codes and project section.

    Also nice work.
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Hey I searched on net using the keywords related to File splitter program and the results were all utilities and other stuff to split the file and finally when I searched here found this. It really helped but I always had the feeling Google is the best way to find the things but today I have proved my opinon wrong
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice