Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fangyeqing
/
xkl-interface-example-all
·
Commits
Go to a project
GitLab
Go to dashboard
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
zhaoyue
8 years ago
Commit
e7ff0078c8795d4192710ded56ad9d7caf04d0d3
1 parent
b5040eac
Can read local report
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
src/main/java/com/xkl/FileUtil.java
src/main/java/com/xkl/FileUtil.java
0 → 100644
View file @
e7ff007
package
main
.
java
.
com
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.InputStreamReader
;
import
java.util.HashSet
;
import
java.util.Set
;
/**
* Created by zhaoyue on 2017/3/7.
*/
public
class
FileUtil
{
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
*
* @param filePath
*/
public
static
Set
<
String
>
readTxtFile
(
String
filePath
)
{
Set
<
String
>
resSet
=
new
HashSet
<>();
try
{
String
encoding
=
"GBK"
;
File
file
=
new
File
(
filePath
);
if
(
file
.
isFile
()
&&
file
.
exists
())
{
//判断文件是否存在
InputStreamReader
read
=
new
InputStreamReader
(
new
FileInputStream
(
file
),
encoding
);
//考虑到编码格式
BufferedReader
bufferedReader
=
new
BufferedReader
(
read
);
String
lineTxt
=
null
;
while
((
lineTxt
=
bufferedReader
.
readLine
())
!=
null
)
{
resSet
.
add
(
lineTxt
.
toLowerCase
().
trim
());
}
read
.
close
();
}
else
{
System
.
out
.
println
(
"找不到指定的文件"
);
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"读取文件内容出错"
);
e
.
printStackTrace
();
}
return
resSet
;
}
public
static
void
main
(
String
argv
[])
{
String
filePath
=
"C:\\Users\\zhaoyue\\Desktop\\ids"
;
// "res/";
Set
<
String
>
resSet
=
readTxtFile
(
filePath
);
for
(
String
line
:
resSet
)
{
System
.
out
.
println
(
line
);
}
}
}
...
...
Please
register
or
login
to post a comment