Randomaccessfile Operating files

Source: Internet
Author: User

1  Packagefile;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.RandomAccessFile;8 9 Ten  Public classinsertcontent { One  A      Public Static voidInsert (String fileName,LongPos,string insertcontent)throwsioexception{ -File tmp = file.createtempfile ("tmp",NULL); - tmp.deleteonexit (); the         Try( -Randomaccessfile RAF =NewRandomaccessfile (FileName, "RW"); -FileOutputStream tmpout =NewFileOutputStream (TMP); -FileInputStream Tmpinputstream =NewFileInputStream (TMP) +             ){ -             //move the record pointer to the end of the file append content + //Raf.seek (Raf.length ()); A              at             //move the record pointer to the specified position - Raf.seek (POS); -              -             byte[] Bbuf =New byte[64]; -          -             intHasread = 0; in              -StringBuffer SBF =NewStringBuffer (); to              +              while((Hasread = Raf.read (bbuf)) > 0 ){ -Tmpout.write (bbuf, 0, hasread); theSbf.append (NewString (BBUF)); *             } $ System.out.println (sbf.tostring ());Panax Notoginseng              - Raf.seek (POS); the              + Raf.write (Insertcontent.getbytes ()); A              the              while((Hasread = Tmpinputstream.read (bbuf)) > 0) { +Raf.write (bbuf, 0, hasread); -             } $         } $     } -      -      Public Static voidMain (string[] args) { the         Try { -Insert ("F:/new 1.txt", "AJSDKJFKSLDJFKL");Wuyi}Catch(IOException e) { the e.printstacktrace (); -         } Wu     } -}

Randomaccessfile Operating files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.