Java学习-第27天-文件流

1
a.File类创建目录
b.判断文件或目录是否存在
c.删除目录和文件

d. 读取目录中的内容
将一个目录中的所有子目录和文件的名字和相关属性加以输出
判断是文件还是目录

e.查看文件或目录的相关属性

2
FileInputStream,FileOutPutStream
文本文件读出内容
写入文本文件

package GaoJiTexing.chapter3;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IOTset {
    public static void main(String[] args) {
       //fileTest();
       if(writeFile("E:\\abc\\file1.txt")){
           System.out.println("文件写入成功!");
       }else {
           System.out.println("文件写入失败!");
       }

        readFile("C:\\windows\\win.ini");

    }

    public static void fileTest(){
        //a.File类创建目录
        File file=new File("E:/abc1/java1/java2/j");
        //file.mkdir();//创建文件目录
        file.mkdirs();//创建文件多级目录

//        b.判断文件或目录是否存在
        File file1=new File("E:/abc");
        if(file1.exists()){
            System.out.println("文件或目录存在!");
        }else{
            System.out.println("文件或目录不存在!");
        }



        System.out.println("=====");
        File file2=new File("E:/abc1/java1/java2/j");
        if(file2.exists()){
            System.out.println("文件或目录存在!");
            //        c.删除目录或文件
            if(file2.delete()) {
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
        }else{
            System.out.println("文件或目录不存在!");
        }
        System.out.println("=========");


//        d. 读取目录中的内容
//        将一个目录中的所有子目录和文件的名字和相关属性加以输出
//        判断是文件还是目录
        File file3=new File("E:\\Hbulider\\project\\MyHtml1");
        File[] files=file3.listFiles();//组成file类型的数组
        for(File f:files){
            if(f.isDirectory()){
                System.out.println("当前元素是一个目录,"
                        +"该目录名是:"+f.getName());
            }else {
                System.out.println("当前元素是一个文件,"
                        +"该文件的名字是:"+f.getName());
            }
        }

        System.out.println("===========");
//
//        e.查看文件或目录的相关属性
        File file4=new File("E:\\abc\\java1\\java2\\j\\语言基础文档");

        String parentPath=file4.getParent();
        System.out.println("parentPath:"+parentPath);

        if(file4.canRead()){
            System.out.println("能够读取!");
        }else{
            System.out.println("不能读取!");
        }

        if(file4.canWrite()){
            System.out.println("可写");
        }else{
            System.out.println("不可写");
        }

        String path=file4.getAbsolutePath();
        System.out.println("path:"+path);


        long time=file4.lastModified();
        System.out.println(time);
        Date date=new Date(time);
        System.out.println(date);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));

        System.out.println("========");
        System.out.println("size:"+file4.length());
    }
//    2. FileInputStream,FileOutPutStream
//    文本文件读出内容
//    写入文本文件
    public static boolean writeFile(String path) {
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(path,true);//append参数为追加模式
            fos.write("java".getBytes());
            fos.write("\n".getBytes());
            fos.write("你好\n".getBytes());
            fos.flush();//刷新,能够及时将缓存内容写入进文件
            return true;
        }catch (FileNotFoundException e){
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                fos.close();//关闭文件,文件全部写入
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //读文件
    public static void readFile(String path){
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(path);
            byte[] data=new byte[1024];
            fis.read(data);
            String str=new String(data);
            System.out.println(str);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值