Java learning: file read/write

Source: Internet
Author: User

There are several methods for reading and writing files in Java, but I think the simplest is the fileinputstream and fileoutputstream classes. For example, Code :

Package jmyang. file; import Java. io. *; public class filetest {/** delete file */public static Boolean Delete (string filename) {boolean result = false; file F = new file (filename); If (F. exists () {try {result = f. delete ();} catch (exception e) {e. printstacktrace () ;}} else {result = true;} return result;}/** Read File */public static string read (string filename) {file F = new file (filename); If (! F. exists () {return "file not found! ";}Fileinputstream FS; string result = NULL; try {FS = new fileinputstream (f); byte [] B = new byte [FS. available ()]; FS. read (B); FS. close (); Result = new string (B);} catch (exception e) {e. printstacktrace ();} return result;}/** Write File */public static Boolean write (string filename, string filecontent) {boolean result = false; file F = new file (filename); try {fileoutputstream FS = new fileout Putstream (f); byte [] B = filecontent. getbytes (); FS. write (B); FS. flush (); FS. close (); Result = true;} catch (exception e) {e. printstacktrace ();} return result;}/** Append content to the file */public static Boolean append (string filename, string filecontent) {boolean result = false; file F = new file (filename); try {If (F. exists () {fileinputstream FSIN = new fileinputstream (f); byte [] bin = new byte [FSIN. Available ()]; FSIN. read (BIN); string oldfilecontent = new string (BIN); // system. out. println ("old content:" + oldfilecontent); FSIN. close (); If (! Oldfilecontent. repeated signorecase ("") {filecontent = oldfilecontent + "\ r \ n" + filecontent; // system. out. println ("new content:" + filecontent) ;}} fileoutputstream FS = new fileoutputstream (f); byte [] B = filecontent. getbytes (); FS. write (B); FS. flush (); FS. close (); Result = true;} catch (exception e) {e. printstacktrace ();} return result ;}}

Call example:

 
// Filetest F = new filetest (); // system. out. println (F. read ("C:/a.txt"); final string filename = "C:/a.txt"; system. out. println (filetest. delete (filename); // system. out. println (filetest. append (filename, "this is the content written by Java 1"); // system. out. println (filetest. append (filename, "this is the content written in Java 2 "));

 

Related Article

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.