yangys
2025-08-30 8a7d30b24624acb234ea3fa877134e0116a5be01
blade-service/blade-mdm/src/test/java/org/springblade/mdm/utils/FileContentUtilTest.java
@@ -1,12 +1,20 @@
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.io.*;
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 {
@@ -43,4 +51,35 @@
      Charset charset = Charsets.charset(charsetName);
      //System.out.println(charset);
   }
   @Test
   public void testReadLineAt() throws IOException {
      Path source = Paths.get("src/test/resources/filecontenttest.txt");
      int lineIndex = 1;
      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));
      }
   }
}