1
|
-package main.java.com;
|
1
|
+package com.xkl;
|
2
|
|
2
|
|
3
|
|
3
|
|
4
|
import java.io.BufferedReader;
|
4
|
import java.io.BufferedReader;
|
|
@@ -23,7 +23,7 @@ public class FileUtil { |
|
@@ -23,7 +23,7 @@ public class FileUtil { |
23
|
* @param filePath
|
23
|
* @param filePath
|
24
|
*/
|
24
|
*/
|
25
|
public static Set<String> readTxtFile(String filePath) {
|
25
|
public static Set<String> readTxtFile(String filePath) {
|
26
|
- Set<String> resSet = new HashSet<>();
|
26
|
+ Set<String> resSet = new HashSet<String>();
|
27
|
try {
|
27
|
try {
|
28
|
String encoding = "GBK";
|
28
|
String encoding = "GBK";
|
29
|
File file = new File(filePath);
|
29
|
File file = new File(filePath);
|
|
@@ -44,16 +44,39 @@ public class FileUtil { |
|
@@ -44,16 +44,39 @@ public class FileUtil { |
44
|
e.printStackTrace();
|
44
|
e.printStackTrace();
|
45
|
}
|
45
|
}
|
46
|
return resSet;
|
46
|
return resSet;
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ public static String readLine(String filePath) {
|
|
|
50
|
+ Set<String> resSet = new HashSet<String>();
|
|
|
51
|
+ try {
|
|
|
52
|
+ String encoding = "GBK";
|
|
|
53
|
+ File file = new File(filePath);
|
|
|
54
|
+ if (file.isFile() && file.exists()) { //判断文件是否存在
|
|
|
55
|
+ InputStreamReader read = new InputStreamReader(
|
|
|
56
|
+ new FileInputStream(file), encoding);//考虑到编码格式
|
|
|
57
|
+ BufferedReader bufferedReader = new BufferedReader(read);
|
|
|
58
|
+ String lineTxt = null;
|
|
|
59
|
+ while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
|
60
|
+ if (!lineTxt.trim().toLowerCase().equals("")) {
|
|
|
61
|
+ return lineTxt.trim();
|
|
|
62
|
+ }
|
47
|
|
63
|
|
|
|
64
|
+ }
|
|
|
65
|
+ read.close();
|
|
|
66
|
+ } else {
|
|
|
67
|
+ System.out.println("找不到指定的文件");
|
|
|
68
|
+ }
|
|
|
69
|
+ } catch (Exception e) {
|
|
|
70
|
+ System.out.println("读取文件内容出错");
|
|
|
71
|
+ e.printStackTrace();
|
|
|
72
|
+ }
|
|
|
73
|
+ return "";
|
48
|
}
|
74
|
}
|
49
|
|
75
|
|
50
|
public static void main(String argv[]) {
|
76
|
public static void main(String argv[]) {
|
51
|
String filePath = "C:\\Users\\zhaoyue\\Desktop\\ids";
|
77
|
String filePath = "C:\\Users\\zhaoyue\\Desktop\\ids";
|
52
|
// "res/";
|
78
|
// "res/";
|
53
|
- Set<String> resSet = readTxtFile(filePath);
|
|
|
54
|
- for (String line : resSet) {
|
|
|
55
|
- System.out.println(line);
|
|
|
56
|
- }
|
79
|
+ System.out.println(readLine(filePath));
|
57
|
}
|
80
|
}
|
58
|
|
81
|
|
59
|
|
82
|
|