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

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) {
    } 
}
发布了30 篇原创文章 · 获赞 1 · 访问量 864

猜你喜欢

转载自blog.csdn.net/weixin_45061669/article/details/104870039
今日推荐