流的概念

程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。
字节流与字符流

内容操作就四个类:OutputStream、InputStream、Writer、Reader

字节流

- 字节输出流OutputStream

Clonseable表示可以关闭的操作,因为程序运行到最后肯定要关闭。
Fluashable表示刷新,清空内存中的数据。

import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo01{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
OutputStream out = null ;
out = new FileOutputStream(f) ;
String str = "Hello World!!!" ;
byte b[] = str.getBytes() ;
out.write(b) ;
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在操作的时候,如果文件本身不存在,则会为用户自动创建新文件。
在操作输出流的时候,也可以使用write(int i)的方法写出数据。
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo02{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
OutputStream out = null ;
out = new FileOutputStream(f) ;
String str = "Hello World!!!" ;
byte b[] = str.getBytes() ;
for(int i=0;i<b.length;i++){
out.write(b[i]) ;
}
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
以上的操作中在写入数据之后,文件之前的内容应经不存在了,因为在io操作中默认的情况是将其进行覆盖的,那么如果现在要想执行追加的功能,则必须设置追加的操作。

import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo03{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
OutputStream out = null ;
out = new FileOutputStream(f,true) ;
String str = "Hello World!!!" ;
byte b[] = str.getBytes() ;
for(int i=0;i<b.length;i++){
out.write(b[i]) ;
}
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
程序本身是可以追加内容了,但是没有换行,是直接追加到末尾的。
如果在文件中想换行的话,使用“\r\n”完成。
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo04{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
OutputStream out = null ;
out = new FileOutputStream(f,true) ;
String str = "\r\nHello World!!!" ;
byte b[] = str.getBytes() ;
for(int i=0;i<b.length;i++){
out.write(b[i]) ;
}
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20

import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo01{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
byte b[] = new byte[1024] ;
input.read(b) ;
input.close() ;
System.out.println("内容为:" + new String(b)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
功能虽然实现了,但是存在问题? 数据的长度、数组的空间。
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo02{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
byte b[] = new byte[1024] ;
int len = input.read(b) ;
input.close() ;
System.out.println("读入数据的长度:" + len) ;
System.out.println("内容为:" + new String(b,0,len)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19

import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo03{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
byte b[] = new byte[(int)f.length()] ;
int len = input.read(b) ;
input.close() ;
System.out.println("读入数据的长度:" + len) ;
System.out.println("内容为:" + new String(b)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
以上是直接使用byte数组的方式完成的。
现在使用public abstract int read() throws IOException读取内容。
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo04{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
byte b[] = new byte[(int)f.length()] ;
for(int i=0;i<b.length;i++){
b[i] = (byte)input.read() ;
}
input.close() ;
System.out.println("内容为:" + new String(b)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
以上的操作只适合于知道输入流的大小,如果现在不知道输入流大小。
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo05{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
byte b[] = new byte[1024] ;
int len = 0 ;
int temp = 0 ;
while((temp=input.read())!=-1){
b[len] = (byte)temp ;
len++ ;
}
input.close() ;
System.out.println("内容为:" + new String(b,0,len)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
当不知道读取内容多大的时候,可以使用读取内容返回值为-1 为读完的标志。
字符流

-
字符输出流Writer

字符流的操作比字节流操作好在一点,就是可以直接输出字符串了。不用再进行转换操作了。
import java.io.File ;
import java.io.Writer ;
import java.io.FileWriter ;
public class WriterDemo01{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Writer out = null ;
out = new FileWriter(f) ;
String str = "Hello World!!!" ;
out.write(str) ;
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
使用字符流默认情况下依然是覆盖已有的文件,如果想要追加的话,则直接在FileWriter上增加一个可追加的标记即可。
import java.io.Writer ;
import java.io.FileWriter ;
public class WriterDemo02{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Writer out = null ;
out = new FileWriter(f,true) ;
String str = "\r\nLIXINGHUA\r\nHello World!!!" ;
out.write(str) ;
out.close() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17

以字符数组的形式读取数据:
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo01{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Reader input = null ;
input = new FileReader(f) ;
char c[] = new char[1024] ;
int len = input.read(c) ;
input.close() ;
System.out.println("内容为:" + new String(c,0,len)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
采用循环的方式,通过文件是否读到低的形式读取:
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo02{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Reader input = null ;
input = new FileReader(f) ;
char c[] = new char[1024] ;
int temp = 0 ;
int len = 0 ;
while((temp=input.read())!=-1){
c[len] = (char)temp ;
len++ ;
}
input.close() ;
System.out.println("内容为:" + new String(c,0,len)) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
字节流与字符流的区别

验证字符流使用了缓存:
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo05{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
OutputStream out = null ;
out = new FileOutputStream(f) ;
String str = "Hello World!!!" ;
byte b[] = str.getBytes() ;
out.write(b) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在使用字节流操作中,及时没有关闭,最终也是可以输出的。
import java.io.File ;
import java.io.Writer ;
import java.io.FileWriter ;
public class WriterDemo03{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Writer out = null ;
out = new FileWriter(f) ;
String str = "Hello World!!!" ;
out.write(str) ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17

import java.io.File ;
import java.io.Writer ;
import java.io.FileWriter ;
public class WriterDemo04{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Writer out = null ;
out = new FileWriter(f) ;
String str = "Hello World!!!" ;
out.write(str) ;
out.flush() ;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18

操作范例


import java.io.* ;
public class Copy{
public static void main(String args[]){
if(args.length!=2){
System.out.println("输入的参数不正确。") ;
System.out.println("例:java Copy 源文件路径 目标文件路径") ;
System.exit(1) ;
}
File f1 = new File(args[0]) ;
File f2 = new File(args[1]) ;
if(!f1.exists()){
System.out.println("源文件不存在!") ;
System.exit(1) ;
}
InputStream input = null ;
OutputStream out = null ;
try{
input = new FileInputStream(f1) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
try{
out = new FileOutputStream(f2) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
if(input!=null && out!=null){
int temp = 0 ;
try{
while((temp=input.read())!=-1){
out.write(temp) ;
}
System.out.println("拷贝完成!") ;
}catch(IOException e){
e.printStackTrace() ;
System.out.println("拷贝失败!") ;
}
try{
input.close() ;
out.close() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 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
原文地址
原文地址:http://lib.youkuaiyun.com/article/javase/2895?knId=215
原文作者:waldmer