JavaSE:文件和流

224 阅读3分钟

001.文件(File类)

0011.什么是文件?

文件是相关记录或放在一起的数据的集合

0012.引入的包

java.io

0013.常用方法

exists()             是否存在
createNewFile()      新建文件
mkdir()              创建目录
mkdirs()             创建多级目录
getName()            获取文件名
getAbsolutePath()    是从盘符开始的路径
getPath()            是从当前路径开始的路径 
isDirectory()        是否是目录
isFile()             是否是文件

0014.代码

public class TestFile {
    public static void main(String[] args) {
    //创建文件 (或D:\\aaa.txt)
    File file = new File("D:/aaa.txt");
    if(!file.exists()){
    	try {
    		file.createNewFile();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    
    //创建目录
    File mk = new File("D:/images");
    if(!mk.exists()){
    	mk.mkdir();
    }
    
    //创建多级目录
    File mk1 = new File("D:/images/icon");
    if(!mk1.exists()){
    	mk1.mkdirs();
    }
    
    //在images目录 下创建txt文本
    File file1 = new  File(mk,"aa.txt");
    if(!file1.exists()){
    	try {
    		file1.createNewFile();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    
    //File常用方法
    //获取绝对路径
    System.out.println(file1.getAbsolutePath());
    //获取文件名
    System.out.println(file.getName());
    //是否是目录
    System.out.println("是否是目录:"+mk.isDirectory()); //true
    //是否是文件
    System.out.println("是否是文件"+file.isFile());//true
    //删除文件
    file.delete(); 
    //有文件的目录不可删除
    mk.delete();
    }	
}

002.流

  • 按流的方向不同
    • 输入流
    • 输出流
  • 按处理数据的单位不同
    • 字节流
    • 字符流
  • 按功能不同
    • 节点流
    • 处理流

0021.InputStream 字节输入流

  • 主要介绍

    • FileInputStream 文件输入流
    • ObjectInputStream 对象输入流
    • BufferedInputStream 缓冲输入流
  • FileInputStream的一些方法

    * read() 从输入流中读取一个数据字节
    * read(byte[] b) 从输入流中将最多b.length个字节的数据读入一个byte数组中
    * read(byte[] b,int off,int len) 从输入流中将最多len个字节的数据读入一个byte数组中
    * close()  关闭并释放资源

0022.OutputStream 字节输出流

  • 主要介绍:

    • FileOutputStream 文件输出流
    • ObjectOutputStream 对象输出流
    • BufferedOutputStream 缓冲输出流
  • FileOutputStream的一些方法

    * write(int b) 将指定字节写入此文件输出流
    * write(byte[] b) 将b.length个字节从指定byte数组写入此文件输出流中
    * write(byte[] b,int off,int len) 将指定byte数组中从偏移量off开始的len个字节写入此文件输出流
    * close()  关闭并释放资源

0023.代码

//FileOutputStream 与 FileOutputStream
//拷贝图片 
public static void main(String[] args) {
    File f1 = new File("D:/aaa.jpg");
    File f2 = new File("F:/aaa.jpg");
    		
    FileInputStream fis = null;
    FileOutputStream fos = null;
    
    try {
    	fis = new FileInputStream(f1);
    	fos = new FileOutputStream(f2);
    	int n=0;
    	byte[] b = new byte[1024];
    	//返回值为-1表示读到了文件末尾
    	while((n=fis.read(b))!=-1){
    		fos.write(b,0,n);
    	}
    	fis.close();
    	fos.close();
    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    }
}
//BufferedInputStream和BufferedOutputStream
public static void main(String[] args) {
    File f1 = new File("D:/c.txt");
    File f2 = new File("D:/d.txt");
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    
    try {
    	bis = new BufferedInputStream(new FileInputStream(f1));
    	bos = new BufferedOutputStream(new FileOutputStream(f2,true));//加了true,追加
    	int n= 0;
    	while((n=bis.read())!=-1){
    		bos.write(n);
    		bos.flush(); //刷新
    	}
    	bos.close();
    	bis.close();
    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    }
}

0024.字符输入流Reader

  • 主要介绍
    • BufferedReader
    • InputStreamReader
    • FileReader

0025.字符输出流Writer

  • 主要介绍
    • BufferedWriter
    • OutputStreamWriter
    • FileWriter

0026.字节字符转换流

  • 主要介绍:
    • InputStreamReader
    • OutputStreamWriter
public static void main(String[] args) {
    readAAA();      //将aaa.txt内容读出到控制台
    copyContent();  //将aaa.txt内容复制到bbb.txt
}

private static void readAAA() {
    try {
    	FileInputStream fis = new FileInputStream("aaa.txt");
    	InputStreamReader isr = new InputStreamReader(fis);
    	
    	int n = 0;
    	char[] c = new char[1024];
    	while((n=isr.read(c))!=-1){
    		String s = new String(c,0,n);
    		System.out.println(s);
    	}
    	
    	isr.close();
    	fis.close();
        } catch (FileNotFoundException e) {
        	e.printStackTrace();
        } catch (IOException e) {
        	e.printStackTrace();
        }
    }
    
private static void copyContent() {
    try {
    	FileInputStream fis = new FileInputStream("aaa.txt");
    	InputStreamReader isr = new InputStreamReader(fis);
    	FileOutputStream fos = new FileOutputStream("bbb.txt",true);
    	OutputStreamWriter osw = new OutputStreamWriter(fos);
    	
    	int n = 0;
    	char[] c = new char[1024];
    
    	while((n=isr.read(c))!=-1){
    		osw.write(c,0,n);
    		osw.flush();
    	}
    	fis.close();
    	fos.close();
    	isr.close();
    	osw.close();
    	
    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    }
}

003.对象序列化与反序列化

  • 什么是序列化和反序列化?

    • 序列化:把Java对象转换为字节序列的过程;
    • 把字节序列恢复为Java对象的过程;
  • 主要介绍

    • ObjectInputStream 对象输入流
    • ObjectOutputStream 对象输出流

0031.对象序列化步骤

  • 创建一个类,继承Serializable接口
  • 创建对象
  • 将对象写入文件
  • 从文件读取对象信息

0032.代码

//1.创建一个类,继承Serializable接口
public class Student implements Serializable{
    ..成员属性、构造方法、set/get方法、toString..
}

//2.3.4测试代码
public static void main(String[] args) {
    File file = new File("D:/student.txt");
    Student stu = new Student("李斯",1,18,new Date(),"北京");
    
    ObjectOutputStream os = null;
    ObjectInputStream is = null;	
    
    try {
    	os = new ObjectOutputStream(new FileOutputStream(file));
    	is = new ObjectInputStream(new FileInputStream(file));
    	//将stu对象信息写入文件
    	os.writeObject(stu);
    	//读对象信息
    	Student o = (Student)is.readObject();
    	System.out.println(o);
    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    } catch (ClassNotFoundException e) {
    	e.printStackTrace();
    }finally{
    	try {
    		is.close();
    		os.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
}