一、输入流和输出流
1、输入流:抽象类InputStream,所有的输入流都继承该类
int read ( ) 从文件读取8 位字节,转为整数,并返回整数
int read ( byte [ ] b) 从文件中读取若干字节保存在b数组中,并返回整数表示读取的字节数
int read ( byte [ ] b, int off, int len) 读取若干字节,保存在b中,off表示保存数据的起始下标,len表示读取的字节数目
void close ( ) 关闭输入流
2、输出流:抽象类OutputStream,所有的输出流都继承该类
void write ( int b) 向输出流写入一个字节
void write ( byte [ ] b) 把参数b指定的字节数组中的数据写到输出流
void write ( byte [ ] b, int off, int len) 把参数b指定的字节数组中的数据按下标off和长度len写入输出流
void flush ( ) 刷新输出流,强制写出缓冲流中的所有字节
void close ( ) 关闭输出流
二、字节输入流和字节输出流
1、字节流读取文件:FileInputStream
public static void main ( String [ ] args) throws Exception {
FileInputStream in= new FileInputStream ( "example.txt" ) ;
int b;
while ( true ) {
b= in. read ( ) ;
if ( b== - 1 )
break ;
System . out. println ( b) ;
}
in. close ( ) ;
}
2、字节流输出文件:FileOutputStreasm
public static void main ( String [ ] args) throws Exception {
FileOutputStream out= new FileOutputStream ( "example.txt" ) ;
String str= "我是呆呆" ;
byte [ ] b= str. getBytes ( ) ;
for ( int i= 0 ; i< b. length; i++ ) {
out. write ( b[ i] ) ;
}
out. write ( b) ;
out. close ( ) ;
}
public static void main ( String [ ] args) throws Exception {
FileOutputStream out = new FileOutputStream ( "example.txt" , true ) ;
String str= "我是呆呆" ;
byte [ ] b= str. getBytes ( ) ;
out. write ( b) ;
out. close ( ) ;
}
3、文件的拷贝:FileInputStream+FileOutputStream
public static void main ( String [ ] args) throws Exception {
FileInputStream in = new FileInputStream ( "Test1\\example.txt" ) ;
FileOutputStream out = new FileOutputStream ( "Test2\\example.txt" ) ;
int b;
while ( true ) {
b = in. read ( ) ;
if ( b == - 1 )
break ;
out. write ( b) ;
}
System . out. println ( "拷贝成功!" ) ;
in. close ( ) ;
out. close ( ) ;
}
4、创建字节流的缓冲区:byte[] arr=new byte[1024];
public static void main ( String [ ] args) throws Exception {
FileInputStream in= new FileInputStream ( "Test1\\example.txt" ) ;
FileOutputStream out= new FileOutputStream ( "Test2\\example.txt" ) ;
int b;
byte [ ] arr= new byte [ 1024 ] ;
while ( ( b= in. read ( arr) ) != - 1 ) {
out. write ( arr, 0 , b) ;
}
System . out. println ( "拷贝成功!" ) ;
in. close ( ) ;
out. close ( ) ;
}
5、字节缓冲流:BufferedInputStream+BufferedOutputStream
public static void main ( String [ ] args) throws Exception {
BufferedInputStream bis= new BufferedInputStream ( new FileInputStream ( "Test1\\example.txt" ) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( "Test2\\des.txt" ) ) ;
int len;
while ( ( len= bis. read ( ) ) != - 1 ) {
bos. write ( len) ;
}
bis. close ( ) ;
bos. close ( ) ;
System . out. println ( "拷贝成功!" ) ;
}
三、字符输入流和字符输出流
1、字符输入流:FileReader
public static void main ( String [ ] args) throws Exception {
FileReader in = new FileReader ( "example.txt" ) ;
int b;
while ( ( b = in. read ( ) ) != - 1 ) {
System . out. print ( ( char ) b) ;
}
in. close ( ) ;
}
2、字符输出流:FileWriter
public static void main ( String [ ] args) throws Exception {
FileWriter out= new FileWriter ( "example.txt" , true ) ;
String str= "\n想,我该怎么办?\n" ;
out. write ( str) ;
out. close ( ) ;
System . out. println ( "写入成功!" ) ;
}
3、按行拷贝数据:BufferedReader+BufferedWriter
public static void main ( String [ ] args) throws Exception {
FileReader in = new FileReader ( "example.txt" ) ;
BufferedReader br = new BufferedReader ( in) ;
FileWriter out = new FileWriter ( "example01.txt" , true ) ;
BufferedWriter bw = new BufferedWriter ( out) ;
String str;
while ( ( str = br. readLine ( ) ) != null ) {
bw. write ( str) ;
bw. newLine ( ) ;
}
br. close ( ) ;
bw. close ( ) ;
}
4、写入行号
public static void main ( String [ ] args) throws Exception {
FileReader in = new FileReader ( "example.txt" ) ;
FileWriter out = new FileWriter ( "example.txt" , true ) ;
LineNumberReader lr = new LineNumberReader ( in) ;
lr. setLineNumber ( 0 ) ;
String line = null ;
while ( ( line = lr. readLine ( ) ) != null ) {
out. write ( lr. getLineNumber ( ) + " " + line) ;
out. write ( "\r\n" ) ;
}
lr. close ( ) ;
out. close ( ) ;
}
四、转换流:InputStreamReader+OutputStreamWriter
public static void main ( String [ ] args) throws Exception {
FileInputStream in = new FileInputStream ( "example.txt" ) ;
InputStreamReader intemp1 = new InputStreamReader ( in) ;
BufferedReader intemp2 = new BufferedReader ( intemp1) ;
FileOutputStream out = new FileOutputStream ( "example.txt" , true ) ;
OutputStreamWriter outtemp1 = new OutputStreamWriter ( out) ;
BufferedWriter outtemp2 = new BufferedWriter ( outtemp1) ;
String str;
while ( ( str = intemp2. readLine ( ) ) != null ) {
outtemp2. write ( str) ;
outtemp2. newLine ( ) ;
}
intemp2. close ( ) ;
outtemp2. close ( ) ;
}
五、对象流:ObjectInputStream+ObjectOutputStream
public static void main ( String [ ] args) throws Exception {
Person personout = new Person ( ) ;
System . out. println ( "-------文件写入之前------" ) ;
System . out. println ( "姓名:" + personout. getName ( ) + " 年龄:" + personout. getAge ( ) + " 手机号:" + personout. getPhone ( ) ) ;
FileOutputStream out = new FileOutputStream ( "Person.txt" ) ;
ObjectOutputStream objout = new ObjectOutputStream ( out) ;
objout. writeObject ( personout) ;
System . out. println ( "已将对象按二进制的格式进行序列化输出!" ) ;
System . out. println ( "************分割线***********" ) ;
FileInputStream in = new FileInputStream ( "Person.txt" ) ;
ObjectInputStream objin = new ObjectInputStream ( in) ;
Person personin = ( Person ) objin. readObject ( ) ;
System . out. println ( "该对象保存的地址为:" + personin) ;
System . out. println ( "姓名:" + personin. getName ( ) + " 年龄:" + personin. getAge ( ) + " 手机号:" + personin. getPhone ( ) ) ;
}
class Person implements Serializable {
private String name = "呆呆" ;
private int age = 20 ;
private long phone = 456123789 ;
public Person ( ) {
}
public Person ( String name, int age, long phone) {
this . name = name;
this . age = age;
this . phone = phone;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public long getPhone ( ) {
return phone;
}
public void setPhone ( long phone) {
this . phone = phone;
}
}
六、成员数据流:DataInputStream+DataOutputStream
public static void main ( String [ ] args) throws Exception {
FileOutputStream out = new FileOutputStream ( "Data.txt" ) ;
BufferedOutputStream outtemp = new BufferedOutputStream ( out) ;
DataOutputStream outdata = new DataOutputStream ( outtemp) ;
outdata. writeInt ( 123 ) ;
outdata. writeInt ( 13456 ) ;
outdata. writeByte ( 123 ) ;
outdata. writeChar ( '1' ) ;
outdata. writeBoolean ( true ) ;
outdata. writeUTF ( "呆呆" ) ;
outdata. close ( ) ;
System . out. println ( "数据写入成功!" ) ;
FileInputStream in = new FileInputStream ( "Data.txt" ) ;
BufferedInputStream intemp = new BufferedInputStream ( in) ;
DataInputStream indata = new DataInputStream ( intemp) ;
System . out. println ( "数据读取成功!" ) ;
System . out. println ( indata. readInt ( ) ) ;
System . out. println ( indata. readInt ( ) ) ;
System . out. println ( indata. readByte ( ) ) ;
System . out. println ( indata. readChar ( ) ) ;
System . out. println ( indata. readBoolean ( ) ) ;
System . out. println ( indata. readUTF ( ) ) ;
indata. close ( ) ;
}
七、打印流:PrintStream
import java. io. * ;
public class Demo01 {
public static void main ( String [ ] args) throws Exception {
PrintStream ps = new PrintStream ( new FileOutputStream ( "example01.txt" ) , true ) ;
int num = 19 ;
Student stu = new Student ( ) ;
ps. println ( "这是一个数字:" + num) ;
ps. println ( stu) ;
ps. close ( ) ;
System . out. println ( "打印输出完成!" ) ;
}
}
class Student {
private String name = "呆呆" ;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
@Override
public String toString ( ) {
return getName ( ) ;
}
}
八、标准输入输出流:System.in、System.out、System.err
九、管道流:PipedInputStream+PipedOutputStream
十、临时存储流:
字节型临时存储:ByteArrayInputStream+ByteArrayOutputStream 字符型临时存储:CharArrayInputStream+CharArrayOutputStream
十一、数据合并流:SequenceInputStream
十二、File类
十三、按指定位置读取文件:RandomAccessFile
十四、字符编码