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

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

View File

@ -94,6 +94,7 @@ public class PathUtil {
} }
// region ----- loop and walk // region ----- loop and walk
/** /**
* 递归遍历目录以及子目录中的所有文件<br> * 递归遍历目录以及子目录中的所有文件<br>
* 如果提供path为文件直接返回过滤结果 * 如果提供path为文件直接返回过滤结果
@ -232,6 +233,7 @@ public class PathUtil {
} }
// region ----- copy // region ----- copy
/** /**
* 拷贝资源到目标文件 * 拷贝资源到目标文件
* <ul> * <ul>
@ -540,14 +542,14 @@ public class PathUtil {
* 获得输出流 * 获得输出流
* *
* @param path Path * @param path Path
* @param options 选项如追加模式传{@link java.nio.file.StandardOpenOption#APPEND}
* @return 输入流 * @return 输入流
* @throws IORuntimeException 文件未找到 * @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; final OutputStream in;
try { try {
in = Files.newOutputStream(path); in = Files.newOutputStream(path, options);
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
@ -631,7 +633,7 @@ public class PathUtil {
*/ */
public static boolean equals(final Path file1, final Path file2) throws IORuntimeException { public static boolean equals(final Path file1, final Path file2) throws IORuntimeException {
// 都为null认定为相同 // 都为null认定为相同
if(null == file1 || null == file2){ if (null == file1 || null == file2) {
return null == file1 && null == file2; return null == file1 && null == file2;
} }
@ -639,7 +641,7 @@ public class PathUtil {
final boolean exists2 = exists(file2, false); final boolean exists2 = exists(file2, false);
if(exists1 && exists2){ if (exists1 && exists2) {
return isSameFile(file1, file2); 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.compress.ZipUtil;
import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.file.FileUtil; 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.lang.Console;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
@ -31,7 +32,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -173,7 +173,7 @@ public class ZipUtilTest {
//https://github.com/dromara/hutool/issues/944 //https://github.com/dromara/hutool/issues/944
final String dir = "d:/test"; final String dir = "d:/test";
final String zip = "d:/test.zip"; 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 //实际应用中, out HttpServletResponse.getOutputStream
ZipUtil.zip(out, Charset.defaultCharset(), false, null, new File(dir)); ZipUtil.zip(out, Charset.defaultCharset(), false, null, new File(dir));
} catch (final IOException e) { } catch (final IOException e) {

View File

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