File类和IO流

File类

一.创建文件或者文件夹

1.创建文件


File file1 = new File("D:\\x0909.txt");
//创建一个文件
boolean flag1 = file1.creatNewFile();
System.out.println(flag1?"创建成功":"创建失败");

2.创建文件夹

File file2 = new File("D:\\x09090");
boolean flag2 = file2.mkdir();
System.out.println(flag2?"创建成功":"创建失败");

3.创建多级文件夹

File file3 = new File("D:\\dsjg\\dsd\\dsd");
boolean flag3 = file3.mkdirs();
System.out.println(flag3?"创建成功":"创建失败");

二.删除文件

boolean flag1 = File.delete();

三.查询文件        

  1. 判断文件路径名是否存在
boolean flag1 = file1.exists();
if(flag1){
        System.out.printf("");
}else{
        file1.mkdir();
}

      2.获取文件大小

File file2 = new File("D:\\8989");
file2.creatNewFile();
Long size = file2.length();
System.out.printf();

        3.获取文件名

String fileName = file2.getName();

        4.获取文件路径

String path = file2.getPath();

四.递归 (遍历文件)

递归:方法自己调用自己

isDirectory():判断文件夹是否有文件

listFiles():查询某个文件下的所有文件

public static void showFile(String pathName){
        File f1 = new File(pathName);
        //1.判断某个文件夹是否有文件
        boolean flag1 = f1.isDirecyory();
        //2.选择某个文件下所有文件
        if(flag1){
            File[] files = f1.listFiles();
            if(int i = 0;files[i]!=null,i<files.length;i++){
                                        boolean flag2 = files[i].isDisectory();
                                        if(flag2)
                                                showFile(files[i].isDirectory())
                                      }else{
                                            String filePath = f1.getPath();
                                            System.out.pritnf("普通文件->"+filepath);
                                            }
         }else{
                //获取此文件的路径
                String filepath = f1.getPath();
                System.out.println("普通文件->"+filePath);
               }
}
public static void main (String[] args){
        showFile("D:\\");
}

IO流

字节流

1.字节输入流

//效率低
//1.建立文件和程序之间的通道
try{
File f1 = new File("D:\\9090.txt");
FileInputStream fis = new FileInputStream(f1);
//2.打开通道
int ch = 0;
while((ch = fis.read())! = -1){
    System.out.pritnf((char)ch);
}
//3.关闭通道
fis.close();
}catch(Exception e){
System.out.printf("文件找不到。")}

//效率高
try{
File f1 = new File("D:\\9090.txt");
//1.建立文件和程序之间的通道
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bis = new BufferedInputStream(fis);//BufferedInputStream()形参必须是FileInputStream类型
//2.打开通道
Byte[] car = new byte[1024];//创建读取数据的数组
int length = 0;
while((len = bis.read(car)) != -1){
System.out.printf(len);
};
//3.关闭通道
fis.close();
bis.close();
}catch(Exception e){
e.printStackTrace();
}

2.字节输出流

//效率低
try{
    //1.要输出的内容
    String data = "hello html,hello,ps"
    //2.建立程序通往文件的通道
    FileOutStream fos = new FileOutputStream("D:\\89");
    FileOutStream fos = new FileOutputStream("D:\\89",true);//可以接着写,不删除原来数据
    //3.打开通道
    byte[] tempByte = data.getBytes();//将String类型转换为write()所需的Byte类型
    fos.write(tempByte);
    //4.关闭通道
    fos.close();
}catch(Exception e){
    e.printStaokTrace();
}
//高效率
try{
File f1 = new File("D:\\9090.txt");
//1.建立文件通向程序的通道
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bis = new BufferedInputStream(fis);

//2.建立程序通向文件的通道
FileOutputStream fos = new FileOutputStream(new File("890.txt"));
BufferedOutputStream bos = new BufferedOutputStream(fos);

//3.打开通道
byte[] car = new byte[1024];//创建存放数据的数组
int len = 0;
while((leng = bis.read(car))! = -1){
   bos.write(car,0,len)//将数组car[]从下标0开始的len个字节输入到bos
}
//3.关闭通道
bis.close();
bos.close();
}catch(Exception e){
System.out.printf("文件找不到。")}

3.IO实现复制


try{
//1.建立文件通向程序的通道
File f1 = new File("D:\\9090.txt");
FileInputStream fis = new FileInputStream(f1);
//建立程序通向文件的通道
FileOutputStream fos = new FileOutputStream("D:\\89.txt");
//2.打开通道
int ch = 0;
while((ch = fis.read())! = -1){
   fos.write(ch);
}
//3.关闭通道
fis.close();
fos.close();
}catch(Exception e){
System.out.printf("文件找不到。")}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值