提交 98bf498d 编写于 作者: 如梦技术's avatar 如梦技术 🐛

代码优化

上级 2da376e7
......@@ -29,6 +29,7 @@ import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
......@@ -299,8 +300,8 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
* @return the file contents, never {@code null}
*/
public static String readToString(final File file, final Charset encoding) {
try (InputStream in = Files.newInputStream(file.toPath())) {
return IoUtil.readToString(in, encoding);
try {
return Files.readString(file.toPath(), encoding);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
......@@ -314,8 +315,8 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
* @return the file contents, never {@code null}
*/
public static byte[] readToByteArray(final File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
return IoUtil.readToByteArray(in);
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
......@@ -364,8 +365,12 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
* end of the file rather than overwriting
*/
public static void writeToFile(final File file, final String data, final Charset encoding, final boolean append) {
try (OutputStream out = new FileOutputStream(file, append)) {
IoUtil.write(data, out, encoding);
try {
if (append) {
Files.writeString(file.toPath(), data, encoding, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} else {
Files.writeString(file.toPath(), data, encoding, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
}
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
......
......@@ -47,7 +47,7 @@ public class PathUtil {
}
@Nullable
public static String toFilePath(@Nullable URL url) {
private static String toFilePath(@Nullable URL url) {
if (url == null) { return null; }
String protocol = url.getProtocol();
String file = UrlUtil.decode(url.getPath(), Charsets.UTF_8);
......
......@@ -52,7 +52,7 @@ public class RuntimeUtil {
pId = NumberUtil.toInt(jvmName.substring(0, index), -1);
return pId;
}
return -1;
return pId;
}
/**
......
......@@ -128,6 +128,7 @@ public class ThreadLocalUtil {
* @see this#get(String)
* @see this#remove(String)
*/
@Nullable
public static <T> T getAndRemove(String key) {
try {
return get(key);
......
......@@ -4,6 +4,8 @@ import net.dreamlu.mica.core.utils.FileUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
/**
* 文件测试
*
......@@ -21,4 +23,21 @@ public class FileTest {
String pathWithoutExtension = FileUtil.getPathWithoutExtension(fileName);
Assert.assertEquals("123123123.xx", pathWithoutExtension);
}
@Test
public void test2() {
String data = "123哈哈";
String testFilePath = FileUtil.getTempDirPath() + File.separator + "1.txt";
File testFile = new File(testFilePath);
FileUtil.writeToFile(testFile, data, false);
String read1 = FileUtil.readToString(testFile);
Assert.assertEquals(data, read1);
String read2 = FileUtil.readToString(testFile);
Assert.assertEquals(data, read2);
FileUtil.writeToFile(testFile, data, true);
String read3 = FileUtil.readToString(testFile);
Assert.assertEquals(data + data, read3);
testFile.deleteOnExit();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册