1、Java文件类----描述文件信息-----用于文件和目录的创建、文件的查找和文件的删除等。
2、File类是对磁盘(硬盘[电脑自带盘符C盘,D盘,外接U盘])文件的描述【对文件具体路径的一种描述】
3、类中定义一些常用属性和操作文件的功能 创建File对象的时候通过路径来创建
路径:D:aabbccdd1.jpg
例:
public static void main(String[] args) throws IOException {
//通过Java类的形式描述这个文件
File file = new File("D:\aa\bb\cc\dd.jpg");
//具体操作
file.createNewFile();
System.out.println(file);
}
}
4.路径
绝对路径:根目录(具体磁盘)到具体文件名
Windows系统下根路径—>盘符(C: D: E: F:)
MacOS,Linux,Unix无盘符---->根目录:/
相对路径:基于工程路径
写法:D:/a/b/c/d.txt 或 D:abcd.txt
写两个 通过一个转译(去除含义)让第二个原样输出。
1.1构造方法
1、File(String path):创建一个以path路径为目标文件的文件对象
2、File(String parent, String child):创建一个以parent+child拼接后的路径为目标文件的文件对象
3、File(File file, String child):创建一个以file文件对象的路径+child拼接后的路径为目标文件的文件对象
public static void main(String[] args) {
//直接指定完整的路径
//绝对路径
File file1 = new File("D:\aa\bb\cc\f.txt");
//相对路径
File file2 = new File("src\f.txt");
//拼接路径 路径+文件名这种拆分
File file3 = new File("D:\aa\bb\cc\","f.txt");
//文件类 + 路径的组合
File file4 = new File("D:\aa\bb\cc\");
File file5 = new File(file4,"f.txt");
}
1.2 File类方法
创建相关方法
boolean createNewFile() 创建新的指定的文件,成功true 失败false – 必须有路径才能成功
boolean mkdir() 创建单级文件夹
boolean mkdirs() 创建多级文件夹
public static void main(String[] args) throws IOException{
File file = new File("D:\aa\bb\cc\dd.mp4");
//有具体路径和文件名才能创建
boolean b = file.createNewFile();
System.out.println("b = " + b);
//如果有不全的路径和文件名--系统找不到指定的路径。
File file1 = new File("D:\aa\bb\cc\hh\dd.mp4");
boolean b1 = file1.createNewFile();
System.out.println("b1 = " + b1);
}
//
public static void main(String[] args) throws IOException{
File file = new File("D:/课程记录/works/JAVA");
//此时没有文件夹,需要手动创建
//boolean b = file.mkdir(); //只能创建一层
boolean b = file.mkdirs(); //可以创建多层目录
//创建文件
File file1 = new File(file,"file.ppt");
boolean b1 = file1.createNewFile();
System.out.println("b1 = " + b1);
}
1.3delete() 和deleteonExit()
1、delete() :执行立刻删除,不走回收站,可以删除文件和空文件夹。
2、deleteonExit() 随时可以调用,但是需要JVM终止的时候才会执行删除,不走回收站,可以删除文件和空文件夹。
public static void main(String[] args) {
File file=new File("D:/ff.txt");
file.delete();//在D盘删除文件ff.txt
file.mkdirs();//在D盘新建新文件夹ff.txt
}
1.4File的判断功能
1、boolean exists() 判断调用对象对应的文件是否在磁盘中存在
2、boolean isFile() 判断调用对象对应的文本文件是不是文件
3、boolean isDirectory() 判断调用对象对应的文本文件是不是文件夹
1.5File的获取功能
1、getAbsolutePath() 获取文件对象的绝对路径
2、getPath() 获取构造方法传入的路径
3、getName() 获取文件对象的文件名
4、length() 获取文件中内容的长度,单位字节
5、String[] list() 获取文件夹下所有文本文件的文件名,返回值是一个字符串
数组
6、File[] listFiles() 获取文件夹下所有文本文件的File对象,返回值是一个File数组
public class Lx1 {
public static void main(String[] args) {
File file=new File("D:/ff");
//绝对路径
System.out.println(file.getAbsolutePath());
//获取构造方法传入的路径
System.out.println(file.getPath());
//获取文件名
System.out.println(file.getName());
//获取文件大小
System.out.println(file.length());
//获取所有文件信息
String[] list = file.list();
System.out.println(Arrays.toString(list));
//获取所有文件并转化为File对象返回
File[] files = file.listFiles();
System.out.println(Arrays.toString(files));
}
}
练习:
键盘录入一个字符串,表示一个文件夹路径,
如果不是文件夹路径则提示重新录入
是文件夹: 所有的大于 50M 的后缀名是.mp4文件的绝对路径打印
分析:
1、键盘录入一个字符串---- 文件路径
2、把录入的字符串路径变为一个File对象
3、判断File对象对应的文件是不是文件夹【isDirectory()】 重新输入–123三步重新来一遍 ---- 循环【死循环】
判断中:
不是文件夹:提示一下 回归循环
是文件夹: 打印当前文件夹下的所有文件 ------ 遍历文件夹— listFiles的得到子文件的File对象
判断文件是不是符合大于 50M 的后缀名是.mp4文件条件
1M = 1024 * 1024 [文件的大小---- length()] & 后缀名是.mp4 — 名字以.mp4结尾 [getName() endsWith()]
打印对应File对象的绝对路径[getAbsolutePath()] 最终结束循环
import java.io.File;
import java.util.Scanner;
public class Lx2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (true){
String root=sc.next();
//转化File
File file=new File(root);
if (file.isDirectory()){
File[] files=file.listFiles();
for (File f:files){
//验证后缀endsWith
boolean b=f.getName().endsWith(".mp4");
if (b){
if (f.length()>50*1024*1024){ System.out.println(file.getAbsolutePath()+"\"+f.getName());
}
}
}
}else {
System.out.println("不是文件夹,请重新输入");
}
}
}
}
2 、I/O流
字节输入流: InputStream
字节输出流: OutputStream
字符输入流: Reader
字符输出流 :Writer
这四个类型类都是抽象类,都不能直接创建对象. java中流使用类来进行描述的,使用的时候创建对应对象,直接使用jdk提供的即可
定义:在使用IO流对数据进行传输的时候使用的以字节为单位操作数据的流。
3.1 InputStream
1、int read() 一次读取一个字节数据到内存
2、int read(byte[] bytes) 使用字节数组去读取多个字节的内容到内存
3、int available() 返回未读取的字节的个数
4、close() 关闭流,所有的io流都有的一个方法
3.2 FileInputStream
InputStream的子类,负责完成磁盘文本文件到内存数据输入交互的操作,功能都是InputStream的功能
构造方法:
1、FileInputStream(File file) 创建一个可以读取指定File对象对应的文件的字节输入流对象
2、FileInputStream(String path) 创建一个可以读取指定路径对应的文件的字节输入流对象
public class FileInputStreamExample {
public static void main(String[] args) {
try {
//抽象父类 实现类(文件字节输入流)
//InputStream inewFileInputStream("D:\log.out");
InputStream is = new FileInputStream(new File("D:\log.o
ut"));
System.out.println("is = " + is);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
3.2.1 int read()
1.字节输入流对象每次读取一个字节,光标向后移动一位,每次读取以光标所在位置开始读取,有返回值,返回值表示读取的字节,
2.所有的字节数读取完毕继续读取的时候得到的是-1
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream(new File("D:\ff.txt"));
//单个读取
//循环读取 -1 表示读取完成
int b = -1;
while((b = is.read()) != -1){
System.out.println(b);
}
}
3.返回值int
InputStream.read()返回一个unsigned byte [0 - 255],而Java里面没有这个类型,所以用int接收。
byte的范围是[-128,127],所以如果read()返回的数在[128,255]的范围内时,则表示负数,即
(byte)128=-128
(byte)129=-127
(byte)130=-126
(byte)255=-1
所以如果read()返回的是byte的话,那就会有负数。
而"返回-1意味着结束",这个信息量用byte是无法表达的,所以必须用int。
3.2.2 int read(byte[] bs) *
read(byte[] bs)方法, 希望一次读取多个,单位字节数组
大水池子(文件->流) 想清空这个水池的水
方案一:一滴一滴的取 - 效率慢。 read()
方案二:一盆(数组)一盆的取 - 效率高。read(byte[] bs)
代码示例:
假设文件有15字节
byte[] bs = new byte[5]; 5 5 5
byte[] bs = new byte[4]; 4 4 4 3
方法结束的标志, 所有的字节数读取完毕继续读取的时候得到的是-1
实际开发中字节数组长度一般给1024或者1024的整数倍.最大一般是8倍(8192)
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream(new File("D:\ff.txt"));
//手动一盆一盆
//自动一盆一盆
byte[] b = new byte[1024];
int len = 0; //长度
while ((len = is.read(b)) != -1){
//System.out.println(Arrays.toString(b));
System.out.println(new String(b));
}
3.3 OutputStream
1.OutputStream是字节输出流的顶层父类,是一个抽象类,定义了所有的字节输出流的共性功能
2.OutputStream自己不能创建对象,不能使用自己的功能,只能找他的子类子类根据设备不同有不同的子类,
3.常见的子类:FileOutputStream
常用方法:
1.void write(int b) 输出字节内容到指定的目标文件中
2.void write(byte[] arr) 输出字节数组中所有的字节内容到目标文件中
3.void write(byte[] arr, int offset, int len) 输出字节数组中指定范围的字节内容到目标文件中
4.offset:输出的字节数组开始索引值
5.len:输出的字节个数
6.void close() 关闭流
文件字节输出流(纯文本文件和流媒体文件都可以使用这种流写出),是OutputStream的子类,负责内存和磁 盘文件之间的数据的输出交互的流,功能都是OutputStream的功能
3.4.1 构造方法
FileOutputStream(File file):创建以指定File对象对应的文件为目标文件的字节输出流对象
FileOutputStream(String path):创建以指定path路径对应的文件为目标文件的字节输出流对象
OutputStream os = new FileOutputStream(“D:mingzi.txt”);
OutputStream os = newFileOutputStream(new File(“D:mingzi.txt”));
3.4.2 void write()
//单字节输出
public static void main(String[] args) throws IOException {
//如果没有文件会默认自动创建,但是不能创建文件夹
OutputStream os =newFileOutputStream("D:\mingzi.txt");
//单字节输出 -- 写入的时候先格式化这个文件。
os.write(97);
os.write(98);
os.write(-27);
os.write(-104);
os.write(-65);
//获取一个中文的字节
byte[] bytes = "嘿".getBytes();
System.out.println(Arrays.toString(bytes));
os.close();
}
3.4.3 void write(Byte[] , off, len)
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("D:\mingzi.txt");
//你好呀
byte[] arr = new byte[]{-27, -104, -65,-27, -104, -65};
//os.write(arr); //将整个的数组进行输出
os.write(arr,0 , arr.length); //基本使用这种
os.close();
}
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("D:\ff.txt"); //输入流
OutputStream os = new FileOutputStream("D:\11\ff.txt");//输出流
//读取到内存3个字
byte[] arr = new byte[6];
int len = 0; //每次读取字节长度
while ((len = is.read(arr)) != -1){
//第一次[-28, -67, -96, -27, -91, -67]
//第二次[-27, -111, -128, -27, -91, -67]
System.out.println(new String(arr,0,len));
//输出流
os.write(arr,0,len);
}
is.close();
os.close();
}
3.5 字节流文件拷贝
把一个指定的文件复制到指定的另一个路径文件中。 比如:把一张图片复制到另一个目录文件下
字节数组文件拷贝
public static void main(String[] args) {
InputStream is = null; //输入
OutputStream os = null;//输出
try {
is = new FileInputStream("D:/log.out");
os = new FileOutputStream("D:/logCopy.out");
//容器
byte[] b = new byte[1024*10];
//获取每次读取的长度
int len = 0;
while ((len = is.read(b)) != -1){
//向外输出
os.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.字节缓冲流
1.字节缓冲流本身不具备读写文件的能力.但是它们可以增强那些具备字节读写能力流的效率.
2.为了增加普通字节输入输出流的效率.普通字节流(FileInputStream和FileOutputStream). 通过缓冲流可以提升普通字节输出流的效率. 字节缓冲流要想使用前提是必须要有普通字节流。
1.BufferedInputStream(InputStream is) 创建一个以is为提升目标的字节输入缓冲流对象
2.把一个普通字节输入传入到BufferedInputStream构造方法,就可以增强这个普通字节输入流的读取能力.
3.BufferedOutputStream(OutputStream os) 创建一个以os为提升目标的字节输出缓冲流
4.把一个普通字节输出传入到BufferedOutputStream构造方法,就可以增强这个普通字节输出流的写出能力
方法:
BufferedInputStream: read() close()
BufferedOutputStream:write() close()
测试单字节输入输出流
public class BufferedInputStreamExample {
public static void main(String[] args) throws IOException {
//文件字节输入流
FileInputStream is = new FileInputStream("D:/GitHub笔记.docx"
);
//字节缓冲输入流
BufferedInputStream bis = new BufferedInputStream(is);
//文件字节输入流
FileOutputStream os = new FileOutputStream("D:/GitHub笔记xxx.
docx");
//字节缓冲输入流
BufferedOutputStream bos = new BufferedOutputStream(os);
long start = System.currentTimeMillis();
//单字节无缓冲流 耗时:3514毫秒
//单字节有缓冲流 耗时:39毫秒
int b = -1; //代表返回的字节
while ((b = bis.read()) != -1){
bos.write(b);
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end-start) +"毫秒");
bis.close();
is.close();
bos.close();
os.close();
}
}
测试字节数组输入输出流
package com.its.buffered;
import java.io.*;
public class BufferedInputStreamExample {
public static void main(String[] args) throws IOException {
//文件字节输入流
FileInputStream is = new FileInputStream("D:/1901-学生管理系统
框架设计.mp4");
//字节缓冲输入流
BufferedInputStream bis = new BufferedInputStream(is);
//文件字节输入流
FileOutputStream os = new FileOutputStream("D:/测试视频xxx.mp
4");
//字节缓冲输入流
BufferedOutputStream bos = new BufferedOutputStream(os);
long start = System.currentTimeMillis();
//单字节无缓冲流 耗时:1347毫秒
byte[] b = new byte[1024*1024];
int len = 0;
while ((len = is.read(b)) != -1){
os.write(b,0,len);
}
//单字节有缓冲流 耗时:1263毫秒
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end-start) +"毫秒");
bis.close();
is.close();
bos.close();
os.close();
}
}
4.2普通字节流和缓冲流对比
1.输入:
当使用字节输入缓冲流读取字节的时候
首先缓冲流对象在内存中创建一个8192大的字节数组
调用读取的方法(read()),
一次性的到目标文件读取8192个字节回来缓存到字节数组buf中
然后内部的字节输入流每次根据调用的读取方法到字节数组中读取对应的个数到内存中,
减少了内存和目标文件(硬盘)的交互次数,提高效率。
2.输出:
当使用字节输出缓冲流往外写字节的时候,首先缓冲流对象在内存中创建一个8192大的字节数组,
字节输出流每次根据调用write()方法
把要写出的字节数写到字节数组buf中,
如果字节数组被写满了自动的一次性写(flush())到目标文件中之后清空字节数组再次的写,
减少了内存和目标文件(硬盘)的交互次数,提高效率。
3.flush()作用
刷新缓冲区。刷新缓冲数组中数据到目标文件,也就是把buf数组中字节刷新
到目标文件。
1.flush方法只是用来刷新字节输出缓冲流的缓冲区内容的方法,调用之后流对象还存在的可以继续使用,但是不要频繁的时候flush方法,会增加内存和目标文件交互次数,降低输出的效率.
2.close方法用来关闭流对象,默认先调用flush方法刷新缓冲区后关闭流对象方法,调用之后流对象不存在的,不能继续使用,即使flush在字节数组没有被写满的情况下,也会强制把数据写到目标文件中,确保数据完整。
要求目标文件需要手动创建 文件夹 D:/aa/bb/cc/mb.*
public static void main(String[] args) throws IOException {
File file = new File("D:/aa/bb/cc/");
if(!file.exists()){
file.mkdirs();
}
InputStream is = new FileInputStream("D:\vue.config.js");
OutputStream os = new FileOutputStream(new File(file,"vue.confi
g.js"));
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os,8192*2);
//缓冲池容量
int len = 0;
byte[] b = new byte[8192/2]; //缓冲池容量是自己定义的容量的倍数
while ((len = bis.read(b)) != -1){
bos.write(b,0,len);
}
bis.close();
bos.close();
//此时is和os 因为缓冲流关闭之后也一起关闭了所以可以省
略
//System.out.println(is.available());
}



