Skip to content

Commit

Permalink
add write in file
Browse files Browse the repository at this point in the history
  • Loading branch information
叶云轩.联想 committed Jun 25, 2019
1 parent 232c9eb commit c52324c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/main/java/org/yyx/xf/tool/document/file/util/UtilFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.yyx.xf.tool.document.file.domain.exception.FileException;
import org.yyx.xf.tool.date.util.UtilDate;
import org.yyx.xf.tool.document.file.domain.exception.FileException;
import org.yyx.xf.tool.string.util.UtilString;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -229,4 +230,32 @@ public static String getUniqueFileName(String originalFilename, boolean keepFile
.append(UtilDate.javaUtilDateToString(new Date(), "yyyyMMddHHmmSSS"));
return sb.append(fileNameSuffix).toString();
}

/**
* 向文件中写内容
*
* @param content 内容
* @param filePath 文件路径
* @return 文件对象
*/
public static File writeInFile(String content, String filePath) {
FileWriter fwriter = null;
try {
// true表示不覆盖原来的内容,而是加到文件的后面。若要覆盖原来的内容,直接省略这个参数就好
fwriter = new FileWriter(filePath, true);
fwriter.write(content);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (fwriter != null) {
fwriter.flush();
fwriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return new File(filePath);
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/yyx/xf/util/maven/UtilMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class UtilMavenTest {

@Test
public void clean() {
UtilMaven.clean("/Users/xuan/.m2/repository");
UtilMaven.clean("C:\\Users\\WangYuannian\\.m2\\repository");
}
}

0 comments on commit c52324c

Please sign in to comment.