文件 - File
# 文件相关函数
## 读取文件文本(软件内部) | readRes
- 功能
读取res文件文本。
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | ----------- |
| file | String | res文件路径 |
- 例子
```groovy
print readRes("option.json")
```
## 读取文件二进制数据 | readResBytes
- 功能
读取res文件二进制数据。
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | ----------- |
| file | String | res文件路径 |
- 例子
```groovy
def bytes = readResBytes("option.json")
print bytes.length
```
## 读取文件文本 | read
- 功能
读取文件文本。
- 参数
| 参数 | 类型 | 说明 |
| ------------ | ------ | -------- |
| file | String | 文件路径 |
| encode[可选] | String | 文件编码 |
- 例子
```groovy
print read("/sdcard/log.txt")
print read("/sdcard/log.txt","utf-8")
```
## 读取文件大小 | length
- 功能
读取文件大小。
- 参数
| 参数 | 类型 | 说明 |
| ------------ | ------ | -------- |
| file | String | 文件路径 |
- 例子
```groovy
def file = new File("/sdcard/hello.txt")
print "文件大小:"+file.length()
```
## 读取文件指定行
- 功能
读取文件大小。
- 参数
| 参数 | 类型 | 说明 |
| ------------ | ------ | -------- |
| file | String | 文件路径 |
- 例子
```groovy
//读取所有行
def file = new File("/sdcard/lines.txt")
//读取文件所有行
def lines = file.readLines()
print "行数量:" + lines.size()
//输出第5行
if (lines.size() > 4) {
// 读取指定行文本
print "第5行:" + lines.get(4)
}
//输出每一行
for (def line : lines) {
print(line)
}
```
## 写入文本 | write
- 功能
写入文本,utf-8编码。
- 参数
| 参数 | 类型 | 说明 |
| ------- | ------ | -------- |
| file | String | 文件路径 |
| content | String | 文本内容 |
- 例子
```groovy
write "/sdcard/log.txt","日志内容"
```
## 追加文件尾 | append
- 功能
向文件末尾追加文本。
- 参数
| 参数 | 类型 | 说明 |
| ------- | ------ | ---------- |
| file | String | 文件路径 |
| content | String | 追加的内容 |
- 例子
```groovy
append "/sdcard/log.txt","日志内容"
```
## 移动文件 | moveFileToFile
- 功能
移动文件到文件。
- 参数
| 参数 | 类型 | 说明 |
| ------ | ------ | ---------------- |
| file | String | 被移动的文件路径 |
| toFile | String | 目标文件路径 |
- 例子
```groovy
moveFileToFile "/sdcard/log.txt","/sdcard/log1.txt"
```
## 移动文件到文件夹 | moveFileToDir
- 功能
移动文件到文件夹。
- 参数
| 参数 | 类型 | 说明 |
| ----- | ------ | ---------------- |
| file | String | 被移动的文件路径 |
| toDir | String | 目标文件夹路径 |
- 例子
```groovy
moveFileToDir "/sdcard/log.txt","/sdcard/log/"
```
## 复制文件 | copyFileToFile
- 功能
复制文件到文件。
- 参数
| 参数 | 类型 | 说明 |
| ------ | ------ | ---------------- |
| file | String | 被复制的文件路径 |
| toFile | String | 目标文件路径 |
- 例子
```groovy
copyFileToFile "/sdcard/log.txt","/sdcard/log1.txt"
```
## 复制文件到文件夹 | copyFileToDir
- 功能
复制文件到文件夹。
- 参数
| 参数 | 类型 | 说明 |
| ----- | --- --- | ------------ |
| file | String | 被复制的文件 |
| toDir | String | 目标文件夹 |
- 例子
```groovy
copyFileToDir "/sdcard/log.txt","/sdcard/log/"
```
## 删除文件 | deleteFile
- 功能
删除文件。
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | ------------ |
| file | String | 被删除的文件 |
- 例子
```groovy
deleteFile "/sdcard/log.txt"
```
## 创建文件 | createFile
- 功能
创建文件。
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | -------------- |
| file | String | 创建的文件路径 |
- 例子
```groovy
createFile "/sdcard/log.txt"
```
## 创建文件夹 | mkdir
- 功能
创建文件夹。
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | ---------- |
| dir | String | 文件夹路径 |
- 例子
```groovy
mkdir "/sdcard/a"
```
## 创建多层文件夹 | mkdirs
- 功能
递归创建多层文件夹
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | :--------- |
| dir | String | 文件夹路径 |
- 例子
```groovy
mkdirs "/sdcard/a/b/c"
```
## 判断文件是否存在 | exists
- 功能
判断文件是否存在
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | :--------- |
| file | String | 文件路径 |
- 例子
```groovy
def ret = new File("/sdcard/").exists()
if (ret){
print("文件存在")
}else{
print("文件不存在")
}
```
## 判断是否为文件 | isFile
- 功能
判断文件是否存在
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | :--------- |
| file | String | 文件路径 |
- 例子
```groovy
def ret = new File("/sdcard/hello.txt").isFile()
if(ret){
print "为文件“
}
```
## 判断是否为文件夹 | isDirectory
- 功能
判断文件是否存在
- 参数
| 参数 | 类型 | 说明 |
| ---- | ------ | :--------- |
| file | String | 文件夹路径 |
- 例子
```groovy
def ret = new File("/sdcard/").isDirectory()
if(ret){
print "为文件文件夹“
}
```
## 释放res文件到指定路径 | releaseFile
- 功能
释放res文件到指定路径。
- 参数
| 参数 | 类型 | 说明 |
| ------- | ------ | ------------ |
| resFile | String | res文件路径 |
| path | String | 释放文件路径 |
- 例子
```groovy
releaseFile("option.json","/sdcard/option.json")
```
## 读写文本
- 读取文本
~~~
def txt = read("/sdcard/hello.txt")
print txt
~~~
- 写入文本
~~~
def txt = "hello"
write("/sdcard/hello.txt",txt)
~~~
## 读取文件大小
~~~
def file = new File("/sdcard/hello.txt")
print "文件大小:"+file.length()
~~~
## 判断文件是否存在
~~~
def ret = new File("/sdcard/").exists()
if (ret){
print("文件存在")
}else{
print("文件不存在")
}
~~~
## 判断是否为文件
~~~
def ret = new File("/sdcard/hello.txt").isFile()
if(ret){
print "为文件"
}
~~~
## 判断是否为文件夹
~~~
def ret = new File("/sdcard/").isDirectory()
if(ret){
print "为文件文件夹"
}
~~~
## 指定编码写入
~~~
new File("/sdcard/hello.txt").write("hello","utf-8")
~~~
## 指定编码读取所有行
~~~
def file = new File("/sdcard/lines.txt")
//判断文件是否存在,如果存在,则读取
if(file.exists()){
def lines = new File("/sdcard/lines.txt").readLines("utf-8")
for(def line : lines){
print line
}
}else{
print("文件不存在~")
}
~~~
## 创建文件
~~~
new File("/sdcard/hello.txt").createNewFile()
~~~
## 创建文件夹
~~~
new File("/sdcard/新建文件夹").mkdirs()
~~~
## 删除文件
~~~
new File("/sdcard/hello.txt").delete()
~~~
## 删除文件夹
~~~
new File("/sdcard/新建文件夹").deleteDir()
~~~
## 读取行
~~~
//写入模拟数据
new File("/sdcard/lines.txt").write("s\ns\ns\nsss\nsdf\nsdfsd\nsdfs\nsdfa")
//读取所有行
def file = new File("/sdcard/lines.txt")
//判断文件是否存在,如果存在,则读取
if(file.exists()){
def lines = file.readLines()
print "行数量:" + lines.size()
//输出第5行
if (lines.size() > 4) {
print "第5行:" + lines.get(4)
}
//输出每一行
for (def line : lines) {
print(line)
}
}
~~~
## 搜索文件(高级功能)*
~~~
import com.jsdroid.commons.FileUtil
//定义一个搜索器
class PngFileSearch implements FileUtil.FileSearch {
def files = []
@Override
boolean stop() {
return false
}
@Override
boolean compare(File file) {
return file.name.endsWith(".png")
}
@Override
void onSearched(File file) {
files.add(file)
}
}
//实例化搜索器
def search = new PngFileSearch()
//搜索图片文件
FileUtil.searchFile(new File("/sdcard/Pictures"), search)
//遍历输出
for(File file :search.files){
print file
}
~~~