一、流的分类
按读取数据的类型:
字节流和字符流两种
区别:
字节流可以读取任何形式的数据:包括文本、音频、图像、视频
字符流只能读取纯文本形式的数据
字节流有哪些:
以Stream结尾的都是字节流
例如:InputStream,OutputStream
FileInputStream/FileOutputStream
BufferedInputStream/BufferedOutputStream
字符流有哪些:
以Reader或者是Writer结尾的都是字符流
例如:Reader/Writer
FileIuptStreamReader/FileOutputStreamWriter
二、File类
它是文件和目录路径名的抽象表示
- 文件和目录是可以通过File封装成对象的
- 对于file而言,其封装的并不是一个真正存在的文件,仅仅是一个路径名而已。它可以是存在的,也可以是不存在的。
2.1 File的构造方法+创建+删除功能
方法名 | 说明 |
---|---|
File(String pathname) | 通过将给定的路径名字符串转换为抽象路径名来创建新的file实例 |
public boolean createNewFile() | 当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件 |
public boolean mkdir() | 创建由此抽象路径名命名的目录 |
public boolean mkdirs() | 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录 |
public boolean delete() | 删除由此抽象路径名表示的文件或者目录 |
public String getParent() | 返回文件目录的上一级目录名 |
boolean renameTo(File newName) | 重命名此File对象对应的文件或目录,若重命名成功返回true |
2.2 File类的判断和获取功能
方法名 | 说明 |
---|---|
public boolean isDirectory() | 测试此抽象路径名表示的file是否为目录 |
public boolean isFile() | 测试此抽象路径名表示的file是否为文件 |
public boolean canWrite() | 判断对象对应文件或目录是否可写 |
public boolean canRead() | 判断对象对应文件或目录是否可读 |
public boolean isAbsolute() | 判断对象对应文件或目录是否为绝对路径名 |
public boolean exists() | 测试此抽象路径名表示的file是否存在 |
public String getAbsolutePath() | 返回此抽象路径名的绝对路径名字符串 |
public File getAbsoluteFile() | 返回绝对路径 |
public String getPath() | 将此抽象路径名转换为路径名字符串 |
public String getName() | 返回由此抽象路径名表示的文件或者目录的名称 |
public String[] list() | 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组 |
public File [ ] listFile() | 返回此抽象路径名表示的目录中的文件和目录的file对象数组 |
public class Test {
public static void main(String[] args) {
File file = new File("e:\\java");
getAllFile(file);
}
public static void getAllFile(File dir){
System.out.println(dir);//输出被遍历的目录名
File[] files = dir.listFiles();
for(File f : files){
// 测试此抽象路径名表示的file是否为目录
if(f.isDirectory()){
getAllFile(f);//递归
}else{
System.out.println("-----------");
String name = f.getName();//返回由此抽象路径名表示的文件或者目录的名称
String pt = f.getPath();//将此抽象路径名转换为路径名字符串
//使用.endsWith判断是否是以“.java”结尾的文件,是就输出
boolean b = f.toString().endsWith(".java");
if(b)
{ System.out.println(f);}
System.out.println("-----------");
}
}
}
}
三、InputStream 和 OutputStream
OutputStream
3.1 FileOutputStream 写入数据到文件,下面创造出来一个TXT文件,里面的方法查API文档使用
- 创建一个FileOutputStream对象,构造方法中传入数据
- 调用write方法,将数据写入文件中,查API文档
- 释放资源
public class Test {
public static void main(String[] args) throws IOException {
//1.创建一个FileOutputStream对象,构造方法中传入数据
FileOutputStream fos = new FileOutputStream("E:\\资料\\niu.txt");
//2.调用write方法,将数据写入文件中,查API文档
fos.write(9);
//3.释放资源
fos.close();
}
}
3.2 FileOutputStream 写入数据到文件,文件数据进行续写和换行
- FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.- FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
public class Test {
public static void main(String[] args) throws IOException {
//1.创建一个FileOutputStream对象,构造方法中传入数据
FileOutputStream fos = new FileOutputStream("E:\\资料\\niu.txt");
//2.调用write方法,将数据写入文件中,查API文档
for (int i = 0; i <=5 ; i++) {
// 续写
fos.write("hello".getBytes());
// 换行
fos.write("\r".getBytes());
}
//3.释放资源
fos.close();
}
}
InputStream
3.3 FileInputStream 写入数据到文件,读取文件数据
- 创建一个FileInputStream 对象
- 调用read方法,将数据写入文件中,查API文档
- 释放资源
public class Test {
public static void main(String[] args) throws IOException {
//1.创建一个FileInputStream对象,构造方法中传入数据
FileInputStream fis = new FileInputStream("E:\\XXX\\niu.txt");
//2.调用read方法,查API文档
System.out.println((char)fis.read());//a
System.out.println(fis.read());//-1
//3.释放资源
fis.close();
}
}
-----------------------------------------------------------------------
public class Test {
public static void main(String[] args) throws IOException {
//1.创建一个FileInputStream对象,构造方法中传入数据
FileInputStream fis = new FileInputStream("E:\\XXX\\niu.txt");
//2.调用read方法,查API文档
byte[] bytes = new byte[1010];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
System.out.println(new String(bytes));
}//ab
//3.释放资源
fis.close();
}
}
文件拷贝
public class Test {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("C:\\xxx\\niu.txt");
FileInputStream fis = new FileInputStream("E:\\xxx\niu.txt");
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
}
文件夹拷贝(摘)
public class CpyDir {
public static void main(String[] args) throws IOException {
File srcFile = new File("F:/XXX");
File destFile = new File("E:/");
cpyDir(srcFile, destFile);
}
public static void cpyDir(File srcFile, File destFile) throws IOException {
checkNull(srcFile);//检查文件是否存在
if (srcFile.isFile()) {
// 进行文件拷贝
copyFile(srcFile, destFile);
return;
}
File[] files = srcFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
createNewDir(destFile, file);
}
cpyDir(file,destFile);
}
}
//创造文件目录
private static void createNewDir(File destFile, File file) {
String srcPath = file.getAbsolutePath();
String destPath = destFile.getAbsolutePath();
String newPath = (destPath + srcPath.substring(3));
File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
}
private static void copyFile(File srcFile, File destFile) throws IOException {
InputStream fis = null;
OutputStream fos = null;
String srcPath = srcFile.getAbsolutePath();
String destPath = destFile.getAbsolutePath();
String newPath = (destPath + srcPath.substring(3));
destFile = new File(newPath);
//局部变量作用域问题
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] flush = new byte[1024*1024];
int len = 0;
while ((len = fis.read(flush)) != -1) {
fos.write(flush,0,len);
}
} finally {
//没有会报空指针异常
if(fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
}
}
private static void checkNull(File srcFile) throws FileNotFoundException {
if (srcFile == null) {
throw new FileNotFoundException("抱歉,文件没有找到!");
}
}
}
四、包装流(缓冲流)
- BufferedReader(Reader in)
- BufferedWriter(Writer out)
- BufferedInputStream(InputStream in) 已经有缓存的字节输入流
- BufferedOutputStream(OutputStream out)
public class Test {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("E:\\资料\\niun.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("数据写入内部缓冲区".getBytes());//文件里面显示:数据写入内部缓冲区
bos.flush();//刷新
}
}
public class Test {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\xxx\\niu.txt"));
for (int i = 0; i < 5; i++) {
bw.write("niu");
}
bw.flush();//刷新
bw.close();//释放
}
}
序列化与反序列化
NotSerializableException 没有序列化异常
Serializable接口来实现
注意:
- 要有对象
- 类一定要实现Serializable接口
- ObjectOutputStream:序列化 ObjectInputStream:反序列化
class Person implements Serializable{
private String name;
private int age;
public Person() {
}
public Person(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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
----------------------------------------------------------------
序列化 —— ObjectOutputStream
public class Test {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\niu.txt"));
oos.writeObject(new Person("小牛",18));
oos.close();
}
}
----------------------------------------------------------------
反序列化 —— ObjectInputStream
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois =new ObjectInputStream(new FileInputStream("E:\\niu.txt"));
Object o =ois.readObject();
ois.close();
System.out.println(o);
Person p = (Person) o;
System.out.println(p.getName() + " " + p.getAge());
}
}