Turn: Java read/write file Daquan, add content to the end of the file

Source: Internet
Author: User

1. Java read and write files

 

At first, Java does not support the processing of text files. In order to make up for this defect, two classes, reader and writer, were introduced. Both classes are abstract classes.
Write (char [] CH, int off, int
The length), flush (), and close () methods are abstract methods. In reader, read (char [] CH, int off, int
The length) and close () methods are abstract methods. Subclass should implement them separately.
When reading and writing text files, It is very convenient to use reader, such as filereader, inputstreamreader, and bufferedreader. The most important class is inputstreamreader,
It is a bridge between byte conversion and character conversion. You can specify the encoding method in the constructor. If you do not specify the encoding method, the underlying operating system uses the default encoding method, such as GBK. When filereader is used to read files
.

Package com. thread. test;

Import
Java. Io. bufferedreader; 
Import java. Io. file; 
Import
Java. Io. fileinputstream; 
Import
Java. Io. filereader; 
Import
Java. Io. ioexception; 
Import
Java. Io. inputstream; 
Import
Java. Io. inputstreamreader; 
Import
Java. Io. randomaccessfile; 
Import java. Io. reader; 
Public class readfromfile

 
Public static void readfilebybytes (string
Filename ){ 
File file = new
File (filename ); 
Inputstream in = NULL; 
Try { 
System. Out. println ("Read File Content in bytes, read one byte at a time :"); 
// Read one byte at a time 
In = new
Fileinputstream (File ); 
Int tempbyte;
While (tempbyte = in. Read ())! =
-1 ){ 
System. Out. Write (tempbyte );
}
In. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
Return;
}
Try {
System. Out. println ("Read File Content in bytes, read multiple bytes at a time :");
// Read multiple bytes at a time
Byte [] tempbytes = new byte [1, 100];
Int byteread = 0; 
In = new
Fileinputstream (filename ); 
Readfromfile. showavailablebytes (in ); 
// Read multiple bytes into the byteread array. byteread is the number of bytes read at a time. 
While (byteread = in. Read (tempbytes ))! =
-1 ){ 
System. Out. Write (tempbytes, 0,
Byteread ); 

} Catch (exception E1 ){ 
E1.printstacktrace (); 
} Finally { 
If (in! = NULL ){ 
Try { 
In. Close (); 
} Catch (ioexception E1)





 
Public static void readfilebychars (string
Filename ){ 
File file = new
File (filename ); 
Reader reader = NULL; 
Try { 
System. Out. println ("Read File Content in character units, read one byte at a time :"); 
// Read one character at a time 
Reader = new inputstreamreader (New
Fileinputstream (File )); 
Int tempchar; 
While (tempchar = reader. Read ())! =
-1 ){ 
// In Windows, when the two characters rn are together, a line break is displayed. 
// If the two characters are displayed separately, the rows are changed twice. 
// Therefore, R is blocked or N is blocked. Otherwise, there will be a lot more blank lines. 
If (char) tempchar )! =
'R '){ 
System. Out. println (char) tempchar ); 


Reader. Close (); 
} Catch (exception e ){ 
E. printstacktrace (); 

Try { 
System. Out. println ("Read File Content in characters, read multiple bytes at a time :"); 
// Read multiple characters at a time 
Char [] tempchars = new
Char [30]; 
Int charread = 0; 
Reader = new inputstreamreader (New
Fileinputstream (filename )); 
// Read multiple characters into the character array. charread is the number of characters read at a time. 
While (charread =
Reader. Read (tempchars ))! =-1 ){ 
// Disable R display. 
If (charread =
Tempchars. Length) & (tempchars [tempchars. Length-1]
! = 'R ')){ 
System. Out. Print (tempchars ); 
} Else { 
For (INT I = 0; I <charread;
I ++ ){ 
If (tempchars [I] = 'R '){ 
Continue; 
} Else { 
System. Out. Print (tempchars [I]); 




} Catch (exception E1 ){ 
E1.printstacktrace (); 
} Finally { 
If (reader! = NULL ){ 
Try { 
Reader. Close (); 
} Catch (ioexception E1)





 
Public static void readfilebylines (string
Filename ){ 
File file = new
File (filename ); 
Bufferedreader reader =
NULL; 
Try { 
System. Out. println ("read the file content in the unit of action, read the entire row at a time :"); 
Reader = new bufferedreader (New
Filereader (File )); 
String tempstring = NULL; 
Int line = 1; 
// Read a row at a time until null is the end of the file. 
While (tempstring = reader. Readline ())! =
Null ){ 
// Display the row number 
System. Out. println ("line" + LINE + ":" +
Tempstring ); 
Line ++; 

Reader. Close (); 
} Catch (ioexception e ){ 
E. printstacktrace (); 
} Finally { 
If (reader! = NULL ){ 
Try { 
Reader. Close (); 
} Catch (ioexception E1)





 
Public static void readfilebyrandomaccess (string
Filename ){ 
Randomaccessfile randomfile =
NULL; 
Try { 
System. Out. println ("randomly reading a file :"); 
// Open a random file stream in read-only mode 
Randomfile = new randomaccessfile (filename,
"R "); 
// File length, number of bytes 
Long filelength =
Randomfile. Length (); 
// Start position of the read object 
Int beginindex = (filelength> 4 )? 4:
0; 
// Move the start position of the read object to the beginindex position. 
Randomfile. Seek (beginindex ); 
Byte [] bytes = new
Byte [10]; 
Int byteread = 0; 
// Read 10 bytes at a time. If the file content is less than 10 bytes, read the remaining bytes. 
// Assign the number of bytes read at a time to byteread 
While (byteread = randomfile. Read (bytes ))! =
-1 ){ 
System. Out. Write (bytes, 0,
Byteread ); 

} Catch (ioexception e ){ 
E. printstacktrace (); 
} Finally { 
If (randomfile! = NULL ){ 
Try { 
Randomfile. Close (); 
} Catch (ioexception E1)





 
Private Static void showavailablebytes (inputstream
In ){ 
Try { 
System. Out. println ("number of bytes in the Current byte input stream:" +
In. Available ()); 
} Catch (ioexception e ){ 
E. printstacktrace (); 


Public static void main (string [] ARGs)

String filename =
"D:/t2.txt "; 
Readfromfile. readfilebybytes (filename ); 
Readfromfile. readfilebychars (filename ); 
Readfromfile. readfilebylines (filename ); 
Readfromfile. readfilebyrandomaccess (filename ); 

}

 

2. append the content to the end of the file. ===

 

Package com. thread. test;

Import
Java. Io. filewriter;

 
Import
Java. Io. ioexception; 
Import
Java. Io. randomaccessfile; 
 
Public class appendtofile

 
Public static void appendmethoda (string
Filename, 
 
String content ){ 
Try { 
// Open a random access file stream in read/write mode 
Randomaccessfile randomfile = new randomaccessfile (filename,
"RW "); 
// File length, number of bytes 
Long filelength =
Randomfile. Length (); 
// Move the Write File pointer to the end of the file. 
Randomfile. Seek (filelength ); 
Randomfile. writebytes (content ); 
Randomfile. Close (); 
} Catch (ioexception e ){ 
E. printstacktrace (); 

Public static void appendmethodb (string filename, string
Content ){
Try {
// Open a file writer. The second parameter true in the constructor indicates that the file is written as an append object. 
Filewriter writer = new filewriter (filename, true );
Writer. Write (content );
Writer. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public static void main (string [] ARGs ){
String filename = "D:/t2.txt ";
String content = "New append! ";
// Append an object by method 
Appendtofile. appendmethoda (filename, content );
Appendtofile. appendmethoda (filename, "APPEND end. N ");
// Display the File Content 
Readfromfile. readfilebylines (filename );
// Append an object by Method B 
Appendtofile. appendmethodb (filename, content );
Appendtofile. appendmethodb (filename, "APPEND end. N ");
// Display the File Content 
Readfromfile. readfilebylines (filename );
}
}

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.