Code
Import java. Io. * ;
Import java. NiO. * ;
Import java. NiO. channels. * ;
Public Class New {
Public Static Void Main (string ARGs []) {
Fileinputstream fin;
Filechannel fchan;
Long Fsize;
Bytebuffer mbuf;
Int I;
Try {
Fin = New Fileinputstream ( " Yourtext.txt " ); // Absolute path of your TXT file
Fchan = Fin. getchannel ();
Fsize = Fchan. Size ();
Mbuf = Bytebuffer. Allocate (( Int ) Fsize );
Fchan. Read (mbuf );
Mbuf. Rewind ();
For (I = 0 ; I < Fsize; I ++ ){
System. Out . Print (( Char ) Mbuf. Get ());
}
Mbuf. Clear ();
Fchan. Close ();
Fin. Close ();
}
Catch (Ioexception e ){
System. Out . Println ( " No file " );
}
}
}
Code
Import java. Io. bufferedreader;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. outputstreamwriter;
Import java. Io. printstream;
Import java. Io. printwriter;
Public Final Class Accesstextfile {
/* *
* 1. Read the text in the stream into a stringbuffer.
* @ Throws ioexception
*/
Public Void Readtobuffer (stringbuffer buffer, inputstream Is )
Throws ioexception {
String line; // Used to save the content read by each row
Bufferedreader = New Bufferedreader ( New Inputstreamreader ( Is ));
Line = Reader. Readline (); // Read the first line
While (Line ! = Null ){ // If line is empty, the read is complete.
Buffer. append (line ); // Add read content to buffer
Buffer. append ( " \ N " ); // Add linefeed
Line = Reader. Readline (); // Read the next row
}
}
/* *
* 2. Demonstrate reading the content in stringbuffer into the stream
*/
Public Void Writefrombuffer (stringbuffer buffer, outputstream OS ){
// Printstream can be used to conveniently output content to the output stream.
// The object usage is the same as that of system. Out.
// (System. Out itself is a printstream object)
Printstream PS = New Printstream (OS );
PS. Print (buffer. tostring ());
}
/* *
* 3 *. Copy the content from the input stream to the input stream.
* @ Throws ioexception
*/
Public Void Copystream (inputstream Is , Outputstream OS) throws ioexception {
// For the read process, see the comments in readtobuffer.
String line;
Bufferedreader = New Bufferedreader ( New Inputstreamreader ( Is ));
Printwriter writer = New Printwriter ( New Outputstreamwriter (OS ));
Line = Reader. Readline ();
While (Line ! = Null ){
Writer. println (line );
Line = Reader. Readline ();
}
Writer. Flush (); // Are you sure you want to write all the items in the output stream?
// Writer is not closed because the OS is passed in from outside.
// Since it is not opened from here, it will not be closed from here
// If the writer is closed, the OS encapsulated in it will be shut down.
}
/* *
* 3. Call the copystream (inputstream, outputstream) method to copy text files.
*/
Public Void Copytextfile (string infilename, string outfilename)
Throws ioexception {
// Generate the corresponding input/output stream based on the input/output file
Inputstream Is = New Fileinputstream (infilename );
Outputstream OS = New Fileoutputstream (outfilename );
Copystream ( Is , OS ); // Copy content using copystream
Is . Close (); // Is Enabled here, so you need to disable
OS. Close (); // OS is enabled here, so you need to disable
}
Public Static Void Main (string [] ARGs) throws ioexception {
Int SW = 1 ; // Three test options
Accesstextfile Test = New Accesstextfile ();
Switch (SW ){
Case 1 : // Test read
{
Inputstream Is = New Fileinputstream ( " E: \ test.txt " );
Stringbuffer Buffer = New Stringbuffer ();
Test. readtobuffer (buffer, Is );
System. Out . Println (buffer ); // Write the content in the buffer.
Is . Close ();
Break ;
}
Case 2 : // Test write
{
Stringbuffer Buffer = New Stringbuffer ( " Only a test \ n " );
Test. writefrombuffer (buffer, system. Out );
Break ;
}
Case 3 : // Test copy
{
Test. copytextfile ( " E: \ test.txt " , " E: \ r.txt " );
}
Break ;
}
}
}