RandomAccessFile

package file; 
 import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.RandomAccessFile; 
 / *  * @describe is used to manipulate the contents of the file  * @purpose Append content to the file, first specify the insertion point, create a temporary file,  delete it automatically when exiting the virtual machine*, insert the content after the insertion point into the temporary file, then insert the file you want to insert, and then append the temporary  * file contents  * */  public class RandomAccessFileTest { 
 public static void main(string[] args) {  RandomAccessFileTest r = new RandomAccessFileTest();  try {  r.insertdata("C:/ps.txt", 52, " Essay Archives December 2017 (1)");  } catch(IOException e) {  // TODO auto-generated catch block  e.printStackTrace();  }  }  /* * @param path the file path where you want to insert the content  * @param point insert point position in the file (bytes)  * @param str the content you want to insert  * */  public void insertdata(String path, long point, String str) throws IOException {  RandomAccessFile raf = null;  File targetfile = new File(path);  raf = new RandomAccessFile(targetfile, "rws");  File temp = File.createTempFile("temp", null); // second parameter is null, the resulting file suffix is ​​.tmp  temp.deleteOnExit();  FileOutputStream out = new FileOutputStream(temp);  FileInputStream in = new FileInputStream(temp);  raf.seek(dot);  bytes[] b = new bytes [1024];  byte[] b2 = new byte[1024];  int len ​​= 0;  while ((LEN = raf.read(B)) != - 1){ out.write(b,0,len); }  raf.seek(点);  raf.write(str.getBytes());  int len2 = 0;  而(((LEN2)= in.read(B2))!= - 1){ 
		  raf.write(b2,0,LEN2);  }  }  }




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326536743&siteId=291194637