Java file operations to facilitate reading and writing small files

Source: Internet
Author: User
<! --

Code highlighting produced by actipro codehighlighter (freeware)
Http://www.CodeHighlighter.com/

-->PackageCn.edu. dlut. wisdom;

ImportJava. Io.*;
ImportJava. util.*;

Public   Class Fileaccess {
Public   Static String readalltext (string filename) Throws Ioexception {
Stringbuffer sb =   New Stringbuffer ();
Char [] Buf =   New   Char [ 1024 ];
Bufferedreader br =   New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (filename )));
While (Br. Read (BUF) ! =   - 1 ){
SB. append (BUF );
}
Return SB. tostring ();
}

Public   Static String readalltext (string filename, string encode) Throws Ioexception {
Stringbuffer sb =   New Stringbuffer ();
Char [] Buf =   New   Char [ 1024 ];
Bufferedreader br =   New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (filename), encode ));
While (Br. Read (BUF) ! =   0 ){
SB. append (BUF );
}
Return SB. tostring ();
}

Public   Static String [] readalllines (string filename) Throws Ioexception {
Arraylist < String > Lines =   New Arraylist < String > ();
String S;
Bufferedreader br =   New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (filename )));
While (S = BR. Readline ()) ! =   Null )
Lines. Add (s );
BR. Close ();
Return Lines. toarray ( New String [] {});
}

Public   Static String [] readalllines (string filename, string encode) Throws Ioexception {
Arraylist < String > Lines =   New Arraylist < String > ();
String S;
Bufferedreader br =   New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (filename), encode ));
While (S = BR. Readline ()) ! =   Null )
Lines. Add (s );
BR. Close ();
Return Lines. toarray ( New String [] {});
}

Public   Static   Void Writealltext (string filename, string text) Throws Filenotfoundexception {
Printwriter pw =   New Printwriter ( New Fileoutputstream (filename ));
PW. Print (text );
PW. Flush ();
PW. Close ();
}

Public   Static   Void Writealltext (string filename, string encode, string text) Throws Filenotfoundexception, unsupportedencodingexception {
Printwriter pw =   New Printwriter ( New Outputstreamwriter ( New Fileoutputstream (filename), encode ));
PW. Print (text );
PW. Flush ();
PW. Close ();
}

Public   Static   Void Writealllines (string filename, string [] lines) Throws Filenotfoundexception {
Printwriter pw =   New Printwriter ( New Fileoutputstream (filename ));
For (String S: Lines)
PW. println (s );
PW. Flush ();
PW. Close ();
}

Public   Static   Void Writealllines (string filename, string encode, string [] lines) Throws Filenotfoundexception, unsupportedencodingexception {
Printwriter pw =   New Printwriter ( New Outputstreamwriter ( New Fileoutputstream (filename), encode ));
For (String S: Lines)
PW. println (s );
PW. Flush ();
PW. Close ();
}

Public   Static   Void Writeobject (string filename, serializable ob) Throws Ioexception {
Objectoutputstream OOS =   New Objectoutputstream ( New Fileoutputstream (filename ));
Oos. writeobject (OB );
Oos. Flush ();
Oos. Close ();
}

Public   Static Object readobject (string filename) Throws Ioexception, classnotfoundexception {
Objectinputstream OIS =   New Objectinputstream ( New Fileinputstream (filename ));
Object ob = Ois. readobject ();
Ois. Close ();
Return Ob;
}
}

 

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.