Java-based I/O Stream Integrated exercises and java-based I/O exercises
1 * byte streams include: FileInputStream and FileOutputStream ).
2 * character streams include: Character Input streams (FileReader), and FileWriter );
Byte stream> byte input stream is nothing more than reading files. Beginners can think so and put the reading of documents in the buffer zone (assuming this buffer zone is available)
> The byte output stream is read from the buffer zone of this assumption.
Streaming> is also true. However, a streaming can only operate plain text files. It is developed to operate text files. What is a plain text file ??
A plain text file is a file that can be opened with notepad.
In the later stage, we will use the ghost stream to operate these plain text files, read the file class content, print it on the console, or input a string manually to the document in a moderate level;
I. Through exercises, we can understand byte streams:
Today, we will share with you all the methods for copying byte streams.
/*
* Enter a folder address on the keyboard and copy the file to another folder. Only the. java file is copied.
* Analysis: 1. When we get this question, we still need to solve the big problem and small problem.
* (1) keyboard input> to determine whether a folder path is correct> WE can encapsulate the input string into a File and use exists () to determine whether a return value of the boolean type exists.
* (2) Get the correct folder address. we started to choose what stream to use for copying. Just now I also said that the bytes stream is suitable for operating plain text files. Therefore, we were able to directly select a byte stream for non-plain text.
* (2_1) someone will ask, what about the buffer stream ?? Don't worry. In fact, the buffer stream is a byte stream of service. You can think of it as an 8-cylinder car engine, changing the car into a plane pleasure. here you should also understand
* What does the buffer stream do? That is, it is used to speed up reading and writing files from byte compaction streams. In fact, the underlying layer is a buffer zone with 8*1024 bytes.
* (3) When copying a folder, there cannot be only files and subfolders in the folder. We will use recursion here. Oh, it's very troublesome. You will find that when we make code later, that's the case.
* (4) How can we tell if there are folders and files in the folder? File provides me with a method to determine: isFile () to determine the File. isDirectory () to determine the folder
* (5) What should they say they put them there first? Put these folders and files in an array? But there is no such method. This File provides me with a special array File []
*/
Come on> start to answer questions
Step 1: Enter the keyboard >>> to determine whether the folder path is correct
Private static File getask (){
// Enter keyboard SC = new keyboard (System. in); System. out. println ("Enter the folder path:"); while (true) {String string = SC. nextLine ();
// Encapsulate the keyboard input File address file = new File (string );
// Determine whether the folder address exists if (! File. exists () {System. out. println ("the file path you entered does not exist. Please enter it again :");
// Determine whether it is a file address} else if (file. isFile () {System. out. println ("You entered a file, not a folder. Please enter it again:");} else {
// Return the folder address return file ;}}}
Step 2: Copy
Private static void copy (File src, File dest) throws IOException {// get the source file address of the two folders just entered. The source file address is src, and the address of the folder to be received is dest. // put the files in the source folder, the word folder is stored in the File array src. listFiles (); method to get File [] files = src. listFiles (); // determines whether the array is empty. if (files! = Null) {// traverse the array for (File file: files) {// if it is a file and ends with java, geName () is used to get the name string of the folder or File if (file. isFile () & file. getName (). endsWith (". java ") {// The qualified copy // buffer stream is coming. Remember to keep up with our byte stream after acceleration, don't install the engine, don't get into the car. // byte input stream for beginners: you can think that reading files to the buffer zone BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file )); // The Byte output stream obtains the buffer content to another object. Here we will explain why the buffer content is stored in the new File (the File address is stored here) // The address of the folder to be received is dest plus the file address // E: \ dest \ xxx. ja Va is spliced into a complete File address BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (new file (dest, File. getName (); // creating a byte array here is actually making file copying faster byte [] by = new byte [1024*8]; // when the index copy is complete,-1 is returned without an array; int len = 0; while (len = bis. read ())! =-1) {// write bos to it. write (by, 0, len);} // close the stream bos. close (); bis. close ();} else if (file. isDirectory () {// create a remove () method to interpret a folder that can contain java files that do not contain folders that are not copied if (remove (file )) {File newfile = new File (dest, file. getName (); if (! Newfile. exists () {// create a folder named newfile in the folder. mkdirs () ;}copy (file, newfile) ;}}} private static boolean remove (File file) {File [] files = file. listFiles (); // first, judge whether the folder is empty. if (files! = Null) {for (File file2: files) {// returns true if (file2.isFile () & file2.getName () if the underlying folder contains files that meet the conditions (). endsWith (". java ") {return true; // determines whether a folder exists. // whether a java file exists at the underlying layer} else if (file2.isDirectory () & remove (file2 )) {return true ;}} return false ;}
The above code may be difficult to understand at the moment. The time is the reason. Next time I update a more detailed
Step 3: main () method
public static void main(String[] args) throws IOException { File src = getScanner(); File dest = getScanner(); copy(src,dest); }
Stream> question
/*
* The first line in the document is exchanged with the last line.
* The second row is exchanged with the last and second rows.
*
*/
Public static void main (String [] args) throws IOException {BufferedReader br = new BufferedReader (new FileReader ("abc \ B .txt ")); bufferedWriter bw = new BufferedWriter (new FileWriter ("abc \ c.txt"); ArrayList <String> list = new ArrayList <> ();
// Byte stream. len is used! =-1 Index
// There is a method in the document that can be read in one row. Of course, you can read String line = null as the byte stream; while (line = br. readLine ())! = Null) {list. add (line) ;}br. close (); // switch between the first line and the last line in the document for (int I = 0; I <list. size ()/2; I ++) {String string = list. get (I );
// Only the length of the set needs to be exchanged/2 times
List. set (I, list. get (list. size ()-1-i); list. set (list. size ()-1-i, string);} // traverse and write the String to the file for (string: list) {bw. write (string); bw. newLine ();}
// Close the stream bw. close ();}
Today I have said so much. Let's look back at what I wrote ........ I did not know what I was talking about. next time, you must come up with a detailed description. I/O streams must be detailed. Some may, but remember that I/O is nothing more than in the initial stage.
This is an accelerator for operating folder files, copying, deleting, writing, and reading. Reading streams. Read-Only only copying your own streams. Do not forget to buffer the streams. Thank you!