This commit is contained in:
Looly 2024-09-29 17:00:03 +08:00
parent ede848ed07
commit 04b95fcb88
5 changed files with 32 additions and 45 deletions

View File

@ -2152,28 +2152,24 @@ public class FileUtil extends PathUtil {
/**
* 获得一个输出流对象
*
* @param file 文件
* @param file 文件
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return 输出流对象
* @throws IORuntimeException IO异常
*/
public static BufferedOutputStream getOutputStream(final File file) throws IORuntimeException {
final OutputStream out;
try {
out = Files.newOutputStream(touch(file).toPath());
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return IoUtil.toBuffered(out);
public static BufferedOutputStream getOutputStream(final File file, final OpenOption... options) throws IORuntimeException {
return PathUtil.getOutputStream(touch(file).toPath(), options);
}
/**
* 获得一个输出流对象
*
* @param path 输出到的文件路径绝对路径
* @param path 输出到的文件路径绝对路径
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return 输出流对象
* @throws IORuntimeException IO异常
*/
public static BufferedOutputStream getOutputStream(final String path) throws IORuntimeException {
public static BufferedOutputStream getOutputStream(final String path, final OpenOption... options) throws IORuntimeException {
return getOutputStream(touch(path));
}

View File

@ -22,17 +22,9 @@ import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.util.Map;
import java.util.Map.Entry;
@ -216,13 +208,13 @@ public class FileWriter extends FileWrapper {
boolean isFirst = true;
for (final T t : list) {
if (null != t) {
if(isFirst){
if (isFirst) {
isFirst = false;
if(isAppend && FileUtil.isNotEmpty(this.file)){
if (isAppend && FileUtil.isNotEmpty(this.file)) {
// 追加模式下且文件非空补充换行符
printNewLine(writer, lineSeparator);
}
} else{
} else {
printNewLine(writer, lineSeparator);
}
writer.print(t);
@ -338,17 +330,16 @@ public class FileWriter extends FileWrapper {
*
* @param in 输入流不关闭
* @param isCloseIn 是否关闭输入流
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return file
* @throws IORuntimeException IO异常
* @since 5.5.2
*/
public File writeFromStream(final InputStream in, final boolean isCloseIn) throws IORuntimeException {
public File writeFromStream(final InputStream in, final boolean isCloseIn, final OpenOption... options) throws IORuntimeException {
OutputStream out = null;
try {
out = Files.newOutputStream(FileUtil.touch(file).toPath());
out = FileUtil.getOutputStream(file, options);
IoUtil.copy(in, out);
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.closeQuietly(out);
if (isCloseIn) {
@ -361,15 +352,12 @@ public class FileWriter extends FileWrapper {
/**
* 获得一个输出流对象
*
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return 输出流对象
* @throws IORuntimeException IO异常
*/
public BufferedOutputStream getOutputStream() throws IORuntimeException {
try {
return new BufferedOutputStream(Files.newOutputStream(FileUtil.touch(file).toPath()));
} catch (final IOException e) {
throw new IORuntimeException(e);
}
public BufferedOutputStream getOutputStream(final OpenOption... options) throws IORuntimeException {
return FileUtil.getOutputStream(file, options);
}
/**

View File

@ -94,6 +94,7 @@ public class PathUtil {
}
// region ----- loop and walk
/**
* 递归遍历目录以及子目录中的所有文件<br>
* 如果提供path为文件直接返回过滤结果
@ -232,6 +233,7 @@ public class PathUtil {
}
// region ----- copy
/**
* 拷贝资源到目标文件
* <ul>
@ -286,7 +288,7 @@ public class PathUtil {
/**
* 通过JDK7+ {@link Files#copy(InputStream, Path, CopyOption...)} 方法拷贝文件
*
* @param src 源文件流使用后不闭流
* @param src 源文件流使用后不闭流
* @param out 目标流
* @return 拷贝bytes数
* @throws IORuntimeException IO异常
@ -539,15 +541,15 @@ public class PathUtil {
/**
* 获得输出流
*
* @param path Path
* @param path Path
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return 输入流
* @throws IORuntimeException 文件未找到
* @since 5.4.1
*/
public static BufferedOutputStream getOutputStream(final Path path) throws IORuntimeException {
public static BufferedOutputStream getOutputStream(final Path path, final OpenOption... options) throws IORuntimeException {
final OutputStream in;
try {
in = Files.newOutputStream(path);
in = Files.newOutputStream(path, options);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@ -631,7 +633,7 @@ public class PathUtil {
*/
public static boolean equals(final Path file1, final Path file2) throws IORuntimeException {
// 都为null认定为相同
if(null == file1 || null == file2){
if (null == file1 || null == file2) {
return null == file1 && null == file2;
}
@ -639,7 +641,7 @@ public class PathUtil {
final boolean exists2 = exists(file2, false);
if(exists1 && exists2){
if (exists1 && exists2) {
return isSameFile(file1, file2);
}

View File

@ -20,6 +20,7 @@ import org.dromara.hutool.core.compress.ZipReader;
import org.dromara.hutool.core.compress.ZipUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.io.file.PathUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
@ -31,7 +32,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@ -173,7 +173,7 @@ public class ZipUtilTest {
//https://github.com/dromara/hutool/issues/944
final String dir = "d:/test";
final String zip = "d:/test.zip";
try (final OutputStream out = Files.newOutputStream(Paths.get(zip))){
try (final OutputStream out = PathUtil.getOutputStream(Paths.get(zip))){
//实际应用中, out HttpServletResponse.getOutputStream
ZipUtil.zip(out, Charset.defaultCharset(), false, null, new File(dir));
} catch (final IOException e) {

View File

@ -16,6 +16,7 @@
package org.dromara.hutool.crypto.symmetric;
import org.dromara.hutool.core.io.IORuntimeException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -50,7 +51,7 @@ public class Sm4StreamTest {
sm4.encrypt(input, out, IS_CLOSE);
System.out.println("============encrypt end");
} catch (final IOException e) {
e.printStackTrace();
throw new IORuntimeException(e);
}
}
@ -60,7 +61,7 @@ public class Sm4StreamTest {
sm4.decrypt(input, out, IS_CLOSE);
System.out.println("============decrypt end");
} catch (final IOException e) {
e.printStackTrace();
throw new IORuntimeException(e);
}
}
}