Continuous transfer of java file operation breakpoint

Preface:
Today, I learned about the breakpoint sequel. First, I will record it. Maybe I don't know it very comprehensively, but mainly see the implementation on the internet.
Text:
According to our understanding, there are two main implementations, one is
Breakpoint continuation can be handled by using Random Access File (Random Access File) to process files. There are seek method and skipBytes() method to move file pointers, which can be understood by oneself. In addition, InputStream can also be used, which has a skip method to move file pointers. The idea is to use buffer for each transmission, write to record the size of the current transmission, then save it, and permanently save the record and corresponding files (database, file, etc.) when pausing. Next time, use the look or skip function to move the pointer and read the transmission.
On Random Access File
"r": Opens in a read-only manner, and any write (write) method that calls the object causes an IOException exception
"rw": Open in the form of reading and writing, supporting the reading or writing of files. If the file does not exist, it is created.
"rws": Open in read and write mode. Unlike "rw", each update of file content is synchronously updated to potential storage devices. Here "s" means synchronous.
"Rwd": Open it in read and write mode. Unlike "rw", every update of file content is synchronously updated to potential storage devices. Using "rwd" mode only requires updating the contents of files to storage devices, while using "rws" mode not only updates the contents of files, but also updates the metadata of files. Therefore, at least one low-level I/O operation is required.
About skip and seek:
Calling the seek method class to locate a location larger than the file length will result in IOException. Unlike skip() of java.io.InputStream, seek can locate anywhere in the file, while skip can only locate another location relative to the current location. Seek method is a relatively high-cost operation, which needs to be used carefully. It is better to use flow data to construct application access mode. (Extracted from Network)
Here are two kinds of code to simulate the implementation, the specific implementation can be referred to, and then try to write a simple implementation of Java Web
Blog 1: Skp implementation
Blog 2: Using seek Implementation

First kind

/**
 * Test Implementation File Breakpoint Continuation
 * Use seek function
 * @author ht032
 *
 */
public class TestFile {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		File file = new File("d:/1.txt");
//		File file = FileManage.CreateDir("d:/user.txt");
//		FileManage.remove("d:/dir1");
		try {
			int len = new TestFile().CopyFile("d:/lenet80.png", "d:/temp.png", 67584);
			System.out.print("Uploaded:"+len/1024.0);
			System.out.println("KB\n"+len);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private RandomAccessFile sourceFile;
	private RandomAccessFile targetFile;

	/**
	 * To achieve replication, you first need to set the source target file stream
	 * 
	 * @param target   Target file
	 * @param position Starting position of source file
	 * @return Returns the current transferred file size
	 */
	public int CopyFile(String source, String target, long position) throws Exception {
		if (source == null)
			throw new Exception("source is null!");
		byte[] buff = new byte[1024];
		int len = 0;

		try {
			// Open file
			targetFile = new RandomAccessFile(target, "rw");
			sourceFile = new RandomAccessFile(source, "r");
			
			//Move the file pointer to the specified location
			sourceFile.seek(position);
			targetFile.seek(position);
			
			long length = sourceFile.length();
			System.out.printf("Total file size%.2fKB\n", length / 1024.0);
			while (true) {

				// int read = rafilein.read(buff);
				int read = sourceFile.read(buff);
				targetFile.write(buff);
				len += read;
				if (len >= length/2 ||read<0)
					break;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// open the log file
//			FileWriter log=new FileWriter(logPath);
//			log.write(str);
			try {
				sourceFile.close();
				targetFile.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return len;
	}

}

Second kinds

package FileDemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class TestFile2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			int len = new TestFile2().CopyFile("d:/lenet80.png", "d:/temp.png", 11264);
			System.out.print("Uploaded:"+len/1024.0);
			System.out.println("KB\n"+len);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private InputStream sourceFile;
	//Random Access File is also available here for client simulation persistence
//	private RandomAccessFile targetFile;
	private OutputStream targetFile;


	/**
	 * To achieve replication, you first need to set the source target file stream
	 * Using skip
	 * @param target   Target file
	 * @param position Starting position of source file
	 * @return Returns the current transferred file size
	 */
	public int CopyFile(String source, String target, long position) throws Exception {
		if (source == null)
			throw new Exception("source is null!");
		byte[] buff = new byte[1024];
		int len = 0;

		try {
			// Open the file and append it to achieve breakpoint persistence
			targetFile = new FileOutputStream(target,true);
			sourceFile = new FileInputStream(source);
			
			//Move the file pointer to the specified location
			sourceFile.skip(position);
			
//			long length = sourceFile.length();
//			System.out.printf("total file size%. 2fKB n", length / 1024.0);
			while (true) {

				// int read = rafilein.read(buff);
				int read = sourceFile.read(buff);
				targetFile.write(buff);
				len += read;
				if(
						read<0
						//Leng > 4*1024// 4KB transmission first
						)
					break;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// open the log file
//			FileWriter log=new FileWriter(logPath);
//			log.write(str);
			try {
				sourceFile.close();
				targetFile.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return len;
	}

}

Keywords: Java Database network

Added by dmarchman on Wed, 11 Sep 2019 12:38:58 +0300