mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 13:54:03 +08:00
[update] 1.5.2-jre11
1. 升级相关依赖 2. 依赖工具读取文件FileInput从FileUtil中提取出来
This commit is contained in:
parent
601611a11f
commit
b6ffffef71
10
UPDATE.md
10
UPDATE.md
@ -1,6 +1,16 @@
|
|||||||
yexuejc-base 更新记录
|
yexuejc-base 更新记录
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
#### version :1.5.2-jre11
|
||||||
|
**time:2024-4-7 14:34:33** <br/>
|
||||||
|
**branch:** jre11 <br/>
|
||||||
|
**update:** <br/>
|
||||||
|
1. 升级相关依赖
|
||||||
|
2. 依赖工具读取文件[FileInput.java](src/main/java/com/yexuejc/base/file/FileInput.java)从[FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)中提取出来
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
#### version :1.5.1-jre11
|
#### version :1.5.1-jre11
|
||||||
**time:2023-6-8 16:02:56** <br/>
|
**time:2023-6-8 16:02:56** <br/>
|
||||||
**branch:** jre11 <br/>
|
**branch:** jre11 <br/>
|
||||||
|
31
pom.xml
31
pom.xml
@ -6,11 +6,11 @@
|
|||||||
|
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.5.1-jre11</version>
|
<version>1.5.2-jre11</version>
|
||||||
|
|
||||||
<name>yexuejc-base</name>
|
<name>yexuejc-base</name>
|
||||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||||
<description>Common toolkits based on JDK11 packaging</description>
|
<description>Common toolkits based on JDK11+ packaging</description>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
@ -42,16 +42,16 @@
|
|||||||
<repos.yexuejc.url>https://nexus.yexuejc.top/repository/</repos.yexuejc.url>
|
<repos.yexuejc.url>https://nexus.yexuejc.top/repository/</repos.yexuejc.url>
|
||||||
<repos.aliyun.url>https://maven.aliyun.com/repository/public</repos.aliyun.url>
|
<repos.aliyun.url>https://maven.aliyun.com/repository/public</repos.aliyun.url>
|
||||||
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
||||||
<jjwt.version>0.11.5</jjwt.version>
|
<jjwt.version>0.12.5</jjwt.version>
|
||||||
<maven.compiler.verbose>true</maven.compiler.verbose>
|
<maven.compiler.verbose>true</maven.compiler.verbose>
|
||||||
<java.version>11</java.version>
|
<java.version>11</java.version>
|
||||||
<validation-api.version>3.0.2</validation-api.version>
|
<validation-api.version>3.0.2</validation-api.version>
|
||||||
|
|
||||||
<commons-io.version>2.11.0</commons-io.version>
|
<commons-io.version>2.11.0</commons-io.version>
|
||||||
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
<bcprov-jdk18on.version>1.78</bcprov-jdk18on.version>
|
||||||
<guava.version>31.1-jre</guava.version>
|
<guava.version>33.1.0-jre</guava.version>
|
||||||
<apache-poi.version>5.2.2</apache-poi.version>
|
<apache-poi.version>5.2.5</apache-poi.version>
|
||||||
<jackson.version>2.14.2</jackson.version>
|
<jackson.version>2.17.0</jackson.version>
|
||||||
<zip4j.version>2.11.4</zip4j.version>
|
<zip4j.version>2.11.4</zip4j.version>
|
||||||
<!-- 文件拷贝时的编码 -->
|
<!-- 文件拷贝时的编码 -->
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@ -93,14 +93,15 @@
|
|||||||
<!--支持大量的密码术算法,并提供JCE 1.2.1的实现-->
|
<!--支持大量的密码术算法,并提供JCE 1.2.1的实现-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.bouncycastle</groupId>
|
||||||
<artifactId>bcprov-jdk15on</artifactId>
|
<artifactId>bcprov-jdk18on</artifactId>
|
||||||
<version>${bcprov-jdk15on.version}</version>
|
<version>${bcprov-jdk18on.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--com.yexuejc.base.util.SysUtil.threadRun 异步处理代码-->
|
<!--com.yexuejc.base.file.FileInput 读文件-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -112,6 +113,12 @@
|
|||||||
<groupId>org.apache.poi</groupId>
|
<groupId>org.apache.poi</groupId>
|
||||||
<artifactId>poi-ooxml</artifactId>
|
<artifactId>poi-ooxml</artifactId>
|
||||||
<version>${apache-poi.version}</version>
|
<version>${apache-poi.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-compress</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
@ -145,7 +152,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.13.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<source>11</source>
|
<source>11</source>
|
||||||
@ -156,7 +163,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
<version>3.2.1</version>
|
<version>3.3.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>attach-sources</id>
|
<id>attach-sources</id>
|
||||||
|
254
src/main/java/com/yexuejc/base/file/FileInput.java
Normal file
254
src/main/java/com/yexuejc/base/file/FileInput.java
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
package com.yexuejc.base.file;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提供读取IO流方法合集
|
||||||
|
* <p>读取速度快慢顺序:</p>
|
||||||
|
* 1. {@link #read4Buffer(InputStream, Charset)} <br>
|
||||||
|
* 2. {@link #read4IOUtilsCopy(InputStream, Charset)} <br>
|
||||||
|
* 3. {@link #read4ByteStreams(InputStream, Charset)} <br>
|
||||||
|
* 4. {@link #read4Byte(InputStream, Charset)} <br>
|
||||||
|
* 5. {@link #read4StringBuilder(InputStream, Charset)} <br>
|
||||||
|
* 6. {@link #read4BufferedReaderParallel(InputStream, Charset, String)}<br>
|
||||||
|
* 7. {@link #read4BufferedReader(InputStream, Charset, String)}<br>
|
||||||
|
* 8. {@link #read4ScannerA(InputStream)}<br>
|
||||||
|
* 9. {@link #read4BufferIO(InputStream, Charset)}<br>
|
||||||
|
* 10. {@link #read4IOUtils(InputStream, Charset)}<br>
|
||||||
|
* 11. {@link #read4ScannerZ(InputStream)}<br>
|
||||||
|
* 12. {@link #read4CharStreams(InputStream, Charset)}<br>
|
||||||
|
*/
|
||||||
|
public class FileInput {
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:byte方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String read4Byte(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
byte[] bytes = new byte[inputStream.available()];
|
||||||
|
inputStream.read(bytes);
|
||||||
|
return new String(bytes, charset == null ? Charset.defaultCharset() : charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:BufferedReader方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4BufferedReader(InputStream inputStream, Charset charset, String lineSeparator) {
|
||||||
|
return new BufferedReader(
|
||||||
|
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
|
||||||
|
).lines().collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:BufferedReader 并行方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4BufferedReaderParallel(InputStream inputStream, Charset charset, String lineSeparator) {
|
||||||
|
return new BufferedReader(
|
||||||
|
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
|
||||||
|
).lines().parallel()
|
||||||
|
.collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:Scanner A方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4ScannerA(InputStream inputStream) {
|
||||||
|
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
|
||||||
|
String str = s.hasNext() ? s.next() : "";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:Scanner Z方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4ScannerZ(InputStream inputStream) {
|
||||||
|
return new Scanner(inputStream).useDelimiter("\\Z").next();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:StringBuilder方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4StringBuilder(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
sb.append(line);
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:ByteArrayOutputStream方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4Buffer(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = inputStream.read(buffer)) != -1) {
|
||||||
|
result.write(buffer, 0, length);
|
||||||
|
}
|
||||||
|
return result.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容:BufferedInputStream+ByteArrayOutputStream方式
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String read4BufferIO(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(inputStream);
|
||||||
|
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||||
|
int result = bis.read();
|
||||||
|
while (result != -1) {
|
||||||
|
buf.write((byte) result);
|
||||||
|
result = bis.read();
|
||||||
|
}
|
||||||
|
return buf.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容: 依赖于commons-io:commons-io {@link IOUtils#copy(Reader, OutputStream, Charset)}
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String read4IOUtilsCopy(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
IOUtils.copy(inputStream, writer, charset == null ? Charset.defaultCharset() : charset);
|
||||||
|
return writer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容: 依赖于commons-io:commons-io {@link IOUtils#toString(InputStream, Charset)}
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String read4IOUtils(InputStream inputStream, Charset charset) throws IOException {
|
||||||
|
return IOUtils.toString(inputStream, charset == null ? Charset.defaultCharset() : charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容: 依赖于com.google.guava:guava {@link com.google.common.io.CharStreams#toString(Readable)}
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String read4CharStreams(InputStream inputStream, Charset charset) throws IOException, ClassNotFoundException {
|
||||||
|
try {
|
||||||
|
Class<?> charStreamsClass = Class.forName("com.google.common.io.CharStreams");
|
||||||
|
Method toStringMethod = charStreamsClass.getMethod("toString", InputStreamReader.class);
|
||||||
|
return (String) toStringMethod.invoke(null, new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new ClassNotFoundException("缺少依赖,请引入Guava");
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new RuntimeException("com.google.common.io.CharStreams.toString调用失败,请检查Guava版本", e);
|
||||||
|
}
|
||||||
|
// return com.google.common.io.CharStreams.toString(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取IO流内容: 依赖于com.google.guava:guava {@link com.google.common.io.ByteStreams#toByteArray(InputStream)}
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String read4ByteStreams(InputStream inputStream, Charset charset) throws IOException, ClassNotFoundException {
|
||||||
|
try {
|
||||||
|
Class<?> charStreamsClass = Class.forName("com.google.common.io.ByteStreams");
|
||||||
|
Method toStringMethod = charStreamsClass.getMethod("toByteArray", InputStreamReader.class);
|
||||||
|
return (String) toStringMethod.invoke(null, new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new ClassNotFoundException("缺少依赖,请引入Guava");
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new RuntimeException("com.google.common.io.ByteStreams.toByteArray调用失败,请检查Guava版本", e);
|
||||||
|
}
|
||||||
|
// return new String(com.google.common.io.ByteStreams.toByteArray(inputStream), charset == null ? Charset.defaultCharset() : charset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*public static void main(String[] args) {
|
||||||
|
long size = FileUtil.size(new File("E:\\OS\\deepin-15.6-amd64\\DeepinCloudPrintServerInstaller_1.0.0.1.exe"));
|
||||||
|
System.out.println(size);
|
||||||
|
System.out.println(1024 * 1024 * 5);
|
||||||
|
if (size > 1024 * 1024 * 5) {
|
||||||
|
System.out.println("文件最大5M");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long s1 = fileSize(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
||||||
|
System.out.println(s1);
|
||||||
|
long s2 = fileSize4Stream(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
||||||
|
System.out.println(s2);
|
||||||
|
|
||||||
|
String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
System.out.println(s1);
|
||||||
|
|
||||||
|
String s = sha1(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
String s2 = sha1ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
System.out.println(s);
|
||||||
|
System.out.println(s2);
|
||||||
|
|
||||||
|
|
||||||
|
String md5 = md5(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
String md52 = md5ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
System.out.println(md5);
|
||||||
|
System.out.println(md52);
|
||||||
|
|
||||||
|
|
||||||
|
String crc32 = crc32(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
|
System.out.println(crc32);
|
||||||
|
}*/
|
@ -1,7 +1,5 @@
|
|||||||
package com.yexuejc.base.pojo;
|
package com.yexuejc.base.pojo;
|
||||||
|
|
||||||
import org.checkerframework.checker.units.qual.C;
|
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -1,29 +1,11 @@
|
|||||||
package com.yexuejc.base.util;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.MappingIterator;
|
|
||||||
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
|
||||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
|
||||||
import com.google.common.io.ByteStreams;
|
|
||||||
import com.google.common.io.CharStreams;
|
|
||||||
import com.yexuejc.base.annotation.CsvToBean;
|
|
||||||
import com.yexuejc.base.pojo.ReadFileBean;
|
|
||||||
import io.jsonwebtoken.lang.Assert;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.MappedByteBuffer;
|
import java.nio.MappedByteBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
@ -35,11 +17,16 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.MappingIterator;
|
||||||
|
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||||
|
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||||
|
import com.yexuejc.base.annotation.CsvToBean;
|
||||||
|
import com.yexuejc.base.pojo.ReadFileBean;
|
||||||
|
import io.jsonwebtoken.lang.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件工具类
|
* 文件工具类
|
||||||
*
|
*
|
||||||
@ -507,7 +494,8 @@ public class FileUtil {
|
|||||||
while ((line = randomAccessFile.readLine()) != null && row <= readFileBean.getReadRowNum()) {
|
while ((line = randomAccessFile.readLine()) != null && row <= readFileBean.getReadRowNum()) {
|
||||||
row++;
|
row++;
|
||||||
readFileBean.setPointer(randomAccessFile.getFilePointer());
|
readFileBean.setPointer(randomAccessFile.getFilePointer());
|
||||||
datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset()))); }
|
datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset())));
|
||||||
|
}
|
||||||
|
|
||||||
if (StrUtil.isEmpty(datas)) {
|
if (StrUtil.isEmpty(datas)) {
|
||||||
//无数据
|
//无数据
|
||||||
@ -572,223 +560,4 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
return new String(result, charset);
|
return new String(result, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 提供读取IO流方法合集
|
|
||||||
* <p>读取速度快慢顺序:</p>
|
|
||||||
* 1. {@link #read4Buffer(InputStream, Charset)} <br>
|
|
||||||
* 2. {@link #read4IOUtilsCopy(InputStream, Charset)} <br>
|
|
||||||
* 3. {@link #read4ByteStreams(InputStream, Charset)} <br>
|
|
||||||
* 4. {@link #read4Byte(InputStream, Charset)} <br>
|
|
||||||
* 5. {@link #read4StringBuilder(InputStream, Charset)} <br>
|
|
||||||
* 6. {@link #read4BufferedReaderParallel(InputStream, Charset, String)}<br>
|
|
||||||
* 7. {@link #read4BufferedReader(InputStream, Charset, String)}<br>
|
|
||||||
* 8. {@link #read4ScannerA(InputStream)}<br>
|
|
||||||
* 9. {@link #read4BufferIO(InputStream, Charset)}<br>
|
|
||||||
* 10. {@link #read4IOUtils(InputStream, Charset)}<br>
|
|
||||||
* 11. {@link #read4ScannerZ(InputStream)}<br>
|
|
||||||
* 12. {@link #read4CharStreams(InputStream, Charset)}<br>
|
|
||||||
*/
|
|
||||||
public static class FileInput {
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:byte方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String read4Byte(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
byte[] bytes = new byte[inputStream.available()];
|
|
||||||
inputStream.read(bytes);
|
|
||||||
return new String(bytes, charset == null ? Charset.defaultCharset() : charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:BufferedReader方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4BufferedReader(InputStream inputStream, Charset charset, String lineSeparator) {
|
|
||||||
return new BufferedReader(
|
|
||||||
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
|
|
||||||
).lines().collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:BufferedReader 并行方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4BufferedReaderParallel(InputStream inputStream, Charset charset, String lineSeparator) {
|
|
||||||
return new BufferedReader(
|
|
||||||
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
|
|
||||||
).lines().parallel()
|
|
||||||
.collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:Scanner A方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4ScannerA(InputStream inputStream) {
|
|
||||||
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
|
|
||||||
String str = s.hasNext() ? s.next() : "";
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:Scanner Z方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4ScannerZ(InputStream inputStream) {
|
|
||||||
return new Scanner(inputStream).useDelimiter("\\Z").next();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:StringBuilder方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4StringBuilder(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
|
||||||
while ((line = br.readLine()) != null) {
|
|
||||||
sb.append(line);
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:ByteArrayOutputStream方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4Buffer(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int length;
|
|
||||||
while ((length = inputStream.read(buffer)) != -1) {
|
|
||||||
result.write(buffer, 0, length);
|
|
||||||
}
|
|
||||||
return result.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容:BufferedInputStream+ByteArrayOutputStream方式
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String read4BufferIO(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
BufferedInputStream bis = new BufferedInputStream(inputStream);
|
|
||||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
||||||
int result = bis.read();
|
|
||||||
while (result != -1) {
|
|
||||||
buf.write((byte) result);
|
|
||||||
result = bis.read();
|
|
||||||
}
|
|
||||||
return buf.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容: 依赖于commons-io:commons-io {@link IOUtils#copy(Reader, OutputStream, Charset)}
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String read4IOUtilsCopy(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
IOUtils.copy(inputStream, writer, charset == null ? Charset.defaultCharset() : charset);
|
|
||||||
return writer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容: 依赖于commons-io:commons-io {@link IOUtils#toString(InputStream, Charset)}
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String read4IOUtils(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
return IOUtils.toString(inputStream, charset == null ? Charset.defaultCharset() : charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容: 依赖于com.google.guava:guava {@link CharStreams#toString(Readable)}
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String read4CharStreams(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
return CharStreams.toString(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取IO流内容: 依赖于com.google.guava:guava {@link ByteStreams#toByteArray(InputStream)}
|
|
||||||
*
|
|
||||||
* @param inputStream
|
|
||||||
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String read4ByteStreams(InputStream inputStream, Charset charset) throws IOException {
|
|
||||||
return new String(ByteStreams.toByteArray(inputStream), charset == null ? Charset.defaultCharset() : charset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*public static void main(String[] args) {
|
|
||||||
long size = FileUtil.size(new File("E:\\OS\\deepin-15.6-amd64\\DeepinCloudPrintServerInstaller_1.0.0.1.exe"));
|
|
||||||
System.out.println(size);
|
|
||||||
System.out.println(1024 * 1024 * 5);
|
|
||||||
if (size > 1024 * 1024 * 5) {
|
|
||||||
System.out.println("文件最大5M");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
long s1 = fileSize(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
|
||||||
System.out.println(s1);
|
|
||||||
long s2 = fileSize4Stream(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
|
||||||
System.out.println(s2);
|
|
||||||
|
|
||||||
String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
System.out.println(s1);
|
|
||||||
|
|
||||||
String s = sha1(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
String s2 = sha1ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
System.out.println(s);
|
|
||||||
System.out.println(s2);
|
|
||||||
|
|
||||||
|
|
||||||
String md5 = md5(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
String md52 = md5ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
System.out.println(md5);
|
|
||||||
System.out.println(md52);
|
|
||||||
|
|
||||||
|
|
||||||
String crc32 = crc32(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
|
||||||
System.out.println(crc32);
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user