第一节 文件对象的使用
File:文件对象,用于表示磁盘上的文件夹或数据文件。
1.1 获取文件列表
package com.test7;
import java.io.File;
public class Test4 {
//用递归的方式
public static void listFiles(String path){
File srcFile=new File(path);
if(!srcFile.exists() || !srcFile.isDirectory()){
return;
}
File[] fs = srcFile.listFiles();
for(int i=0;i<fs.length;i++){
File f= fs[i];
if(f.isFile()){
System.out.println(f.getPath()+" "+f.length());
}else{
System.out.println(f.getPath()+" <dir>");
listFiles(f.getPath());
}
}
}
public static void main(String[] args) {
listFiles("D:ftp_smx");
}
}
第二节 IO数据流的使用
数据流:以字节或字符为单位按特定的方向传送就构成了数据流。
2.1 流的分类
2.2 输入输出字节流的使用
字节流:
1.输入字节流
抽象类:InputStream 程序可以从中连续读取字节的对象称为字节输入流
实现类:FileInputStream
2.输出字节流
抽象类:OutputStream 程序可以向其中连续写入字节的对象称为字节输出流
实现类:FileOutputStream
流的使用步骤:
7
//1.使用File类找到一个文件,如果当前文件不存在则创建新文件
File f = new File("d:"+File.separator+"demo.txt");
//2.实例化IO流类
OutputStream os = new FileOutputStream(f,true);
String str="hello world";
//3。进行写操作
os.write(str.getBytes());
//4.关闭输出流
os.close();
//输入字节流
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
in.close();
2.3 缓冲字节流
缓冲流:缓冲流是一个处理流,在普通流的基础上添加了缓冲区,能够在传输数据的时候提升读取或写入效率。
package com.test3;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 Test4 {
public static void main(String[] args) throws IOException {
//例子:使用缓冲流复制文件
//创建字节输入流
InputStream is =new FileInputStream("d:/test.rar");
//创建字节输出流
OutputStream os = new FileOutputStream("e:/test.rar");
//字节缓冲输入流
BufferedInputStream bis = new BufferedInputStream(is);
//字节缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(os);
//定义字节数组存储读取的数据
byte[] bs = new byte[1024];
while(true){
//分批次读取数据
int count = bis.read(bs);
//如果读不到数据时,退出循环
if(count==-1){
break;
}
//写数据到另外一个文件
bos.write(bs, 0, count);
}
//清空缓冲流的缓冲区
bos.flush();
//关闭流对象
bos.close();
bis.close();
}
}
2.4 字符流
字符流:以字符为基本单位进行数据的传输
2.4.1 输入字符流
抽象类:Reader
实现类:FileReader
2.4.2 输出字符流
抽象类:Writer
实现类:FileWriter
File f = new File("d:/demo.doc");
Writer out = new FileWriter(f);
out.write("hello world");
out.close();
Reader reader = new FileReader(f);
char c[] = new char[1024];
int len = reader.read(c);//将内容读取到字符数组中
System.out.println(new String(c,0,len));
reader.close();
2.5 格式化打印字符流
PrintWriter: 是Writer的子类,其作用是将格式化对象打印到一个文本输出流, 主要方法:print()、println()、write()
PrintWriter pw = new PrintWriter("d:/test.txt");
pw.println("hello world");
pw.write("java");
pw.flush();
pw.close();
2.6 对象流(处理流)
对象流:可以将内存中的对象数据序列化到磁盘文件中
ObjectOutputStream:对象输出流
ObjectInputStream:对象输入流
package com.test5;
import java.io.Serializable;
//对象要实现序列化接口
public class Student implements Serializable{
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package com.test5;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class Test1 {
//保存对象到磁盘文件
public static void wObj() throws IOException{
//学生对象
Student s=new Student("黄蓉",18);
System.out.println(s);
//创建字节输出流对象
OutputStream os = new FileOutputStream("d:/student.obj");
//使用字节输出流构建对象输出流
ObjectOutputStream oos = new ObjectOutputStream(os);
//通过对象流写对象数据到磁盘
oos.writeObject(s);
//关闭流对象
oos.close();
}
public static void rObj() throws FileNotFoundException, IOException, ClassNotFoundException{
//创建对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/student.obj"));
//读取对象,转为学生
Student stu = (Student)ois.readObject();
//输出学生信息
System.out.println(stu);
//关闭流
ois.close();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
//wObj();
rObj();
}
}
作业:
查找以下流的概念描述,通过代码掌握其用法:
缓冲字符流:BufferedWriter,BufferedReader
字节字符转换流:InputStreamReader,OutputStreamWriter