The functions implemented are:
1. Read the text of one file and enter it into another file
2. Read a file stream, intercept the corresponding bytes, and enter it into another file
Pseudo code, as follows:
/**
* Example 1
* Read the text of one file and enter it into another file
*/
String sendContent="F:/test3.txt";
String receiveContent="F:/test/aaa.txt";
try {
RandomAccessFile randomFile = new RandomAccessFile(sendContent, "rw");
// Move the starting position of the read file to the beginIndex position.
int beginIndex =0;
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//Read 10 bytes at a time, if the file content is less than 10 bytes, read the remaining bytes. Assign the number of bytes read at a time to byteread
RandomAccessFile randomReceiveFile = new RandomAccessFile(receiveContent, "rw");
randomReceiveFile.seek(randomReceiveFile.length());
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
//Get the content and enter it into another file
randomReceiveFile.write(bytes, 0, byteread);//ok
}
} catch (Exception e) {
e.printStackTrace ();
}
/**
* Example 2
* Read a file stream, intercept the corresponding bytes, and enter into another file
*/
String sendContent="F:/test.txt";
String receiveContent="F:/test/aaa.txt";
try {
// RandomAccessFile randomFile = new RandomAccessFile(sendContent, "rw");
InputStream randomFile = new FileInputStream(sendContent);
// Move the starting position of the read file to the beginIndex position.
int beginIndex =0;
randomFile.skip(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//Read 10 bytes at a time, if the file content is less than 10 bytes, read the remaining bytes. Assign the number of bytes read at a time to byteread
RandomAccessFile randomReceiveFile = new RandomAccessFile(receiveContent, "rw");
randomReceiveFile.seek(randomReceiveFile.length());
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
//Get the content and enter it into another file
randomReceiveFile.write(bytes, 0, byteread);//ok
}
} catch (Exception e) {
e.printStackTrace ();
}