对文件的操作

2 文件

 

文件操作,对电脑文件、文件夹操作过程

2.1 文件夹分隔符

 

"\n"

/ 通用的分隔符

windows:

c:\

linux

/root/abc

2.2 文件操作

 

2.2.1 创建一个文件

 

for (int i=0;i<10;i++) {

        String path = "D:/birth"+i+".txt";

        File myfile = new File(path);

        myfile.createNewFile();

    }

2.2.2 删除

2.2.3 查询

 

 

 

 

3 问题

 

5! = 5x4x3x2x1

递归算法:  自己调用自己

5x4x3x2x1

5

5 x 4!                             nx f(n-1)

      4x3!

         3x2!

            2x1!

public static int f(int n) {

    if (n == 1 || n == 2) return 1;

    return f(n - 1) + f(n - 2);

}

public static void main(String[] args) {

    System.out.println(f(6));

}

4 文件查询

 

查询C:\Windows 文件下面的所有文件

public class FileFind {

private static int n = 0;

public static void f(File file) {

    if (file.isFile()) {  // 如果是文件,终止条件

        //System.out.println(file.getName());

        n++;

        String name = file.getName();

        if (name.endsWith(".jpg") || name.endsWith(".png")

                || name.endsWith(".gif") || name.endsWith(".bmp")) {

            System.out.println(name);

        }

        return;

    }

    File[] files = file.listFiles(); //文件夹

    if (files != null) {

        for (File fs : files) {

            f(fs);

        }

    }

}

public static void main(String[] args) {

    String path = "C:/";

    File file = new File(path);

    f(file);

    System.out.println("文件数:" + n);

 }

}

5 流(Stream)

 

I/O     输入输出

5.1 读/写

 

输入、输出

Input/ Output

Scanner     System.out.println()

参照物:  内存

5.1.1 流的分类

5.1.1.1 方向

 

 

 

 

输入流、输出流

5.1.1.2 大小

 

 

字节流(视频、音频、图片)、字符流(文本)

5.2 InputStream

 

5.3 OutStream

 

public class OutputStreamDemo {

    public static void main(String[] args) throws Exception {

        String path = "D:/test/test0.txt";

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一句话(English):");

        String line = sc.nextLine();

        OutputStream out = new FileOutputStream(path);

        for (int i=0;i<line.length();i++) {

            int n = line.charAt(i);

            out.write(n);

        }

        out.close();

    }

}

6 文件拷贝

 

InputStream in = new FileInputStream("D:\\HCJ_2312\\01_javase\\2024_0602\\video\\20240602-

06-map-02.mp4");

OutputStream out = new FileOutputStream("D:\\my.mp4");

int n = 0;

long s = System.currentTimeMillis();

byte[] buffer = new byte[1024*1024];// 1kb

while((n = in.read(buffer))!=-1){

    out.write(buffer);

}

long e = System.currentTimeMillis();

out.close();

in.close();

System.out.println("拷贝耗时:"+(e-s)+"毫秒");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值