1. copy a file as a file stream
Copy an existing file to a specified location. In this example, copy the source file "/home/hadoop/scene1" and rename it "/home/hadoop/scene1bak"
CodeAs follows:
View code
Package ERP. SJTU. XW; Import Java. Io. file; Import Java. Io. fileinputstream; Import Java. Io. fileoutputstream; Import Java. Io. ioexception; Public Class Copyfile { Public Static Void Main (string ARGs []) Throws Ioexception {copyfile cf = New Copyfile (); string SRC = "/Home/hadoop/scene1" ; String dest = "/Home/hadoop/scene1bak" ; Cf. copyfile (SRC, DEST );} /** Copy a file as a file stream * @ Param SRC file source directory * @ Param Target directory of the Dest file * @ Throws Ioexception * This method is tested and supports Chinese processing and can be copied to multiple formats, such as txt, XML, JPG, and Doc. */ Public Void Copyfile (string SRC, string DEST) Throws Ioexception {fileinputstream in = New Fileinputstream (SRC ); // Define input stream File file = New File (DEST ); // Definition file. The specified location "DEST" may already exist. If it does not exist, you need to create it. If (! File. exists ()) // If the object does not exist, call the file creation method. File. createnewfile (); fileoutputstream out = New Fileoutputstream (File ); // Define output stream Int C; Byte Buffer [] = New Byte [1024]; // Defines the buffer size. /** * Read the text in the input file into the buffer. * If the text size is smaller than the buffer capacity, you can read it once. * If the buffer size is greater than the buffer size, multiple reads are required. */ While (C = in. Read (buffer ))! =-1) // Read the text content from the input stream and read it into the buffer. {System. Out. println (C ); // Length of the text read in the output buffer For ( Int I = 0; I <C; I ++ ) Out. Write (buffer [I]); // Inhaled the content in the buffer into the output stream. } In. Close (); Out. Close ();}}
Output result:
1024
89
The size of the scene1 file is (1113 = 1024 + 89) bytes. 2. obtain the information entered by the console user.
ThisProgramThe user obtains the information entered in the console. The Code is as follows:
View code
Package ERP. SJTU. XW; Import Java. Io. ioexception; Public Class Getconsoleinputmessage { Public Static Void Main (string ARGs []) Throws Ioexception {getconsoleinputmessage instance = New Getconsoleinputmessage (); string Str =Instance. getinputmessage (); system. Out. println (STR); system. Exit ( 0 );} /** Obtain information entered by the console user * @ Return * @ Throws Ioexception * can return user input information. The disadvantage is that Chinese input is not supported and further improvement is required. */ Public String getinputmessage () Throws Ioexception {string Str = Null ; System. Out. println ( "Enter the command :" ); Byte Buffer [] = New Byte [1024]; // Create a buffer Int Count = system. In. Read (buffer ); // Write the input in the console to the buffer and return the buffer String Length. System. Out. println (count ); // Char ch [] = new char [count-2]; // The last two digits in the buffer are the Terminator, Which is meaningless. Therefore, it is deleted. Char Ch [] = New Char [Count-1]; // Defines an array of characters. The length is Count-1. Because the last character in the buffer is the Terminator, it is meaningless. Therefore, it is deleted. For ( Int I = 0; I <count-1; I ++ ) {Ch [I] = ( Char ) Buffer [I]; // STR = new string (CH ); // System. Out. println (STR ); } Str = New String (CH ); // Converts an array of characters into a string. Return STR ;}}
Output result:
Enter the command: Please input you commond25please input you commond
Note: here, the buffer length of input and output is 25, but in fact there are only 24 characters in the string, because the last bit in the buffer is the Terminator.
3. Use printstream to write files
Use printstream to write text to an empty file. The Code is as follows:
View code
Package ERP. SJTU. XW; Import Java. Io. file; Import Java. Io. filenotfoundexception; Import Java. Io. fileoutputstream; Import Java. Io. ioexception; Import Java. Io. printstream; Public Class Printstreamdemo { Public Static Void Main (string ARGs []) Throws Ioexception {printstreamdemo PSD = New Printstreamdemo (); PSD. printstreamdemo (); // Test printstreamdemo () PSD. printstreamdemo2 (); //// Test printstreamdemo2 () } Public Void Printstreamdemo ()Throws Ioexception { Try {String SRC = "/Home/hadoop/printstreamdemo "; // Source File Path File file = New File (SRC ); // Definition file. If the file specified by the path does not exist, it is created. If (! File. exists () file. createnewfile (); fileoutputstream out = New Fileoutputstream (File );// Define file output stream Printstream PS = New Printstream (out ); // Print content to the file output stream For ( Int I = 0; I <10; I ++ ) PS. println ( "This is" + I + "line ." );} Catch (Filenotfoundexception e) {e. printstacktrace ();}} /** * Use printstream to write files */ Public Void Printstreamdemo2 () Throws Ioexception { Try { /** * Note: fileoutputstream out = new fileoutputstream ("/home/hadoop/printstreamdemo2"); * If the local directory contains a file printstreamdemo3, the text is written to the file. * If the local directory does not contain the printstreamdemo3 file, the file is automatically created and text is written to it. * This is the operation set of the following code * string src = "/home/hadoop/printstreamdemo"; // The source file path * file = new file (SRC); // defines the file, if the file specified by the path does not exist, the * If (! File. exists () * file. createnewfile (); * fileoutputstream out = new fileoutputstream (File); // defines the file output stream */ Fileoutputstream out = New Fileoutputstream ("/home/hadoop/printstreamdemo2" ); Printstream PS = New Printstream (out ); For ( Int I = 0; I <10; I ++ ) PS. println ( "This is" + I + "line ." );} Catch (Filenotfoundexception e) {e. printstacktrace ();}}}
Run the above Code to automatically generate the printstreamdemo and printstreamdemo2 files. The text content of these two files is:
This is 0 line. this is 1 line. this is 2 line. this is 3 line. this is 4 Line. this is 5 line. this is 6 line. this is 7 line. this is 8 line. this is 9 line.
4. Use stringbuffer to write files
The Code is as follows:
View code
Package ERP. SJTU. XW; Import Java. Io. file; Import Java. Io. fileoutputstream; Import Java. Io. ioexception; Public Class Stringbufferdemo { Public Static Void Main (string ARGs [])Throws Ioexception {stringbufferdemo SBD = New Stringbufferdemo (); // Create an object instance SBD. stringbufferdemo (); // It is awkward to call a method with the same name as a class. } /** * Use stringbuffer to write files * This method can be used to set the encoding to effectively solve Chinese problems. * @ Throws Ioexception */ Public Void Stringbufferdemo () Throws Ioexception {string SRC = "/Home/hadoop/stringbufferdemo" ; File File = New File (SRC ); If (File. exists () file. createnewfile (); fileoutputstream out = New Fileoutputstream (file, True ); For (Int I = 0; I <10; I ++ ) {Stringbuffer sb = New Stringbuffer (); sb. append ( "This is the" + I + "line. /N "); // If "/N" is not added, the line feed cannot be implemented. Out. Write (sb. tostring (). getbytes ("UTF-8" );} Out. Close ();}}
Run the above Code to automatically generate the stringbufferdemo file. The text content is:
This is row 0th. This is row 1st. This is row 2nd. This is row 3rd. This is row 4th. This is row 5th. This is row 6th. This is row 7th. This is row 8th. This is row 9th.
PS: Java stream concept (2011-10-13)
Stream is one of the most important basic concepts in Java. Stream is used for file reading and writing, network sending and receiving, and process communication. What is stream used? Is used for input and output. Why does the input and output use the "stream" method? Because the basic unit of input and output is byte, input is to get a string of bytes, and output is to send a string of bytes. However, in many cases, it is impossible for the program to process all the bytes, but to process a little bit. For example, if you download World of Warcraft, you cannot download it all to the memory and save it to the hard disk. Instead, you can download it and save it. In this case, the stream method is very suitable. In Java, each stream is an object. There are two types of streams: inputstream and outputstream ). For an input stream, you only need to extract bytes from the stream without stopping. For an output stream, you only need to pass the prepared byte string to it.
________ Java program
|
External System -- | -- (input stream) --> processing logic -- (output stream) --- | --> External System
| __________________________ |
In general, we need to imagine the JAVA input stream and output stream from the memory perspective. The input stream is "We Are the memory, and the external system has content to be input into the memory, so it is called the input stream ". The output stream is "We Are the memory. We want to output data from the memory and write it to the external system, so it is called the output stream ."