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,34 +22,38 @@ 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}
|
||||||
*/
|
*/
|
||||||
@ -60,6 +64,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
|
*
|
||||||
* @param file 文件
|
* @param file 文件
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
||||||
*/
|
*/
|
||||||
@ -69,6 +74,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
|
*
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
||||||
* @param charset 编码,使用 {@link CharsetUtil}
|
* @param charset 编码,使用 {@link CharsetUtil}
|
||||||
*/
|
*/
|
||||||
@ -78,6 +84,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
|
*
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
||||||
*/
|
*/
|
||||||
@ -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) {
|
||||||
@ -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;
|
||||||
@ -197,7 +206,7 @@ public class FileWriter extends FileWrapper{
|
|||||||
* @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);
|
||||||
@ -235,10 +244,10 @@ public class FileWriter extends FileWrapper{
|
|||||||
* @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()));
|
||||||
@ -287,15 +296,11 @@ public class FileWriter extends FileWrapper{
|
|||||||
* @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;
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -384,22 +389,23 @@ public class FileWriter extends FileWrapper{
|
|||||||
*/
|
*/
|
||||||
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