IO流基础

IO流基础

File类

File 文件或者目录的抽象表现形式

构造器

File(String pathname) ;()内为文件路径名,可能存在,也可能不存在

File file1 = new File("D://");
File file2 = new File("D:/AAA/BBB");
File file3 = new File("D:\\test.txt");

注意:单个\为转义字符

File(String parent, String child); 从父路径名字符串和子路径名字符串创建新的 File实例

File file4 = new File("D:\\","test.txt");

File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例

File file5 = new File(file,"test.txt");

常用方法

File file = new File(“某文件路径”);

File file1 = new File(“gaga/haha.txt”);

方法名返回值类型描述调用
setReadOnly()boolean标记此抽象路径名指定的文件或目录,以便仅允许读取操作file.setReadOnly()
canWrite()boolean测试应用程序是否可以修改此抽象路径表示的文件file.canWrite()
createNewFile()boolean当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。 注意: 不能创建文件夹,只能创建文件file.createNewFile()
delete()boolean删除此抽象路径名表示的文件或目录file.delete()
exists()boolean测试此抽象路径名表示的文件或目录是否存在file.exists()
getAbsolutePath()String返回此抽象路径名的绝对路径名字符串;相对路径 : 默认相对于项目file1.getAbsolutePath()
getAbsoluteFile()File返回此抽象路径名的绝对形式file1.getAbsoluteFile()
lastModified()long(毫秒数)返回上次修改此抽象路径名表示的文件的时间file1.lastModified()
listFiles()File[]返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件file.listFiles()

IO流

File 类 只能操作文件外部的内容,无法操作文件内部内容的读写

流:
数据以先入先出的顺序进行传输,流就是管道,就是用来 传输数据的

IO 流
java提供了一个io包,提供了很多类,提供了很多功能能够实现数据的读写

需要的条件

数据源:数据从数据源写出

目的地:从目的地中把数据读出来

关系:数据源 --> 数据 --> 目的地

流的分类:

  1. 流向分(以程序为中心):输入流;输出流
  2. 操作单元分:字符流(以字符为单位);字节流(万能流-》计算机中数据以字节为单位)
  3. 功能划分:节点流(能够实现读写能力);功能流(增强节点流功能与性能的流)

节点流、字节流

读入流

字节输入流 InputStream -> 输入字节流的父类->抽象类->不能实例化
文件字节输入流 FileInputStream -> 具体的类 ->可以实例化
数组流

创建
public static void main(String[] args) throws Exception {
    //1.创建输入流
    //FileInputStream(String name)  参数: name 数据源文件的路径
    //FileInputStream(File file) 参数: file 数据源文件的路径的file对象
    InputStream src = new FileInputStream("D://test.txt");

    //2.读入数据
    //int read() 从此输入流中读取一个字节的数据。  已经到达文件末尾没有读到数据 返回-1
   /*
   	1)单个数据读入
   	int num = src.read();
   	System.out.println((char)num);
   	System.out.println((char)(src.read()));
   	System.out.println((char)(src.read()));
   	System.out.println(src.read());
   */

   /*
   	2)优化 : 重复读入--> 循环读取
   	int result = -1;  //存储每次读入的数据
   	while((result = src.read())!=-1){
   	    System.out.println((char)result);
   	}
   */
    //3)字节数组读入
    //准备字节数组
    byte[] car = new byte[1024];
    int len = -1; //记录每次读取到数组中的数据个数

    //重复读入
    while((len = is.read(car))!=-1){
        //获取的数据是读入的数据个数
        System.out.println(new String(car,0,len));
    }

    //3.关闭
    src.close();
}

异常捕获:

​ 注意提升变量的作用域,其他按照提示就行

写出流

字节流 节点流 输出流
字节输出流 OutputStream 抽象父类 -> 不能实例
文件字节输出流 FileOutputStream 具体子类 -> 实例

创建
public static void main(String[] args) throws IOException {

    //1.构建输出流
    //FileOutputStream  (String name) 参数name为目的地路径字符串
    //FileOutputStream (File file)  参数file为目的地路径file对象
    //FileOutputStream (File file, boolean append)  参数append是否追加数据,默认不追加覆盖,true->追加
    //FileOutputStream(String name, boolean append)
    OutputStream os = new FileOutputStream("D://test.txt",true);
    //2.写出数据
    //void write(int b) 将指定的字节写入此文件输出流。
    os.write(98);
    os.write(99);

    //3.刷出
    os.flush();
    //4.关闭
    os.close();
}

大体写入流差不多,不过在构造器参数上有些差异;为保证输出的数据完整,需要在关闭前使用flush()方法刷出残留数据

注意:
写出内容默认覆盖源文件内容
目标文件不存在系统会默认构建
目标文件所在的路径不存在,系统不会构建

文件拷贝

大致顺序:源文件–>字节输入流–>程序–>字节输出流–>目的地新文件

程序在此过程中担任中转站的角色

需要读入写出的配合使用

public static void main(String[] args) {
    //1.构建流  字节输入   字节输出
    InputStream is = null;
    OutputStream os = null;
    try {
      is = new FileInputStream("D:/hehe.txt");
      os = new FileOutputStream("D:/houhou.txt");
      //2.读入写出
      byte[] car = new byte[6];
	  int len = -1; //每次读到字节数组中数据的个数
      while((len = is.read(car))!=-1){
         os.write(car,0,len);
      }
      //3.刷出
      os.flush();
	} catch (FileNotFoundException e) {
  		e.printStackTrace();
	} catch (IOException e) {
  		e.printStackTrace();
	} finally {
        //4.关闭 后打开的先关闭
        if(os!=null){
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(is!=null){
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
}

节点流 字符流

字符输入流 Reader
文件字符输入流 FileReader

字符输出流 Writer
文件字符输出流 FileWriter

注意:
子类无新增方法需要使用,可以多态指向
读入数据的时候如果出现乱码问题,注意字符编码格式统一

与字节流类型,不过只能针对纯文本使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值