mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
ede848ed07
commit
04b95fcb88
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user