1, the inheritance of the stream, as well as the byte stream and character stream.
2, Node stream fileoutputstream and FileInputStream and processing flow bufferedinputstream and Bufferedoutputstream. and the corresponding fileoutputwriter,fileinputreader,bufferedinputreader,bufferedoutputwriter.
3. Conversion Flow InputStreamReader and OutputStreamWriter
One: The inheritance relationship of the stream
BYTE stream
Character Stream
The use of character streams and byte streams: Byte streams are typically used to process images, videos, and Ppt,word types of files. Character streams are typically used to work with plain text types of files, such as TXT files, which can be used to process plain text files, but character streams cannot be used to process non-text types of files such as image video.
Two: Processing flow Bufferedreader,bufferedwriter,bufferedinputstream
Bufferedoutputsstream, a layer of node flow is to be wrapped. That is, the processing flow is based on the node flow, with buffered flow, also known as the buffer stream, the buffer stream processing file input and output speed is the fastest. So the general buffer flow is used more.
Here are two simple examples of File replication:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
public class MycopyTest {
public static void main(String[] args) {
File src = new File( "D:/1.jpg" );
// D:/1.jpg必须的存在不然会报错
File dest = new File( "D:/2.jpg" );
// 如果D:/2.jpg存在则覆盖,如果不存在则新建
streamCopy(src, dest);
}
private static void readCopy(File src,File dest)
{
FileReader fr= null ;
FileWriter fw= null ;
BufferedReader br= null ;
BufferedWriter bw= null ;
try {
fr= new FileReader(src);
fw= new FileWriter(dest);
br= new BufferedReader(fr);
bw= new BufferedWriter(fw);
String str;
while ((str=br.readLine())!= null )
{
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void streamCopy(File src, File dest) {
FileInputStream fis = null ;
FileOutputStream fos = null ;
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte [] b = new byte [ 1024 ];
while ((len = bis.read(b)) != - 1 ) {
bos.write(b, 0 , len);
// bos.write(b,0,len);是把读到数组的大小字节写入
// bos.write(b);最后一次如果数组未写满的话就会多读。
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
bis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
Three: Conversion flow InputStreamReader and OutputStreamWriter
The function of the conversion stream, the text file in the hard disk in the form of byte stream storage, through the InputStreamReader read after the conversion to a character stream to the program processing, the program handles the character stream through OutputStreamWriter conversion to a byte stream save.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 |
public class InputStreamWriterTest {
public static void main(String[] args) {
File src = new File( "D:/Files/狗屁.txt" );
File dest = new File( "D:/Files/斯密斯.txt" );
BufferedWriter bw = null ;
BufferedReader br = null ;
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
InputStreamReader ir = new InputStreamReader(fis, "GBK" );
OutputStreamWriter ow = new OutputStreamWriter(fos, "GBK" );
bw = new BufferedWriter(ow);
br = new BufferedReader(ir);
String str;
while ((str = br.readLine()) != null ) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
Related articles:
Use of the output stream outputstring () of the Java IO stream
A comprehensive introduction to IO flow in Java
Related videos:
latest Java full video tutorial-free online video tutorial