Java IO test example-byte stream-example stream

Source: Internet
Author: User

//***

* Java IO detailed description see http://www.senma.org/blogs/356.html

* You can also refer to: http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

*/

Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. PipedInputStream;
Import java. io. PipedOutputStream;
Import java. io. PrintStream;
Import java. io. SequenceInputStream;
Import java.net. URL;
Import java.net. URLConnection;
/****
*
* @ Author masen
* Http://weibo.com/masen
* Http://www.senma.org/
*
*/
Public class ReadFileTest {

/***
* The text is read by the upstream stream.
* @ Throws IOException
*/
Public static void readerReadFile (String filename)
{
// BufferedReader reader = new BufferedReader (new FileReader (name ));
// Because Filereader inherits InputStreamReader, both methods are OK.
BufferedReader reader = null;
Try {
Reader = new BufferedReader (new InputStreamReader (new FileInputStream (filename )));
String s1 = "";
While (s1 = reader. readLine ())! = Null)
{
System. out. println (s1 );
}

} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally
{
Try {
If (reader! = Null) reader. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/***
* Byte streams read text
* Byte streams read web information. Likewise
* @ Throws IOException
*/
Public static void streamReadFile (String filename)
{
FileInputStream stream = null;
Try {
Stream = new FileInputStream (filename );
Byte [] temp = new byte [1, 1024];
Int len = 0;
While (len = stream. read (temp ))! =-1)
{// Note that because the temp cache is a fixed term, not all output
System. out. print (new String (temp, 0, len ));
}

} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally
{
Try {
If (stream! = Null) stream. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/***
* The Network reads data and uses the RST stream.
* Here we can see that InputstreamReader is a bridge between stream and reader.
* @ Param url
*/
Public static void readerReadWeb (String url)
{
BufferedReader reader = null;
Try {
URLConnection con = new URL (url). openConnection ();
Reader = new BufferedReader (new InputStreamReader (con. getInputStream ()));
String s1 = "";
While (s1 = reader. readLine ())! = Null)
{
System. out. println (s1 );
}

} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally
{
Try {
If (reader! = Null) reader. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/***
* Recursively listing Directories
*
* @ Param dir current file directory
* @ Param name: current file name
* @ Param deep current recursive depth
* @ Param dept maximum depth allowed
*/
Public static void listDir (String dir, String name, int deep, int dept)
{
File f = new File (dir + File. separator + name );
For (int I = 0; I <deep; I ++)
System. out. print ("");
If (f. isDirectory ())
{
String [] dirs = f. list ();
System. out. print ("--" + name );

If (dirs = null | dirs. length = 0)
{
Return;
}
System. out. print ("\ r \ n ");
For (String s: dirs)
{
If (deep + 1) <dept)
ListDir (dir + File. separator, s, deep + 1, dept );
}
}
Else
{
System. out. print ("--" + name );
System. out. print ("\ r \ n ");
}

}

/***
* Writing byte streams to files
* @ Param file
* @ Param content
*/
Public static void streamWriteFile (String name, String content)
{
File f = new File (name );
FileOutputStream stream = null;
Try {
If (! F. exists ())
{// The file does not exist
F. createNewFile ();
Stream = new FileOutputStream (f );
Byte [] temp = content. getBytes ();
Stream. write (temp );
}
Else
{// Append if the file exists
Stream = new FileOutputStream (f, true );
Byte [] temp = content. getBytes ();
For (int I = 0; I <temp. length; I ++)
Stream. write (temp [I]);
}

} Catch (IOException e ){
E. printStackTrace ();
}
Finally
{
Try {
If (stream! = Null)
Stream. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

/***
* The volume stream writes files.
* @ Param file
* @ Param content
*/
Public static void writerWriteFile (String name, String content)
{
File f = new File (name );
FileWriter stream = null;
Try {
If (! F. exists ())
{// The file does not exist
F. createNewFile ();
Stream = new FileWriter (f );
Stream. write (content );
}
Else
{// Append if the file exists
Stream = new FileWriter (f, true );
Stream. write (content );
}

} Catch (IOException e ){
E. printStackTrace ();
}
Finally
{
Try {
If (stream! = Null)
Stream. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

/***
* Pipeline byte stream Test
* The pipeline flow test is similar.
*
*/
Public class PipeWriter implements Runnable
{
Private PipedOutputStream out = null;
Private String content;
Public PipeWriter ()
{
Out = new PipedOutputStream ();
}

Public PipedOutputStream getOut (){
Return out;
}

Public String getContent (){
Return content;
}

Public void setContent (String content ){
This. content = content;
}

Public void run (){
While (content! = Null &&! Content. equals (""))
{
Try {
Out. write (content. getBytes ());
Content = "";
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}

}

Public class PipeReader implements Runnable
{
Private PipedInputStream in = null;
Public PipeReader ()
{
In = new PipedInputStream ();
}

Public void run (){
Byte [] buf = new byte [1, 1024];
Try {
Int len = in. read (buf );
System. out. println (new String (buf, 0, len ));
// In. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

Public PipedInputStream getIn (){
Return in;
}

}

/***
* Printstream Test
* It can be seen that printstream does not directly target the specific output stream form.
* Compared with FileOutputStream of the same type, FileOutputStream is more abstract, which is equivalent to the encapsulation interface of write operations: It submits the written content to the output stream, and the specific implementation is completely ignored.
*
* System. out can be redirected to the corresponding stream.
* Similarly, this can be true for System. in. No more examples.
*/
Public static void printStreamTest ()
{
PrintStream print = null;
Try {
Print = new PrintStream (new FileOutputStream (new File ("d :"
+ File. separator + "hello.txt"), true ));
Print. println ("I Like You ");
Print. printf ("Name: % s. Age: % d.", "Yang Mi", 15 );
Print. close ();
Print = new PrintStream (System. out );
Print. println ("I Like You ");
Print. printf ("Name: % s. Age: % d.", "Yang Mi", 15 );
Print. close ();

// Redirect the system output stream, such as log4j
Print = new PrintStream (new FileOutputStream (new File ("d :"
+ File. separator + "hello.txt"), true ));
System. setOut (print );
System. out. println ("kill you ");
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

/****
* Sequenceinputstream tests and merges two input streams in sequence.
* @ Param name1
* @ Param name2
*/
Public static void sequenceInput (String name1, String name2)
{
Try {
InputStream a = new FileInputStream (name1 );
InputStream B = new FileInputStream (name2 );
SequenceInputStream s = new SequenceInputStream (a, B );
BufferedReader r = new BufferedReader (new InputStreamReader (s ));
String t = "";
While (t = r. readLine ())! = Null)
{
System. out. println (t );
}
A. close ();
B. close ();
S. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

/**
* @ Param args
*/
Public static void main (String [] args ){
// StreamReadFile ("c:/t1.txt ");
// ReaderReadFile ("c:/t1.txt ");
// ListDir ("d:", "", 0, 4 );
// StreamWriteFile ("d:/1.txt"," Yang Mi ");
// StreamWriteFile ("d:/1.txt"," ");
// WriterWriteFile ("d:/2.txt"," Yang Mi ");
// WriterWriteFile ("d:/2.txt"," ");

/***
ReadFileTest t = new ReadFileTest ();
PipeWriter x = t. new PipeWriter ();
PipeReader y = t. new PipeReader ();
Thread w = new Thread (x );
Thread r = new Thread (y );
Try {
X. getOut (). connect (y. getIn ());
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
X. setContent ("111 ");
W. start ();
R. start ();
X. setContent ("111 ");
**/

/// PrintStreamTest ();

SequenceInput ("d:/1.txt"," d:/2.txt ");

}
}

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.