一个简单的断点续传方法案例

public static void keep(){
	File sourceFile = new File("E:/a.txt");
    File targetFile = new File("E:/b.txt");
    byte[] buf = new byte[1];
    try(
    		FileInputStream    fis = new FileInputStream(sourceFile);
        	FileOutputStream  fos = new FileOutputStream(targetFile);
    		) {
    	 while (fis.read(buf) != -1) {
             fos.write(buf);
             if (targetFile.length() == 6) {
                 position = 6;
                 try {
                     Thread.sleep(10000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 try(
                 	    RandomAccessFile readFile = new RandomAccessFile(sourceFile, "rw");
                         RandomAccessFile writeFile = new RandomAccessFile(targetFile, "rw");
                 		) {
                     readFile.seek(position);
                     writeFile.seek(position);

                     byte[] buf1 = new byte[1];
                     while (readFile.read(buf1) != -1) {
                         writeFile.write(buf1);
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
                 }

             }
         }
    } 
    catch (IOException e) {
    } 
}
上一篇:GitToc-为你的Github仓库的Readme自动生成一个目录


下一篇:程序员基本功系列3——二分查找