- 1.字节流:
一切文件数据在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传输的始终为二进制数据。
流的关闭原则: 先开后关,后开先关。
package zjl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class test {
public static void main(String[] args) {
InputStream fis=null;//抽象类,不能实例化
OutputStream fos=null;//抽象类,不能实例化
try {
fis=new FileInputStream("d:\a.txt");//调用子类
fos=new FileOutputStream("d:\t.txt");
int bt=0;
while((bt=fis.read())!=-1) {
System.out.print((char)bt);//强制转化类型,要不然看不见内容
fos.write(bt);//输入到t.txt中,如果D盘没有a.txt和t.txt会报错
}
fis.close();//看完写完,要关闭
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package zijieliu;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) {
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\a.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("d:\q.txt"));
byte[] buffer = new byte[1024];
int len=0;
while((len=bis.read(buffer))!=-1) {
System.out.print(new String(buffer,0,len));
bos.write(buffer);
}
bos.flush();//刷新,一次写入数据//还可以防止出错
bis.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 2.字符流:
字符流以字符为单位获取数据的流,称为字符流,它们都继承于Reader/Writer抽象类。常用的字符流有:
FileReader/FileWriter,文件字符流(节点流)
BufferedReader/BufferedWriter,字符缓存流(处理流)
InputStreamReader/OutputStreamWriter,转换流(处理流)
package zfl;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) {
try {
FileReader fr=new FileReader("d:\a.txt");
FileWriter fw=new FileWriter("d:\t.txt");
char[] buff=new char[1024];
int len=0;
while((len=fr.read(buff))!=-1) {
System.out.print(new String(buff,0,len));//调用buff数值,转为字符串,从0位读到len
fw.write(buff);
}
fr.close();//关闭
fw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package zifuliu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) {
try {
BufferedReader br=new BufferedReader(new FileReader("d:\a.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\b.txt"));
char[] buff=new char[1024];
int len=0;
while((len=br.read(buff))!=-1) {
System.out.print(new String(buff,0,len));
bw.write(buff);
}
bw.flush();
br.close();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 3.字节流和字符流转换
package zjlzfl; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class test { public static void main(String[] args) { try { BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("d:\a.txt"))); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\t.txt"))); bw.write("hello!"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }



