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
7a25b73376
commit
a0f407865e
@ -22,35 +22,39 @@ import java.util.Map.Entry;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件写入器
|
* 文件写入器
|
||||||
* @author Looly
|
|
||||||
*
|
*
|
||||||
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class FileWriter extends FileWrapper{
|
public class FileWriter extends FileWrapper {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 FileWriter
|
* 创建 FileWriter
|
||||||
* @param file 文件
|
*
|
||||||
|
* @param file 文件
|
||||||
* @param charset 编码,使用 {@link CharsetUtil}
|
* @param charset 编码,使用 {@link CharsetUtil}
|
||||||
* @return FileWriter
|
* @return FileWriter
|
||||||
*/
|
*/
|
||||||
public static FileWriter create(File file, Charset charset){
|
public static FileWriter create(File file, Charset charset) {
|
||||||
return new FileWriter(file, charset);
|
return new FileWriter(file, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 FileWriter, 编码:{@link FileWrapper#DEFAULT_CHARSET}
|
* 创建 FileWriter, 编码:{@link FileWrapper#DEFAULT_CHARSET}
|
||||||
|
*
|
||||||
* @param file 文件
|
* @param file 文件
|
||||||
* @return FileWriter
|
* @return FileWriter
|
||||||
*/
|
*/
|
||||||
public static FileWriter create(File file){
|
public static FileWriter create(File file) {
|
||||||
return new FileWriter(file);
|
return new FileWriter(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------- Constructor start
|
// ------------------------------------------------------- Constructor start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
* @param file 文件
|
*
|
||||||
|
* @param file 文件
|
||||||
* @param charset 编码,使用 {@link CharsetUtil}
|
* @param charset 编码,使用 {@link CharsetUtil}
|
||||||
*/
|
*/
|
||||||
public FileWriter(File file, Charset charset) {
|
public FileWriter(File file, Charset charset) {
|
||||||
@ -60,7 +64,8 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
* @param file 文件
|
*
|
||||||
|
* @param file 文件
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
||||||
*/
|
*/
|
||||||
public FileWriter(File file, String charset) {
|
public FileWriter(File file, String charset) {
|
||||||
@ -69,8 +74,9 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
|
*
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
||||||
* @param charset 编码,使用 {@link CharsetUtil}
|
* @param charset 编码,使用 {@link CharsetUtil}
|
||||||
*/
|
*/
|
||||||
public FileWriter(String filePath, Charset charset) {
|
public FileWriter(String filePath, Charset charset) {
|
||||||
this(FileUtil.file(filePath), charset);
|
this(FileUtil.file(filePath), charset);
|
||||||
@ -78,8 +84,9 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
|
*
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
||||||
*/
|
*/
|
||||||
public FileWriter(String filePath, String charset) {
|
public FileWriter(String filePath, String charset) {
|
||||||
this(FileUtil.file(filePath), CharsetUtil.charset(charset));
|
this(FileUtil.file(filePath), CharsetUtil.charset(charset));
|
||||||
@ -88,6 +95,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 构造<br>
|
* 构造<br>
|
||||||
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
||||||
|
*
|
||||||
* @param file 文件
|
* @param file 文件
|
||||||
*/
|
*/
|
||||||
public FileWriter(File file) {
|
public FileWriter(File file) {
|
||||||
@ -97,6 +105,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 构造<br>
|
* 构造<br>
|
||||||
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
||||||
|
*
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
||||||
*/
|
*/
|
||||||
public FileWriter(String filePath) {
|
public FileWriter(String filePath) {
|
||||||
@ -107,7 +116,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将String写入文件
|
* 将String写入文件
|
||||||
*
|
*
|
||||||
* @param content 写入的内容
|
* @param content 写入的内容
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
@ -118,9 +127,9 @@ public class FileWriter extends FileWrapper{
|
|||||||
writer = getWriter(isAppend);
|
writer = getWriter(isAppend);
|
||||||
writer.write(content);
|
writer.write(content);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
}catch(IOException e){
|
} catch (IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}finally {
|
} finally {
|
||||||
IoUtil.close(writer);
|
IoUtil.close(writer);
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
@ -151,7 +160,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将列表写入文件,覆盖模式
|
* 将列表写入文件,覆盖模式
|
||||||
*
|
*
|
||||||
* @param <T> 集合元素类型
|
* @param <T> 集合元素类型
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
@ -163,7 +172,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将列表写入文件,追加模式
|
* 将列表写入文件,追加模式
|
||||||
*
|
*
|
||||||
* @param <T> 集合元素类型
|
* @param <T> 集合元素类型
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
@ -175,8 +184,8 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将列表写入文件
|
* 将列表写入文件
|
||||||
*
|
*
|
||||||
* @param <T> 集合元素类型
|
* @param <T> 集合元素类型
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
@ -188,16 +197,16 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将列表写入文件
|
* 将列表写入文件
|
||||||
*
|
*
|
||||||
* @param <T> 集合元素类型
|
* @param <T> 集合元素类型
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @param lineSeparator 换行符枚举(Windows、Mac或Linux换行符)
|
* @param lineSeparator 换行符枚举(Windows、Mac或Linux换行符)
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
* @since 3.1.0
|
* @since 3.1.0
|
||||||
*/
|
*/
|
||||||
public <T> File writeLines(Collection<T> list, LineSeparator lineSeparator, boolean isAppend) throws IORuntimeException {
|
public <T> File writeLines(Collection<T> list, LineSeparator lineSeparator, boolean isAppend) throws IORuntimeException {
|
||||||
try (PrintWriter writer = getPrintWriter(isAppend)){
|
try (PrintWriter writer = getPrintWriter(isAppend)) {
|
||||||
for (T t : list) {
|
for (T t : list) {
|
||||||
if (null != t) {
|
if (null != t) {
|
||||||
writer.print(t);
|
writer.print(t);
|
||||||
@ -212,9 +221,9 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
|
* 将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
|
||||||
*
|
*
|
||||||
* @param map Map
|
* @param map Map
|
||||||
* @param kvSeparator 键和值之间的分隔符,如果传入null使用默认分隔符" = "
|
* @param kvSeparator 键和值之间的分隔符,如果传入null使用默认分隔符" = "
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
* @since 4.0.5
|
* @since 4.0.5
|
||||||
@ -226,19 +235,19 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
|
* 将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
|
||||||
*
|
*
|
||||||
* @param map Map
|
* @param map Map
|
||||||
* @param lineSeparator 换行符枚举(Windows、Mac或Linux换行符)
|
* @param lineSeparator 换行符枚举(Windows、Mac或Linux换行符)
|
||||||
* @param kvSeparator 键和值之间的分隔符,如果传入null使用默认分隔符" = "
|
* @param kvSeparator 键和值之间的分隔符,如果传入null使用默认分隔符" = "
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
* @since 4.0.5
|
* @since 4.0.5
|
||||||
*/
|
*/
|
||||||
public File writeMap(Map<?, ?> map, LineSeparator lineSeparator, String kvSeparator, boolean isAppend) throws IORuntimeException {
|
public File writeMap(Map<?, ?> map, LineSeparator lineSeparator, String kvSeparator, boolean isAppend) throws IORuntimeException {
|
||||||
if(null == kvSeparator) {
|
if (null == kvSeparator) {
|
||||||
kvSeparator = " = ";
|
kvSeparator = " = ";
|
||||||
}
|
}
|
||||||
try(PrintWriter writer = getPrintWriter(isAppend)) {
|
try (PrintWriter writer = getPrintWriter(isAppend)) {
|
||||||
for (Entry<?, ?> entry : map.entrySet()) {
|
for (Entry<?, ?> entry : map.entrySet()) {
|
||||||
if (null != entry) {
|
if (null != entry) {
|
||||||
writer.print(StrUtil.format("{}{}{}", entry.getKey(), kvSeparator, entry.getValue()));
|
writer.print(StrUtil.format("{}{}{}", entry.getKey(), kvSeparator, entry.getValue()));
|
||||||
@ -254,8 +263,8 @@ public class FileWriter extends FileWrapper{
|
|||||||
* 写入数据到文件
|
* 写入数据到文件
|
||||||
*
|
*
|
||||||
* @param data 数据
|
* @param data 数据
|
||||||
* @param off 数据开始位置
|
* @param off 数据开始位置
|
||||||
* @param len 数据长度
|
* @param len 数据长度
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
@ -267,8 +276,8 @@ public class FileWriter extends FileWrapper{
|
|||||||
* 追加数据到文件
|
* 追加数据到文件
|
||||||
*
|
*
|
||||||
* @param data 数据
|
* @param data 数据
|
||||||
* @param off 数据开始位置
|
* @param off 数据开始位置
|
||||||
* @param len 数据长度
|
* @param len 数据长度
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
@ -279,23 +288,19 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 写入数据到文件
|
* 写入数据到文件
|
||||||
*
|
*
|
||||||
* @param data 数据
|
* @param data 数据
|
||||||
* @param off 数据开始位置
|
* @param off 数据开始位置
|
||||||
* @param len 数据长度
|
* @param len 数据长度
|
||||||
* @param isAppend 是否追加模式
|
* @param isAppend 是否追加模式
|
||||||
* @return 目标文件
|
* @return 目标文件
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public File write(byte[] data, int off, int len, boolean isAppend) throws IORuntimeException {
|
public File write(byte[] data, int off, int len, boolean isAppend) throws IORuntimeException {
|
||||||
FileOutputStream out = null;
|
try (FileOutputStream out = new FileOutputStream(FileUtil.touch(file), isAppend)) {
|
||||||
try {
|
|
||||||
out = new FileOutputStream(FileUtil.touch(file), isAppend);
|
|
||||||
out.write(data, off, len);
|
out.write(data, off, len);
|
||||||
out.flush();
|
out.flush();
|
||||||
}catch(IOException e){
|
} catch (IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
} finally {
|
|
||||||
IoUtil.close(out);
|
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@ -315,7 +320,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 将流的内容写入文件
|
* 将流的内容写入文件
|
||||||
*
|
*
|
||||||
* @param in 输入流,不关闭
|
* @param in 输入流,不关闭
|
||||||
* @param isCloseIn 是否关闭输入流
|
* @param isCloseIn 是否关闭输入流
|
||||||
* @return dest
|
* @return dest
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
@ -326,11 +331,11 @@ public class FileWriter extends FileWrapper{
|
|||||||
try {
|
try {
|
||||||
out = new FileOutputStream(FileUtil.touch(file));
|
out = new FileOutputStream(FileUtil.touch(file));
|
||||||
IoUtil.copy(in, out);
|
IoUtil.copy(in, out);
|
||||||
}catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
IoUtil.close(out);
|
IoUtil.close(out);
|
||||||
if(isCloseIn){
|
if (isCloseIn) {
|
||||||
IoUtil.close(in);
|
IoUtil.close(in);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -371,7 +376,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
*
|
*
|
||||||
* @param isAppend 是否追加
|
* @param isAppend 是否追加
|
||||||
* @return 打印对象
|
* @return 打印对象
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public PrintWriter getPrintWriter(boolean isAppend) throws IORuntimeException {
|
public PrintWriter getPrintWriter(boolean isAppend) throws IORuntimeException {
|
||||||
return new PrintWriter(getWriter(isAppend));
|
return new PrintWriter(getWriter(isAppend));
|
||||||
@ -380,26 +385,27 @@ public class FileWriter extends FileWrapper{
|
|||||||
/**
|
/**
|
||||||
* 检查文件
|
* 检查文件
|
||||||
*
|
*
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
private void checkFile() throws IORuntimeException {
|
private void checkFile() throws IORuntimeException {
|
||||||
Assert.notNull(file, "File to write content is null !");
|
Assert.notNull(file, "File to write content is null !");
|
||||||
if(this.file.exists() && false == file.isFile()){
|
if (this.file.exists() && false == file.isFile()) {
|
||||||
throw new IORuntimeException("File [{}] is not a file !", this.file.getAbsoluteFile());
|
throw new IORuntimeException("File [{}] is not a file !", this.file.getAbsoluteFile());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印新行
|
* 打印新行
|
||||||
* @param writer Writer
|
*
|
||||||
|
* @param writer Writer
|
||||||
* @param lineSeparator 换行符枚举
|
* @param lineSeparator 换行符枚举
|
||||||
* @since 4.0.5
|
* @since 4.0.5
|
||||||
*/
|
*/
|
||||||
private void printNewLine(PrintWriter writer, LineSeparator lineSeparator) {
|
private void printNewLine(PrintWriter writer, LineSeparator lineSeparator) {
|
||||||
if(null == lineSeparator) {
|
if (null == lineSeparator) {
|
||||||
//默认换行符
|
//默认换行符
|
||||||
writer.println();
|
writer.println();
|
||||||
}else {
|
} else {
|
||||||
//自定义换行符
|
//自定义换行符
|
||||||
writer.print(lineSeparator.getValue());
|
writer.print(lineSeparator.getValue());
|
||||||
}
|
}
|
||||||
|
@ -231,9 +231,6 @@ public class UploadFile {
|
|||||||
} finally {
|
} finally {
|
||||||
IoUtil.close(out);
|
IoUtil.close(out);
|
||||||
}
|
}
|
||||||
// if (getFileName().length() == 0 && size == 0) {
|
|
||||||
// size = -1;
|
|
||||||
// }
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user