Package cn.mytext.ref;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.PrintStream;
Import Java.io.SequenceInputStream;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.Enumeration;
Import java.util.Properties;
public class Splitfile {
public static void Main (string[] args) throws IOException {
String src = "d:\\1.bmp";
Splitfile (SRC);
}
Splits a file according to a specified number of copies
private static void Splitfile (String fileName) throws IOException {
File src = new file (fileName);//Get source file
if (!src.exists ())//Determine if the source file exists
{
throw new RuntimeException ("file does not exist");
}
Long src_size = Src.length ();//Get the size of the source file
Long split_size = src_size/3;//Gets the size of each file after splitting
Bufferedinputstream bis = new Bufferedinputstream (
New FileInputStream (SRC));//Instantiate buffered input stream object
Bufferedoutputstream BOS = null;//Declaration name buffered output stream
int len = 0;//read to file size
byte[] buf = new byte[1024*1024];//define Buffer
String src_name = src.getname () + "-";//Get Source file name
File Dest = new file ("D:\\image");//directory of output files
File config = new file (dest, "config.properties");//Declaration profile
if (!config.exists ())//Determine if the configuration file exists
{
Config.createnewfile ();//Create configuration file
}
Properties prop = new properties ();//Configuration file Collection
int count = 1;//records the score of the current file
File new_file = null;
System.out.println ("Start splitting files");
For read and write operations
while (len = Bis.read (buf))!=-1)//Read data
{
if (new_file = = NULL)//Determine if the output file is instantiated
{
new_file = new File (dest,src_name+count+ ". part");//Create Split file
BOS = new Bufferedoutputstream (new FileOutputStream (New_file));//output stream is associated with a split file
Prop.setproperty (Integer.tostring (count), New_file.getname ());//define the build and value of the collection
}
if (New_file.length () >=split_size)//Determine whether the current split file reaches the size of the defined partition
{
System.out.println (New_file.getname () + ":d One")//print the currently completed split file
count++;//for the newly created split file ordinal +1
new_file = new File (dest,src_name+count+ ". part");//Create a fresh split file
BOS = new Bufferedoutputstream (new FileOutputStream (New_file));//output stream is associated with a split file
Prop.setproperty (Integer.tostring (count), New_file.getname ());//define the build and value of the collection
}
Bos.write (buf, 0, Len);//Write Data
Bos.flush ();//Flush Buffer
}
if (len = =-1)//due to the possibility that the last copy is smaller than the size of the split, it cannot be printed.
{
System.out.println (New_file.getname () + ":d One")//print the currently completed split file
}
Close the stream
Bos.close ();
Bis.close ();
Persist the information of a set
Prop.store (new FileOutputStream (config), "config_file");
}
Merging files
private static void Mergefile (String dest,string pro_file) throws FileNotFoundException, IOException {
1. Get the configuration file
File DestFile = new file (dest);//Get a given directory
File config = new file (dest,pro_file);//Get configuration file
2. Read the information from the configuration file into the collection
Properties prop = new properties ();//Create Configuration file Collection
Prop.load (new FileInputStream (config));//collection associated with the configuration file
3, gets the value of the corresponding key
System.out.println (Prop.size ());//print the length of the current collection
String srcfilename = (string) prop.get ("1");//Get Source file name
Srcfilename = srcfilename.substring (0, Srcfilename.lastindexof ("-"));//Intercept file name
The file Srcfile = new file (new file (dest), srcfilename);//Create merged files
if (!srcfile.exists ())//Determine if the file exists
{
Srcfile.createnewfile ();//does not exist create
}
Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (srcfile));//composite file associated with output stream
Arraylist<fileinputstream> al = new arraylist<fileinputstream> ();//Create Collection Object
String file_num = new string ();//defines the Read key
for (int i = 1; i<= prop.size (); i++)
{
File_num = integer.tostring (i);
string file_name = (string) prop.get (file_num);//Gets the corresponding value according to the corresponding key
System.out.println (file_name);//Print the current value
File Part_file = new file (destfile,file_name);//Gets the file name of the corresponding key
Al.add (New FileInputStream (Part_file));//Add the newly added input file stream to the collection
}
4, getting the value information is using the stream merge
Enumeration<fileinputstream> en = Collections.enumeration (AL);
Sequenceinputstream sis = new Sequenceinputstream (en);
byte[] buf = new byte[1024];
int len = 0;
while (len = Sis.read (buf))!=-1)
{
Bos.write (buf, 0, Len);
Bos.flush ();
}
Bos.close ();
Sis.close ();
}
}
Split and use profile merge files according to the score of a given split file