Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can write programe in java for spliting large file by size without truncketing data?
i am having following code wich split the records but data will be get trucketed

Java
import java.io.*;
import java.util.Date;
public class SplitFiles
{   
	FileInputStream fin; 
	FileOutputStream fout;
	int len;  
	int splitlen; 
	String str;   
	
	
	SplitFiles()  
	{        
		try  
	{    
		fin=new FileInputStream("d:\\try.txt"); 
		//str=fileName;
		len=0; 
		splitlen=1024; 
		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("d:\\try.txt");
			int c=fin.read(); 
			while(c!=-1)  
			{               
				FileOutputStream fw=new FileOutputStream("D:\\Manoj_Zete\\auto generated records\\"+new Date().getTime()+".txt");
				while(c!=-1 && len<splitlen)>
				{      
					fw.write(c);  
					c=fin.read(); 
					len++; 
					}    
				len=0; 
				fw.close(); 
				i++;    
				}      
			}      
		catch(Exception e)
		{            
			 
			e.printStackTrace(); 
			
		}  
		}
	
public static void main(String ar[])
{
	new SplitFiles();
	
}
}
Posted
Updated 12-Aug-14 20:38pm
v3

1 solution

I see two (reasonable) scenarios:
  1. Fixed size (say SIZE) records: read (and write to the output files) N bytes, where N = (splitlen / SIZE)* SIZE;
  2. Variable size, delimited records: read complete records until read size > splitsize, then write to the output file all (but the least read) records. Iterate until all the records are read (and written to the output).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900