July 22, 2022
By: ldy
Java File文件基础操作 -clj版本
Java File文件基础操作 -clj版本
Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。
File对象代表磁盘中实际存在的文件和目录。
材料主要来自https://docs.oracle.com/javase/tutorial/essential/io/index.html IO Stream 部
分相关的 Class 在 java.io 包下. 文件相关操作在 java.io.file,java.nio.file 包下
引入依赖:
(import [java.io File])
说明: 本文档都是在jar包同目录下进行,所以
(def jarpath (System/getProperty "user.dir"))
1.文件存储信息
getTotalSpace 文件存在大小,getFreespace 文件未分配字节大,getUsablespace 文件存储上此java虚拟机可用字节数
使用场景:当电脑某个磁盘分区可用空间剩余多少G或者剩余空间剩余百分之几的时候,磁盘告警。
1.1 指定路径所属文件存储的存储信息
(let [file (File. (System/getProperty "user.dir"))
total (.getTotalSpace file)
unused (.getFreeSpace file)
avail (.getUsableSpace file)
used (- total unused)]
["total size(GB)" (int (/ total 1024 1024 1024))
"unused size(GB) " (int (/ unused 1024 1024 1024))
"available size(GB)" (int (/ avail 1024 1024 1024))
"used size(GB)" (int (/ used 1024 1024 1024))])
1.2 所有可用文件系统根目录的根目录的存储信息
(let [roots (File/listRoots)]
(doseq [file roots]
(prn
["path " (.getPath file)
"total size(GB)" (int (/ (.getTotalSpace file) 1024 1024 1024))
"unused size(GB) " (int (/ (.getFreeSpace file) 1024 1024 1024))
"available size(GB)" (int (/ (.getUsableSpace file) 1024 1024 1024))])))
2.文件创建
当且仅当不存在具有此抽象路径名指定的名称的文件时,原子地创建由此抽象路径名指定的一个新的空文件。
boolean createNewFile()
2.1创建文件,直接创建
前提是次抽象路径名指定名称的文件的父级目录是存在的。
(defn create-file
"创建文件,成功true,失败false"
[file-path]
(let [file (File. file-path)]
(.createNewFile file)))
(comment
(create-file (str jarpath (File/separator) "tmt.text")) ;;=> true
(create-file (str jarpath (File/separator) "tmt.text")) ;;=> false ,文件已存在,创建失败
(create
2.2 创建文件,避免重复创建
(defn create-file-not-exist
"创建文件,成功true,失败false"
[file-path]
(let [file (File. file-path)]
(if (not (.exists file))
(.createNewFile file)
(println "文件已存在,勿重复创建"))))
(comment
(create-file-not-exist (str jarpath (File/separator) "tmt.text")) ;;=> 文件已存在,勿重复创建
(create-file-not-exist (str jarpath (File/separator) "tmt1.text")) ;;=> true ,创建成功
)
2.3 要在一个或多个父目录可能尚不存在时创建目录
(defn create-file-dir
"创建文件,成功true,失败false"
[file-path]
(let [file (File. file-path)
parent-file (.getParentFile file) ;;返回的是File类型,可以调用exsit()等方法
parent-file-path (.getParent parent-file) ;;返回的是String类型
]
(println "parent-file" parent-file)
(println "parent-file-path" parent-file-path)
(when (.exists parent-file)
(.mkdirs parent-file))
(if (not (.exists file))
(.createNewFile file)
(println "文件已存在,勿重复创建"))))
(create-file-dir (str jarpath "/tmt/tmt1/test.txt")) ;;true
3.目录的创建和读取
3.1 目录创建
- 要在一个或多个父目录可能尚不存在时创建多级别目录
- 已存在的目录时创建失败
- 单目录创建,父级目录不存在时创建失败
3.1.1单目录创建
(def dir-path (str jarpath "/tmt"))
(def dir-paths (str jarpath "/tmt/tmt1"))
(.mkdir (File. dir-paths)) ;;=> false
(.mkdir (File. dir-path)) ;;=> true
(.mkdir (File. dir-path)) ;;=> false ,目录已存在
3.1.2 多目录层级创建
要在一个或多个父目录可能尚不存在时创建多个级别的目录
(.mkdirs (File. (str jarpath "/tmt/tmt1"))) ;;=> true
(.mkdirs (File. (str jarpath "/tmt/tmt1")));; => false,目录已存在
3.2 目录内容读取
- 目录下所有内容,返回file类型数组 ,listFiles()
- 目录下所有内容名称,返回string类型数组,list()
3.2.1 目录下所有内容,返回file类型数组
;;目录内容 返回file对象列表
(.listFiles (File. (System/getProperty "user.dir")))
;;=>
[
#object[java.io.File 0x3d7d3c2 "/Users/ldy/RedCreation/train-project/project.clj"],
#object[java.io.File 0x5ac38d8a "/Users/ldy/RedCreation/train-project/dev-config.edn"],
#object[java.io.File 0x6661ce22 "/Users/ldy/RedCreation/train-project/test-config.edn"],
#object[java.io.File 0x11f7965f "/Users/ldy/RedCreation/train-project/test"]]
遍历所有目录内容,打印内容名称
(let [files (.listFiles (File. jarpath))]
(doseq [path-file files]
(prn "=file-name=:" (.getName path-file))))
;;=>
"=file-name=:" "project.clj"
"=file-name=:" "dev-config.edn"
"=file-name=:" "test-config.edn"
"=file-name=:" "test"
"=file-name=:" "Dockerfile"
"=file-name=:" "target"
3.2.2 目录下所有内容名称,返回string类型数组
(.list (File. (System/getProperty "user.dir")))
;;=>
["project.clj", "dev-config.edn", "test-config.edn","test"]
4.文件或目基本信息
判断是否是文件
(.isFile (File. (str (System/getProperty "user.dir") "/project.clj"))) ;;=> true (.isFile (File. (System/getProperty "user.dir"))) ;;=> false判断是否是目录
(.isDirectory (File. (str jarpath "/project.clj"))) ;;=>false (.isDirectory (File. jarpath));;=>true基本信息
(let [file (File. (str jarpath "/project.clj"))]
{:length (.length file)
:path (.getPath file)
:absulute-path (.getAbsolutePath file)
:absulute-file (.getAbsoluteFile file)
:cononical-path (.getCanonicalPath file)
:cononical-file (.getCanonicalFile file)
:parent (.getParent file)
:parent-file (.getParentFile file)
:name (.getName file)
:last-modified (.lastModified file)
:can-read (.canExecute file)
:can-write (.canWrite file)
:can-execute (.canExecute file)})
;;执行结果
{:path "/Users/ldy/RedCreation/train-project/project.clj",
:can-read false,
:parent "/Users/ldy/RedCreation/train-project",
:cononical-path "/Users/ldy/RedCreation/train-project/project.clj",
:name "project.clj",
:can-execute false,
:last-modified 1658420928848,
:can-write true,
:length 2970,
:absulute-path "/Users/ldy/RedCreation/train-project/project.clj",
:parent-file
#object[java.io.File 0x10de460b "/Users/ldy/RedCreation/train-project"],
:absulute-file
#object[java.io.File 0x4c0b51ea "/Users/ldy/RedCreation/train-project/project.clj"],
:cononical-file
#object[java.io.File 0x63ea8962 "/Users/ldy/RedCreation/train-project/project.clj"]}
5.文件或目录删除
boolean delete()
(let [file-path (str jarpath "/tmt/test" (rand-int 10) ".txt")
dirs-path (str jarpath "/tmt/tmt1" )]
(.mkdirs (File. dirs-path))
(.createNewFile (File. file-path))
(println "file exist?:" (.exists (File. file-path)))
(println "dir exist?:" (.exists (File. dirs-path)))
(println "not empty dir:" (.delete (.getParentFile (File. dirs-path)))) ;;=> false 删除非空目录
(println "not exist file:" (.delete (File. (str jarpath "/tmt/test2.txt")))) ;;=> false 不存在文件
(println "not exist dir:" (.delete (File. (str jarpath "/tmtm")))) ;;=> false 不存在的目录
(println "exist empty dir:" (.delete (File. file-path))) ;;=> true 存在的文件
(println "exist file:" (.delete (File. dirs-path))) ;;=> true 存在的空目录
)
5.文件或目录重命名和一点
boolean renameTo()
5.1 重命名
5.1.1 文件重命名
(let [ofile (File. (str jarpath "/tmt/test.txt"))]
(.renameTo ofile (File. (str jarpath "/tmt/test.txt")))
;;=> false 同目录文件名相同
(.renameTo ofile (File. (str jarpath "/tmt/test-renameto.txt")))
;;=> true 同目录不同文件名
(.renameTo (File. (str jarpath "/tmt/testp.txt"))
(File. (str jarpath "/tmt/test-renameto.txt")))
;;=> false 被重命名文件不存在
)
5.1.2 目录重命名
(.renameTo (File. (str jarpath "/tmt/tmt5"))
(File. (str jarpath "tmtn")))
;;=> false 不存在的目录
(.renameTo (File. (str jarpath "/tmt"))
(File. (str jarpath "tmtn")))
;;=> false 非空目录
(.renameTo (File. (str jarpath "/tmt/tmt2"))
(File. (str jarpath "/tmt/tmt22")))
;;=> true 空目录
5.2 文件或目录移动
;;(.mkdirs (File. (str jarpath "/tmt/tmt1")))
;;(.createNewFile (File. (str jarpath "/tmt/tmt1/test.txt")))
;;(.mkdir (File. (str jarpath "/tmt/tmt2")))
;;(.listFiles (File. (str jarpath "/tmt")))
;;(.listFiles (File. (str jarpath "/tmt/tmt2")))
5.2.1 文件移动
有以下几种情况:
源文件和目标文件抽象路径都存在,true
目标文件已存在, false
源文件不存在,false
目标文件父级目录不存在,false
(.renameTo (File. (str jarpath "/tmt/tmt1/test.txt"))
(File. (str jarpath "/tmt/tmt2/test.txt"))) ;;=> true 把test.txt移动到tmt2目录下
(.renameTo (File. (str jarpath "/tmt/tmt1/test.txt"))
(File. (str jarpath "/tmt/tmt2/test.txt"))) ;;=> false 目标文件已存在
(.renameTo (File. (str jarpath "/tmt/tmt1/test.txt"))
(File. (str jarpath "/tmt/tmt3/test.txt"))) ;;=> false 目标目录不存在
(.renameTo (File. (str jarpath "/tmt/tmt1/test1.txt"))
(File. (str jarpath "/tmt/tmt2/test1.txt"))) ;;=> false 源文件不存在
5.2.2 目录移动
- 源目录是空目录 ,true
- 源目录是非空目录,目录下所有文件也会移动目标目录
;;(.mkdir (File. (str jarpath "/tmp")))
(.renameTo (File. (str jarpath "/tmt/tmt1"))
(File. (str jarpath "/tmp/tmt1"))) ;;=> true 空目录移动
(.renameTo (File. (str jarpath "/tmt/tmt2"))
5.3 文件或目录移动并且重命名
(.renameTo (File. (str jarpath "/tmt/tmt1/test.txt"))
(File. (str jarpath "/tmt/tmt2/test2.txt")))
;;=> true 把test.txt移动到tmt2目录下,并且重命民为test2.txt