Java流、字节输入输出流、释放

File类:

注:File对象只能对文件进行操作,不能操作文件中的内容

File的构造方法:

public File(String pathname) :根据文件路径创建文件对象。
public File(String parent, String child) :根据父路径和子路径名字创建文件对象。
public File(File parent, String child) :根据父路径相应文件对象和子路径名创建文件对象。
常用方法:

Java流:

File只能操作文件,但是不能操作文件中的内容。不同的字符集存字符数据的原理是不一样的。有了这两个知识的基础,再学IO流,就可以对文件中的数据进行操作了。

IO流的作用:

可以对文件或网络中的数据进行读、写的操作

输入流(Input):把数据从磁盘、网络中读取到程序中来
输出流(Output):把程序中的数据写入磁盘、网络中

Java的输出流主要由OutputStream类和Write类作为基类;而输入流则主要由InputStream类和Reader类作为基类。

 

 字节流、字符流的基类都是抽象类

InputStream:

InputStream是一个抽象类,意味着不能直降实例化它,需要使用它的具体子类来进行实例化,这些子类提供了特定类型数据源的实际实现。

FileInputStream:

是InputStream的子类。

使用FileInputStream读取文件中的字节数据,步骤如下:

  • 创建FileInputStream文件字节输入流对象,与源文件接通。

  • 调用read()方法开始读取文件的字节数据。

  • 调用close()方法释放资源

public static void main(String[] args) {
    String name = "E:/a.txt"; //hello world!
    try {
         FileInputStream fileInputStream = new FileInputStream(name);
//          int read=fileInputStream.read();
//          System.out.println(read); //104
         byte[] bytes=new byte[9];
         fileInputStream.read(bytes);
         System.out.println(new String(bytes)); //hello wor
//          byte[] bytes=fileInputStream.readAllBytes();
//          System.out.println(new String(bytes)); //hello world!
//          fileInputStream.close();
     }catch (IOException e) {
         throw new RuntimeException(e);
     }
}
String filename="E:/t.txt";
        File file=new File(filename);
        file.createNewFile();
        System.out.println("cg");
        FileInputStream fileInputStream=null;
        byte[] bytes=new byte[6];
        int readlen=0;
        try{
            fileInputStream=new FileInputStream(file);
            int a=0;
//            while((a=fileInputStream.read())!=-1){
            while((readlen=fileInputStream.read(bytes))!=-1){
//                System.out.print((char)a);//因为读取的是一个一个字节,返回int类型
                System.out.println(new String(bytes,0,readlen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }

OutputStream:

OutputStream 也是一个抽象类,因此不能直接实例化,但可以通过其子类来创建实例。

FileOutputStream:

用于向文件写入字节数据的输出流。

构造方法

FileOutputStream(File file): 创建文件输出流以写入指定File对象表示的文件。

FileOutputStream(File file, boolean append): 创建文件输出流以写入指定File对象表示的文件,append表示是否拼接。

FileOutputStream(String name): 创建文件输出流以写入具有指定名称的文件。

FileOutputStream(String name, boolean append): 创建文件输出流以写入具有指定名称的文件, append表示是否拼接。

常用方法

使用FileOutputStream往文件中写数据的步骤如下:

  • 创建FileOutputStream文件字节输出流管道,与目标文件接通。

  • 调用wirte()方法往文件中写数据

  • 调用close()方法释放资源

String filepath="E:/t.txt";
        FileOutputStream fileOutputStream=null;
        try{
            fileOutputStream=new FileOutputStream(filepath);//会覆盖原来的内容
            //fileOutputStream=new FileOutputStream(filepath,true);//追加的方式
            fileOutputStream.write('A');
            String str="hello hello";
            fileOutputStream.write(str.getBytes());
            fileOutputStream.write(str.getBytes(),0,3);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

IO流资源释放:

JDK7以前的资源释放用try...catch...finally语句来处理。

try{
     // 有可能产生异常的代码
 }catch(异常类 e){
     // 处理异常的代码
 }finally{
     // 释放资源的代码
     // finally里面的代码有一个特点,不管异常是否发生,finally里面的代码都会执行。
 }

JDK7以后资源释放:try-with-resourses

它会自动释放资源,代码写起来也相对简单

// JDK 7 之后 使用 try-with-resources 实现了 AutoCloseable 这个接口的类 对象才可以
try(FileInputStream fis = new FileInputStream(file);  // 自动调用close() IOException
    FileOutputStream fos = new FileOutputStream(dest);) {
    //            fis.read();
    //            fos.write(1);
} catch(IOException e) {
    System.out.println(e.getMessage());
} finally {
    System.out.println(1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值