On the occasion of the new Year I began my first blog, I wish you a Happy New year, the study of work progress, I in the Java programming in the study will have some of their own experience or learn to share, but also hope to learn together, below I will learn I/O flow related issues and share learning.
I/O flow is an important part of our future Java programming, and we learn that Java must operate on files, and I/O flow is the basis of our operations, so we need to master four-stream usage, in Java we can divide the flow into two types of input and output, Or you can divide by byte stream and character stream.
namely: output stream has, character output stream, byte output stream.
The input stream has a character output stream, a byte output stream.
In bytes and characters, the byte stream and the character stream each have their own input and output methods.
Let's first introduce the byte input stream:
First we need to use the package needs to import first
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
public class Text01 {
public static void Main (string[] args) throws FileNotFoundException {
Because it is a byte input stream, we will read out the contents of the file and choose the path below the path to find the file in FileInputStream ("D:\\java.txt") to write our path later.
But the class itself throws an exception so we need to choose how to handle it, either continue to throw it up, or use the Try-catch method to handle it.
FileInputStream fis = new FileInputStream ("D:\\java.txt");
In the process of using this method, you need a byte array to act as a buffer, and the stream of bytes we read will first exist in the buffer.
Byte[] bytes= new byte[1024];
int A;
try {
We use the Read method of the byte input stream to read and read the data into a byte array.
A = Fis.read (bytes);
while (A!=-1) {
String c= new String (bytes);
After reading the byte array can be converted to a string, while using the loop method, in order to avoid the length of the byte array to the limit caused by the reading is not complete.
System.out.println (c);
A= fis.read (bytes);
}
} catch (IOException e) {
E.printstacktrace ();
}finally{
try {
Finally we want to close the stream, we'll report an exception when we close it, we'll handle it here.
Fis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Next we introduce the byte output stream:
Byte output stream and byte input stream there are a lot of acquaintances, first of all we have to do is also the guide package.
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class Shuchuliuoutput {
public static void Main (string[] args) throws IOException {
Same byte input stream we need to select the file path, we can customize the folder name, we can write the value of true after the path, indicating that the original file content is not overwritten, otherwise it will overwrite the previous.
FileOutputStream fos = new FileOutputStream ("D:\\writeh", true);
Create a content to output
string BC = new String ("Haohaohao");
Converts the input into a byte array
Byte[] Bytes=bc.getbytes ();
Use the Write method of the FileOutputStream to output the write, the same way this method will have an exception can continue to throw or processing.
Fos.write (bytes,0,bytes.length);
Close the stream
Fos.close ();
}
}
We're looking at the character input stream:
The process is much like the byte input stream, except that the byte array is replaced with a character array
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
public class Zifishuchu {
public static void Main (string[] args) throws IOException {
FileReader FR = new FileReader ("D:\\java.txt");
char[] cc = new char[1024];
int data;
while ((Data=fr.read (cc))!=-1) {
String Aa=new string (cc);
SYSTEM.OUT.PRINTLN (AA);
}
Fr.close ();
}
}
We're looking at the character output stream:
Import Java.io.FileWriter;
Import java.io.IOException;
public class Writeshuchu {
public static void Main (string[] args) throws IOException {
FileWriter FW = new FileWriter ("D:\\wenjian.txt");
String cc = "good study, day up";
char[] AA = Cc.tochararray ();
Fw.write (AA, 0, aa.length);
Fw.close ();
}
}
Use of I/O streams in Java