其他分享
首页 > 其他分享> > 一个简单的断点续传方法案例

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

作者:互联网

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) {
    } 
}

标签:断点续传,RandomAccessFile,readFile,targetFile,案例,File,简单,new,byte
来源: https://blog.csdn.net/weixin_45061669/article/details/104870039