Java Io operation code

Source: Internet
Author: User
---------- Reading data from standard device system. In ------------------
-----------------------------------------------------
Read byte: bufferedinputstream
Read characters: bufferedreader + inputstreamreader
----------------------------------------------
Import java. Io .*;


--------------------------------------------------------------------------------
----------------- The standard output system. Out is a print stream printstream ------------------

Import java. Io. *; public class printstandardoutput {public static void main (string [] ARGs ){
String myanswer = "no, and that's final ,";
System. Out. println ("Hello world of Java ");
System. Out. println ("the answer is" + myanswer + "at this time."); printwriter PW = new printwriter (system. Out );
PW. println ("the answer is" + myanswer + "at this time."); int I = 42;
PW. println (I + '=' + "the answer .");
PW. println ("Note:" + I + '=' + "the answer .");
PW. println (I + "=" + "the answer .");
PW. println (I + ('=' + "the answer."); PW. Close ();
}
}

Bytes -----------------------------------------------------------------------------------
--------------------- To read (output to-) a text file -----------------------------

Bufferedreader is = new bufferedreader (New filereader ("XXXX. Text"); read
Bufferedoutputstream byteout = new bufferedoutputstream (New fileoutputstream ("XX. dat "));
// Write to text!

-----------------------------------------------
At 04-09-20 :49
Aeonsun said:
Import java. Io .*;
Public class openfilebyname {
Public static void main (string [] ARGs) throws ioexception {
Bufferedreader is = new bufferedreader (New filereader ("myfile.txt "));
Bufferedoutputstream bytesout = new bufferedoutputstream (
New fileoutputstream ("bytes. dat "));

// Code here to read from is, write to bytesout

Bytesout. Close ();
}
}
 
At 04-09-20
Aeonsun said:
Import java. Io .*;

Public class fileio {

Private fileio (){}

Public static void copyfile (string inname, string outname)
Throws filenotfoundexception, ioexception {
Bufferedinputstream is =
New bufferedinputstream (New fileinputstream (inname ));
Bufferedoutputstream OS =
New bufferedoutputstream (New fileoutputstream (outname ));
Copyfile (is, OS, true );
}

/** Copy a file from an opened inputstream to opened outputstream */
Public static void copyfile (inputstream is, outputstream OS, Boolean close)
Throws ioexception {
Int B; while (B = is. Read ())! =-1 ){
OS. Write (B );
}
Is. Close ();
If (close)
OS. Close ();
}

Public static void copyfile (reader is, writer OS, Boolean close)
Throws ioexception {
Int B; while (B = is. Read ())! =-1 ){
OS. Write (B );
}
Is. Close ();
If (close)
OS. Close ();
}

Public static void copyfile (string inname, printwriter PW, Boolean close)
Throws filenotfoundexception, ioexception {
Bufferedreader is = new bufferedreader (New filereader (inname ));
Copyfile (is, PW, close );
}

Public static string Readline (string inname)
Throws filenotfoundexception, ioexception {
Bufferedreader is = new bufferedreader (New filereader (inname ));
String line = NULL;
Line = is. Readline ();
Is. Close ();
Return line;
}

Protected static final int blksiz = 8192;

Public void copyfilebuffered (string inname, string outname) throws
Filenotfoundexception, ioexception {
Inputstream is = new fileinputstream (inname );
Outputstream OS = new fileoutputstream (outname );
Int COUNT = 0;
Byte B [] = new byte [blksiz];
While (COUNT = is. Read (B ))! =-1 ){
OS. Write (B, 0, count );
}
Is. Close ();
OS. Close ();
}

Public static string readertostring (reader is) throws ioexception {
Stringbuffer sb = new stringbuffer ();
Char [] B = new char [blksiz];
Int N;

While (n = is. Read (B)> 0 ){
SB. append (B, 0, N );
}

Return sb. tostring ();
}

Public static string inputstreamtostring (inputstream is)
Throws ioexception {
Return readertostring (New inputstreamreader (is ));
}

Public static void stringtofile (string text, string filename)
Throws ioexception {
Bufferedwriter OS = new bufferedwriter (New filewriter (filename ));
OS. Write (text );
OS. Flush ();
OS. Close ();
}

Public static bufferedreader openfile (string filename)
Throws ioexception {
Return new bufferedreader (New filereader (filename ));
}
}
 
At 04-09-20
Aeonsun said:
Import Java, Io .*;

Class fileprocess
{

Public static void main (string ARGs [])
{
Int B;
Byte buffer [] = new byte [1000];
Try
{B = system. Out. Read (buffer); // access data for writing!
Fileoutputstream out = new fileootputstream ("line. Text ");
Out. Write (buffer, 0, B); // write it! Note the relationship between the byte buffer and the Chinese part stream
]
Catch (ioexception E)
{System. Out. println ("error ");
}
]
}
At 04-09-20
Aeonsun said:
Import Java, Io .*;

Public class read
{
Public static void main (string ARGs [] 0
{
Int B;
Byte buffer = new byte [2000];
Try
{Fileinputstream readfile = new fileinputstream ("XXXX. Java ");
B = readfile (buffer, 0,25000 );
Try {string STR = NRE string (buffer, 0, B, "default"); // construct a String object!
System. Out. println (STR );}
Catch (unsuportedencodeingexception E)
{E. printstacktrace ();}
Catch (ioexception E)
{System. Out. println ("error ");}
}
}
At 04-09-20 :51
Aeonsun said:

-----------------------------------------------------------------------
Import java. Io .*;
Import java. AWT .*;
Import javax. Swing .*;
Import java. AWT. event .*;

--------------- Use bufferedreader -- filereader to read text files! -----------

--------------- Always pay attention to the byte stream or the upstreaming stream -------------------- in Io ----------------------

 

Class filewindow extends jframe implements actionlistener
{
Jtextarea text;
Bufferedreader in;
Jbutton button;
Filereader file;
Filewindow ()
{
Super ("File Upload stream ");
Container con = getcontentpane ();
TEXT = new jtextarea (50, 50 );
Text. setbackground (color. Blue );
Try {
File F = new file ("E: // a.txt ");
File = new filereader (f );
In = new bufferedreader (File );
/** Bufferedreader (Reader in) constructor,
* Access bufferedreader to read stream filereader from file character
* Stream in order to use the bufferedreader object method Readline () to efficiently read rows!
*/

}
Catch (filenotfoundexception e ){}
Catch (ioexception e ){}
Button = new jbutton ("read ");
Button. addactionlistener (this );
Con. setlayout (New borderlayout ());
Setsize (300,200 );
Setvisible (true );

Con. Add (text, "center ");
Con. Add (button, "South ");
Addwindowlistener (New windowadapter ()
{Public void windowclosing (invalid wevent E)
{Setvisible (false); system. Exit (0 );}});

}
Public void actionreceivmed (actionevent E)
{
String S;
If (E. getsource () = button)
Try {
While (S = in. Readline ())! = NULL)
Text. append (S + '/N ');
// Here, you can also use bufferstring to temporarily Save the read character data!
}
Catch (ioexception E1 ){}
}
// --------- Main ()----------
Public static void main (string ARGs [])
{
Filewindow win = new filewindow ();
Win. Pack ();
}
}
 
At 04-09-20 :51
Aeonsun said:
----------------- Randomaccessfile randomly reads the file ---------------
Import java. Io .*;

Public class randomread
{
Final Static string filename = "E: // a.txt ";
Protected string filename;
Protected randomaccessfile seeker;

Public static void main (string [] argv) throws ioexception {
Randomread r = new randomread (filename );

System. Out. println ("offset is" + R. readoffset ());
System. Out. println ("message is/" "+ R. readmessage () + "/".");
}

/** Constructor: Save filename, construct randomaccessfile */
Public randomread (string fname) throws ioexception {
Filename = fname;
Seeker = new randomaccessfile (fname, "RW ");
}

/** Read the offset field, defined to be at location 0 in the file .*/
Public int readoffset () throws ioexception {
Seeker. Seek (0 );
Seeker. writechars (filename); // move to very beginning
Return Seeker. readint (); // and read the offset
}

/** Read the message at the given offset */
Public String readmessage () throws ioexception {
Seeker. Seek (120); // move to where
Return Seeker. Readline (); // and read the string
}
}
At 04-09-20 :51
Aeonsun said:
Some comments: axman

I did not want to talk about anything, but I still wrote it out in the spirit of being technically responsible.

I/O understanding is level 3 (if Java Io has level 10)
Too many errors.
Unfamiliar with I/O layers
Java Io mainly includes
Java. Io package and Java. Nio package.

Java. Io mainly extends from four interfaces:
Byte:
Inputstream/outputstream, Which is encapsulation, filtering, and specific implementation class for specific object processing.
Character:
Reader/writer (in the original article, all writer interfaces are written as write, which means you do not understand this interface at all. If you often use the writer interface, how can you tell the difference between writer and write, not a mistake, but all of them are write)

Among the above four interfaces, the underlying layer is all the blocked stream of Operation bytes.

Java. Io is mainly based on block Operation blocks (buffer). Through the blocking and non-blocking modes that can be set, the data output is greatly improved.
It also controls the channel by selecting the selector mode, so that multiple users can concurrently transfer data in the same output input channel. for example, a socket port can be concurrently accessed by an infinite number of clients (theoretically unlimited. is the classic I/O multiplexing technology.
 

Public class Systemin
{
Public static void main (string ARGs [])
{Try {// Stream Conversion!
Bufferedreader is = new bufferedreader (New inputstreamreader (system. In ))
String inputline = NULL;
While (inputline = is. Readline ())! = NULL)
System. Out. println (inputline );
Is. Close ();
}
Catch (ioexception E)
{System, out. println ("ioxe:" + E );
}
}
}

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.