| | |
| | | package org.springblade.mdm.utils; |
| | | |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.junit.jupiter.api.Assertions; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.junit.jupiter.api.io.TempDir; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.Charsets; |
| | | |
| | |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.List; |
| | | |
| | | public class FileContentUtilTest { |
| | | |
| | |
| | | |
| | | @Test |
| | | public void testReadLineAt() throws IOException { |
| | | String file = "D:/a.txt"; |
| | | |
| | | Path source = Paths.get("src/test/resources/filecontenttest.txt"); |
| | | int lineIndex = 1; |
| | | FileInputStream fis = new FileInputStream(file); |
| | | FileInputStream fis = new FileInputStream(source.toFile()); |
| | | |
| | | String expected = "第二行"; |
| | | String read = FileContentUtil.readLineAt(fis,lineIndex); |
| | | Assertions.assertEquals(expected,read); |
| | | |
| | | } |
| | | |
| | | @Test |
| | | public void testReplaceReadLineAt(@TempDir Path tempDir) throws IOException { |
| | | // 加载资源文件 |
| | | Path source = Paths.get("src/test/resources/filecontenttest.txt"); |
| | | //File file = new File(classLoader.getResource("filecontenttest.txt").getFile()); |
| | | File testFile = source.toFile(); |
| | | try(FileInputStream fis = new FileInputStream(testFile);) { |
| | | |
| | | String expected = "newline2"; |
| | | int lineIndex = 1; |
| | | InputStream ins = FileContentUtil.replaceAtLine(fis, lineIndex, expected); |
| | | List<String> list = IOUtils.readLines(ins, Charsets.UTF_8); |
| | | System.out.println(list); |
| | | Assertions.assertEquals(expected, list.get(lineIndex)); |
| | | } |
| | | |
| | | } |
| | | } |