2 文件 |
| |
| ||
2.1 文件夹分隔符 |
| |
|
windows:
|
linux
| ||
2.2 文件操作 |
| |
2.2.1 创建一个文件 |
|
| |||
|
|
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 文件查询 |
| |
|
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) |
| |
|
5.1 读/写 |
| |
|
|
|
输入流、输出流
|
|
字节流(视频、音频、图片)、字符流(文本)
5.2 InputStream |
|
5.3 OutStream |
| |
| ||
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)+"毫秒"); |