add FileUtils
This commit is contained in:
parent
e1bee91780
commit
f5271b058b
8
pom.xml
8
pom.xml
|
@ -8,6 +8,14 @@
|
|||
<artifactId>CodeSnippet</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.github.aborn.tools;
|
||||
|
||||
import com.github.aborn.utils.FileUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -35,12 +37,14 @@ public class StringTools {
|
|||
|
||||
/**
|
||||
* 对邮件里的内容获取
|
||||
* TODO 从文件中读取内容
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String inputValue = "zhangsan <zhangsan@qq.com>; lisi <lisi@hotmail.com>; ";
|
||||
String fileName = "/Users/aborn/temp/t.txt";
|
||||
String fileContent = FileUtils.read(fileName);
|
||||
System.out.println(getEmailNameInfo(inputValue));
|
||||
System.out.println("\n");
|
||||
System.out.println(getEmailNameInfo(fileContent));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.github.aborn.utils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author aborn
|
||||
* @date 2021/06/30 11:43 AM
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String file = "/Users/aborn/temp/t.txt";
|
||||
System.out.println(read(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file content as string,
|
||||
* null if file doesn't exist or read exception
|
||||
*
|
||||
* @param fileName 文件名全路径
|
||||
* @return file content
|
||||
*/
|
||||
public static String read(String fileName) {
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File file = new File(fileName);
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 再从文件里读
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
// 临时存储 bitSet的 array信息
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
// 缓冲区大小
|
||||
int bufferSize = 1024;
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
int n;
|
||||
// 读取到buffer缓冲区,缓冲区大小为bufferSize
|
||||
while ((n = inputStream.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, n);
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue