JAvaファイル切断


ファイルカットには、カット機能とマージ機能があります.次の2つのクラスは、カットクラスとマージクラスです.ユーザーがカットスケール、カット後の小ファイルの保存パス、インデックスファイルを指定できます.
カットクラスは、まずユーザが入力したサイズに従って小さなファイルにカットして指定したフォルダに入れ、インデックスファイルを作成してファイル数を格納します.
マージクラスは、まずインデックスファイルを読み込み、元のファイル名と小さなファイル数を得てから、小さなファイルを1つずつ読み込んで大きなファイルにつなぎます.
 
カットクラス:
import java.io.*;
/**
 *      
 * @author la
 *
 */
public class CutFile {
	public String fileName=null;
	public long unitSize=0;//        
	public String targetDir=null;//            
	public int cutFile() throws Exception{
		File file=new File(fileName);
		long size=file.length();//    
		int count=0;//    
		long pos=0;//    
		long last=0;//     
		DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream(file), (int )unitSize));
		byte[] databuf=new byte[(int)unitSize];
		while(pos<size)
		{
			count ++;
			last=size-pos;
			if(last<unitSize)
				databuf=new byte[(int)last];
			dis.read(databuf);
			System.out.println("count="+count+";pos="+pos+";databuf.length="+databuf.length);
			pos=pos+databuf.length;
			//    
			try {
				RandomAccessFile raFile=new RandomAccessFile(targetDir+file.getName()+"_"+count, "rw");
				raFile.write(databuf);
				raFile.close();
			} catch (Exception e) {
				throw e;
			}
		}
		//      
		File file1=new File(targetDir+file.getName()+"_count");
		FileWriter fWriter=new FileWriter(file1);
		BufferedWriter bWriter=new BufferedWriter(fWriter);
		String string=(new Integer(count)).toString();
		bWriter.write(string, 0, string.length());
		bWriter.flush();
		bWriter.close();
		return count;
	}
}

クラスのマージ:
 
import java.io.*;
/**
 *     
 * @author la
 *
 */
public class LinkFile {
	public String fileName=null;
	public String targetDir=null;
	public int linkFIle()throws Exception {
		//      
		int count=0;
		try {
			File countFile=new File(this.fileName);
			FileReader fReader=new FileReader(countFile);
			BufferedReader bReader=new BufferedReader(fReader);
			bReader.mark(1);
			count=Integer.parseInt(bReader.readLine());
			bReader.close();
		} catch (Exception e) {
			throw e;
		}
		//         
		String oldFileName=null;
		String countFileDir=null;
		try {
			String countFileName=new File(this.fileName).getName();
			countFileDir=fileName.substring(0, this.fileName.lastIndexOf(countFileName)).replace('\\', '/');
			oldFileName=countFileName.substring(0, countFileName.length()-6);
		} catch (Exception e) {
			throw e;
		}
		//     
		FileInputStream fis=null;
		byte[] data=null;
		try {
			FileOutputStream fos=new FileOutputStream(this.targetDir+oldFileName);
			for (int i = 1; i <=count; i++) {
				fis=new FileInputStream(countFileDir+oldFileName+"_"+i);
				data=new byte[fis.available()];
				fis.read(data);
				fis.close();
				fos.write(data);
			}
			fos.close();
		} catch (Exception e) {
			throw e;
		}
		return count;
	}
}