一篇文章带你快速入门Java IO流

文件:–>文件是保存数据的地方比如大家使用的excel,txt文件等,也可以保存图片,音频
文件流—>文件在程序中是以流的形式在操作的
在这里插入图片描述
流:数据在数据源(文件)和程序(内存)之间的经历
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径

创建文件

创建文件对象相关构造器和方法

new File(String pathname) //根据路径构造一个File对象
new File(File parent,String chile)//根据父目录文件+子路径构造
new File(String parent,String child)//根据父目录+子路径构造
createNewFile 创建新文件

创建一个文件—>方式一

//new File(String pathname) //根据路径构造一个File对象
String filePath="g:\\new1.txt";
File file = new File(filePath);
try {
     file.createNewFile();
     System.out.println("文件创建成功");
     } catch (IOException e) {
         e.printStackTrace();
   }

在这里插入图片描述

创建一个文件—>方式二

//new File(File parent,String chile)//根据父目录文件+子路径构造
File parentFile = new File("g:\\");
String fileName="news2.txt";
File file = new File(parentFile, fileName); //内存中创建对象
    try {
        file.createNewFile();  //磁盘中创建出来也就是真正的文件
        System.out.println("第二种方式创建文件");
    } catch (IOException e) {
        e.printStackTrace();
      }
    }

在这里插入图片描述

创建一个文件—>方式三

//new File(String parent,String child)//根据父目录+子路径构造
String parent="g:\\";
String child="news3.txt";
File file = new File(parent, child); //内存中创建对象
    try {
        file.createNewFile();  //磁盘中创建出来也就是真正的文件
        System.out.println("第三种方式创建文件");
    } catch (IOException e) {
        e.printStackTrace();
      }

在这里插入图片描述

文件的信息获取


File file = new File("g:\\new1.txt");
System.out.println("文件名字"+file.getName());
System.out.println("文件的绝对路径"+file.getAbsolutePath());
System.out.println("文件的父级目录"+file.getParent());
System.out.println("文件大小(字节)"+file.length());
System.out.println("文件是否存在"+file.exists());
System.out.println("是不是一个文件"+file.isFile());
System.out.println("是不是一个目录"+file.isDirectory());

在这里插入图片描述

文件的目录操作

目录的操作和文件的删除

mkdir //创建一个级目录
mkdirs //创建多级目录
delete //删除空目录或者文件
//删除文件
String filePath="g:\\news1.txt";
File file = new File(filePath);
if (file.exists()){
    if (file.delete()) {
        System.out.println(filePath+"删除成功");
    }else {
        System.out.println(filePath+"删除失败");
    }
}else {
    System.out.println("文件不存在");
}

在这里插入图片描述

//删除目录
String filePath="g:\\news";
File file = new File(filePath);
if (file.exists()){
    if (file.delete()) {
        System.out.println(filePath+"删除目录成功");
    }else {
        System.out.println(filePath+"删除目录失败");
    }
}else {
    System.out.println("目录不存在");
}

在这里插入图片描述

如果找不到相关文件就创建一个

String directoryPath ="G:\\new\\abc";
File file = new File(directoryPath);
if (file.exists()) {
    System.out.println(directoryPath+"存在");
}else {
    if (file.mkdirs()) {
        System.out.println(directoryPath+"创建成功");
    }else {
        System.out.println(directoryPath+"创建失败");
    }
}

在这里插入图片描述

Io原理以及流的分类

在这里插入图片描述
javaIO 流原理
输入input :读取外部设备数据(磁盘,光盘等存储设备的数据)到程序(内存中)
输出output设备:将程序(内存)数据输出到磁盘,光盘等存储设备中。
在这里插入图片描述
流的分类
按操作数据单位不同分为:字节流(8bit)字符流(按字符)
按数据流的流向不同分为::输入流,输出流
按流的校色不同分为:节点流,处理流,包装流
在这里插入图片描述

FIleInputStream

InputStream :字节输入流
InputStream抽象类是所有类字节输入流的超类
InputStream 常用的子类

FilrInputStream 文件输入流
BufferedInputStream :缓冲字节输入流
ObjectInputStream :对象字节输入流

在这里插入图片描述

构造讲解

FileInputStream(File file) //通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
FileInputStream(String name) //通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。 

常见方法

int read​() //从该输入流读取一个字节的数据。  
int read​(byte[] b) // 从该输入流读取最多 b.length个字节的数据到一个字节数组。  
int read​(byte[] b, int off, int len) //从该输入流读取最多 len个字节的数据到字节数组。 
void close​() //关闭此文件输入流并释放与流相关联的任何系统资源。  

读入缓冲区的总字节数,或者如果没有更多的数据,因为文件的结尾已经到达, -1 。

String filePath="g:\\news2.txt";
int readData=0;
FileInputStream fileInputStream=null;
fileInputStream=new FileInputStream(filePath);
while (((readData = fileInputStream.read()) != -1)) {
    System.out.print((char) readData);
}
//关闭文件流,释放资源
fileInputStream.close();

在这里插入图片描述

单个字节读取效率比较低

byte[] buf = new byte[8];
int readLen=0;
//扩大作用范围
FileInputStream fileInputStream=null;
fileInputStream=new FileInputStream(filePath);
while (((readLen = fileInputStream.read(buf)) != -1)) {
    System.out.print(new String(buf,0,readLen));
}
fileInputStream.close();

补充字节解码为字符

public String(byte[] bytes,int offset,int length)
//通过使用平台的默认字符集解码指定的字节子阵列,构造新的String 。 新的String的长度是字符集的函数,因此可能不等于子String的长度。 
bytes - 要解码为字符的字节 
offset - 要解码的第一个字节的索引 
length - 要解码的字节数 

每次读取八个字节这样效率高一些

FIleOutputStream

文件输出流是用于将数据写入File或FileDescriptor的输出流。 文件是否可用或可能被创建取决于底层平台。 特别是某些平台允许一次只能打开一个文件来写入一个FileOutputStream (或其他文件写入对象)。 在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败。

void write​(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。  
void write​(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。  
void write​(int b) 将指定的字节写入此文件输出流。  

如果写入文件不存在就会创建一个文件

String filePath="g:\\abc.txt";
FileOutputStream fileOutputStream=null;
//内容会覆盖
fileOutputStream=new FileOutputStream(filePath);
String str="hello,world";
fileOutputStream.write(str.getBytes());
fileOutputStream.close();

在这里插入图片描述
在这里插入图片描述

fileOutputStream=new FileOutputStream(filePath,true);

文件拷贝

在这里插入图片描述

/*
完成文件拷贝 将abc.png-->qwe.png
1:创建文件的输入流,将文件读入到程序
2:创建文件的输出流,将读取到的文件数据写入到指定的文件
 */
String srcFilePath="g:\\abc.png";
String destFilePath="g:\\qwe.png";
FileOutputStream fileOutputStream=null;
FileInputStream fileInputStream=null;
fileInputStream=new FileInputStream(srcFilePath);
fileOutputStream=new FileOutputStream(destFilePath);
byte[] buf = new byte[8];
int readLen=0;
while (((readLen = fileInputStream.read(buf)) !=-1)) {
    //防止产生空字节导致图片不显示
    fileOutputStream.write(buf,0,readLen);
}
fileInputStream.close();
fileInputStream.close();

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值