File Read and Write use Java

Source: Internet
Author: User
Read files:

Fileinputstream
Create a fileinputstream by opening a connection to the actual file.

Name. Create a new filedescriptor object to indicate the file connection.
Inputstreamreader
Inputstreamreader is a bridge between byte streams: it uses the specified charset to read bytes and decode them into words.

. The character set used can be specified by the name or explicitly specified. Otherwise, the default Character Set of the platform may be accepted.
Bufferedreader
Read text from the character input stream, buffer each character, so as to provide efficient reading of characters, arrays and rows. You can specify a buffer.

Or use the default size. In most cases, the default value is large enough.
Stringbuffer
A variable string of thread-safe characters. A string buffer similar to a string, but cannot be modified. Although at any point in time

It contains a specific character sequence, but the length and content of the sequence can be changed by calling some methods.

Public static void main (string [] ARGs ){

// Read the file content
File a = new file ("C:/add2.txt ");
If (A. exists ()){
Fileinputstream Fi = new fileinputstream ();
Inputstreamreader ISR = new inputstreamreader (FI, "GBK ");
Bufferedreader bfin = new bufferedreader (ISR );
String rline = "";
While (rline = bfin. Readline ())! = NULL ){
System. Out. println (rline );
}
}

}

Write File:

Fileoutputstream and filewriter are usually used to write files in Java. filewriter can only write text files. Fileoutputstream is often combined with bufferedoutputstream. In actual applications, most text files are written. So the following test uses three different methods to generate a file of the same number of rows and the same size.

Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io .*;

Public class filetest {
Public filetest (){
}

Public static void main (string [] ARGs ){
Fileoutputstream out = NULL;
Fileoutputstream outstr = NULL;
Bufferedoutputstream buff = NULL;
Filewriter fw = NULL;
Int COUNT = 1000; // number of lines written to the file
Try {
Out = new fileoutputstream (new file ("C:/add.txt "));
Long begin = system. currenttimemillis ();
For (INT I = 0; I

  • Previous:Question
  • Next article:Awk
  • 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.