1.流的分类
a.从方向划分:输入流和输出流
b.从读取和写入的方式:字符流和字节流
Reader ,Writer.
2.字节流
FileInputStream 和 FileOutputStream
代码如下:
3.缓冲流
BufferedInputStream 和 BufferedOutputStream
代码如下:
4.对象流
ObjectInputStream 和 ObjectOutputStream
Student stu = new Student();
oos.wirteObject(stu);
class Student implements 序列化接口 {
name;
transient score;//表示将对象写入文件的时候,瞬时的属性是不会写进去的。
}
文件读取是以一个byte读取,而汉字是两个byte,所以在读取汉字的时候要注意。。
a.从方向划分:输入流和输出流
b.从读取和写入的方式:字符流和字节流
Reader ,Writer.
2.字节流
FileInputStream 和 FileOutputStream
代码如下:
public void fileTest(String path,String newPath){
try {
//实例化一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(newPath);
//获取文件字节的总数
int length = fis.available();
//根据总数定义一个byte数组
byte [] array = new byte[length];
fis.read(array);
fos.write(array);
3.缓冲流
BufferedInputStream 和 BufferedOutputStream
代码如下:
public void bufferedTest(String path,String newPath){
try {
//实例化一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(newPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//获取文件字节的总数
int length = bis.available();
//根据总数定义一个byte数组
byte [] array = new byte[length];
bis.read(array);
bos.write(array);
bos.flush();
4.对象流
ObjectInputStream 和 ObjectOutputStream
Student stu = new Student();
oos.wirteObject(stu);
class Student implements 序列化接口 {
name;
transient score;//表示将对象写入文件的时候,瞬时的属性是不会写进去的。
}
文件读取是以一个byte读取,而汉字是两个byte,所以在读取汉字的时候要注意。。