Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
5ae8ac29
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5ae8ac29
编写于
8月 20, 2016
作者:
C
cmj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 08/20 log
上级
b77b22bc
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
103 addition
and
100 deletion
+103
-100
README.md
README.md
+10
-1
utilcode/build.gradle
utilcode/build.gradle
+1
-1
utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java
...de/src/main/java/com/blankj/utilcode/utils/FileUtils.java
+92
-98
未找到文件。
README.md
浏览文件 @
5ae8ac29
...
...
@@ -82,6 +82,15 @@
> - 将输入流写入文件 *writeFileFromIS*
> - 将字符串写入文件 *writeFileFromString*
> - 简单获取文件编码格式 *getFileCharsetSimple*
> - 获取文件行数 *getFileLines*
> - 指定编码按行读取文件到List *readFile2List*
> - 指定编码按行读取文件到StringBuilder中 *readFile2SB*
> - byte单位转换(单位:unit) *byte2Unit*
> - 获取文件大小 *getFileSize*
> - 根据全路径获取最长目录 *getDirName*
> - 根据全路径获取文件名 *getFileName*
> - 根据全路径获取文件名不带拓展名 *getFileNameNoExtension*
> - 根据全路径获取文件拓展名 *getFileExtension*
> - **图片相关→[ImageUtils.java][image.java]**
> - 完善ing
...
...
@@ -205,7 +214,7 @@
***
Gradle:
```
groovy
compile
'com.blankj:utilcode:1.1.
0
'
compile
'com.blankj:utilcode:1.1.
1
'
```
### Proguard
...
...
utilcode/build.gradle
浏览文件 @
5ae8ac29
...
...
@@ -8,7 +8,7 @@ android {
minSdkVersion
11
targetSdkVersion
23
versionCode
2
versionName
"1.1.
0
"
versionName
"1.1.
1
"
}
buildTypes
{
release
{
...
...
utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java
浏览文件 @
5ae8ac29
package
com.blankj.utilcode.utils
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.BufferedReader
;
import
java.io.Closeable
;
import
java.io.File
;
...
...
@@ -433,7 +434,7 @@ public class FileUtils {
if
(!
createOrExistsFile
(
file
))
return
false
;
OutputStream
os
=
null
;
try
{
os
=
new
FileOutputStream
(
file
,
append
);
os
=
new
BufferedOutputStream
(
new
FileOutputStream
(
file
,
append
)
);
byte
data
[]
=
new
byte
[
KB
];
while
(
is
.
read
(
data
)
!=
-
1
)
os
.
write
(
data
);
return
true
;
...
...
@@ -500,14 +501,14 @@ public class FileUtils {
*/
public
static
String
getFileCharsetSimple
(
File
file
)
{
int
p
=
0
;
BufferedInputStream
bin
=
null
;
InputStream
is
=
null
;
try
{
bin
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
p
=
(
bin
.
read
()
<<
8
)
+
bin
.
read
();
is
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
p
=
(
is
.
read
()
<<
8
)
+
is
.
read
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
closeIO
(
bin
);
closeIO
(
is
);
}
switch
(
p
)
{
case
0xefbb
:
...
...
@@ -558,63 +559,67 @@ public class FileUtils {
}
/**
*
按行读取文件
*
指定编码按行读取文件到List
*
* @param file 文件
* @return 行链表
* @param filePath 文件路径
* @param charsetName 编码格式
* @return 文件行链表
*/
public
static
List
<
String
>
readFile
ByLine
(
File
fil
e
)
{
return
readFile
ByLine
(
file
,
null
);
public
static
List
<
String
>
readFile
2List
(
String
filePath
,
String
charsetNam
e
)
{
return
readFile
2List
(
getFileByPath
(
filePath
),
charsetName
);
}
/**
*
按行读取文件
*
指定编码按行读取文件到List
*
* @param file 文件
* @param charsetName
字符
编码格式
* @return 行链表
* @param charsetName 编码格式
* @return
文件
行链表
*/
public
static
List
<
String
>
readFileByLine
(
File
file
,
String
charsetName
)
{
if
(
file
==
null
)
return
null
;
List
<
String
>
list
=
new
ArrayList
<>();
BufferedReader
reader
=
null
;
try
{
if
(
charsetName
==
null
)
{
reader
=
new
BufferedReader
(
new
FileReader
(
file
));
}
else
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
file
),
charsetName
));
}
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
list
.
add
(
line
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
closeIO
(
reader
);
public
static
List
<
String
>
readFile2List
(
File
file
,
String
charsetName
)
{
return
readFile2List
(
file
,
0
,
0x7FFFFFFF
,
charsetName
);
}
return
list
;
/**
* 指定编码按行读取文件到List
*
* @param filePath 文件路径
* @param start 需要读取的开始行数
* @param end 需要读取的结束行数
* @param charsetName 编码格式
* @return 包含制定行的list
*/
public
static
List
<
String
>
readFile2List
(
String
filePath
,
int
start
,
int
end
,
String
charsetName
)
{
return
readFile2List
(
getFileByPath
(
filePath
),
start
,
end
,
charsetName
);
}
/**
*
读取前几行数据
*
指定编码按行读取文件到List
*
* @param file 文件
* @param endLineNum 需要读取的行数
* @param start 需要读取的开始行数
* @param end 需要读取的结束行数
* @param charsetName 编码格式
* @return 包含制定行的list
*/
public
static
List
<
String
>
readFile
ByLine
(
File
file
,
int
endLineNum
)
{
public
static
List
<
String
>
readFile
2List
(
File
file
,
int
start
,
int
end
,
String
charsetName
)
{
if
(
file
==
null
)
return
null
;
List
<
String
>
list
=
new
ArrayList
<>();
if
(
start
>
end
)
return
null
;
List
<
String
>
list
=
null
;
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
FileReader
(
file
));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
list
.
add
(
line
);
if
(
list
.
size
()
==
endLineNum
)
{
break
;
int
curLine
=
1
;
list
=
new
ArrayList
<>();
if
(
StringUtils
.
isSpace
(
charsetName
))
{
reader
=
new
BufferedReader
(
new
FileReader
(
file
));
}
else
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
file
),
charsetName
));
}
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(
curLine
>
end
)
break
;
if
(
start
<=
curLine
&&
curLine
<=
end
)
list
.
add
(
line
);
++
curLine
;
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
@@ -624,73 +629,44 @@ public class FileUtils {
return
list
;
}
public
static
StringBuilder
readFile
(
String
filePath
,
String
charsetName
)
{
File
file
=
new
File
(
filePath
);
if
(!
file
.
isFile
())
return
null
;
return
readFile
(
file
,
charsetName
);
/**
* 指定编码按行读取文件到StringBuilder中
*
* @param filePath 文件路径
* @param charsetName 编码格式
* @return StringBuilder对象
*/
public
static
StringBuilder
readFile2SB
(
String
filePath
,
String
charsetName
)
{
return
readFile2SB
(
getFileByPath
(
filePath
),
charsetName
);
}
public
static
StringBuilder
readFile
(
File
file
,
String
charsetName
)
{
StringBuilder
sb
=
new
StringBuilder
(
""
);
/**
* 指定编码按行读取文件到StringBuilder中
*
* @param file 文件
* @param charsetName 编码格式
* @return StringBuilder对象
*/
public
static
StringBuilder
readFile2SB
(
File
file
,
String
charsetName
)
{
if
(
file
==
null
)
return
null
;
StringBuilder
sb
=
null
;
BufferedReader
reader
=
null
;
try
{
InputStreamReader
is
=
new
InputStreamReader
(
new
FileInputStream
(
file
),
charsetName
);
reader
=
new
BufferedReader
(
is
);
sb
=
new
StringBuilder
(
);
reader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
file
),
charsetName
)
);
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(!
sb
.
toString
().
equals
(
""
))
{
sb
.
append
(
"\r\n"
);
}
sb
.
append
(
line
);
sb
.
append
(
"\r\n"
);
// windows系统换行为\r\n,Linux为\n
}
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
reader
!=
null
)
{
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
closeIO
(
reader
);
}
return
sb
;
}
/**
* @param filePath
* @param charsetName
* @return
*/
public
static
List
<
String
>
readFileToList
(
String
filePath
,
String
charsetName
)
{
File
file
=
new
File
(
filePath
);
if
(!
file
.
isFile
())
return
null
;
List
<
String
>
fileContent
=
new
ArrayList
<>();
BufferedReader
reader
=
null
;
try
{
InputStreamReader
is
=
new
InputStreamReader
(
new
FileInputStream
(
file
),
charsetName
);
reader
=
new
BufferedReader
(
is
);
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
fileContent
.
add
(
line
);
}
reader
.
close
();
return
fileContent
;
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"IOException occurred. "
,
e
);
}
finally
{
if
(
reader
!=
null
)
{
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* byte单位转换(单位:unit)
*
...
...
@@ -750,10 +726,10 @@ public class FileUtils {
}
/**
*
获取文件路径的父级
目录
*
根据全路径获取最长
目录
*
* @param filePath
* @return
* @param filePath
文件路径
* @return
filePath最长目录
*/
public
static
String
getDirName
(
String
filePath
)
{
if
(
StringUtils
.
isSpace
(
filePath
))
return
filePath
;
...
...
@@ -761,12 +737,24 @@ public class FileUtils {
return
lastSep
==
-
1
?
""
:
filePath
.
substring
(
0
,
lastSep
+
1
);
}
/**
* 根据全路径获取文件名
*
* @param filePath 文件路径
* @return 文件名
*/
public
static
String
getFileName
(
String
filePath
)
{
if
(
StringUtils
.
isSpace
(
filePath
))
return
filePath
;
int
lastSep
=
filePath
.
lastIndexOf
(
File
.
separator
);
return
lastSep
==
-
1
?
filePath
:
filePath
.
substring
(
lastSep
+
1
);
}
/**
* 根据全路径获取文件名不带拓展名
*
* @param filePath 文件路径
* @return 文件名不带拓展名
*/
public
static
String
getFileNameNoExtension
(
String
filePath
)
{
if
(
StringUtils
.
isSpace
(
filePath
))
return
filePath
;
int
lastPoi
=
filePath
.
lastIndexOf
(
'.'
);
...
...
@@ -780,6 +768,12 @@ public class FileUtils {
return
filePath
.
substring
(
lastSep
+
1
,
lastPoi
);
}
/**
* 根据全路径获取文件拓展名
*
* @param filePath 文件路径
* @return 文件拓展名
*/
public
static
String
getFileExtension
(
String
filePath
)
{
if
(
StringUtils
.
isSpace
(
filePath
))
return
filePath
;
int
lastPoi
=
filePath
.
lastIndexOf
(
'.'
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录