package org.springblade.mdm.utils;
|
|
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Test;
|
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;
|
|
public class FileContentUtilTest {
|
|
//@Test
|
public void testInsert() throws IOException {
|
int lineNo= 0;
|
ByteArrayInputStream byteStream;
|
ByteArrayInputStream streamForCharset;
|
try (InputStream ins = new FileInputStream("D:/a.txt");){
|
byte[] allBytes = ins.readAllBytes();
|
byteStream = new ByteArrayInputStream(allBytes);
|
streamForCharset = new ByteArrayInputStream(allBytes);
|
}
|
String charsetName = FileContentUtil.detectFromInputStream(streamForCharset);
|
Charset charset = Charsets.charset(charsetName);//new String("test文本".getBytes(charset))
|
InputStream newStream = FileContentUtil.insertLine(byteStream,1,"tes中间行t");
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(newStream, charset));) {
|
String line;
|
//读取
|
while((line = reader.readLine()) != null){
|
lineNo++;
|
System.out.println("line "+lineNo+":"+line);
|
}
|
} catch (IOException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
//@Test
|
public void testDelete() throws IOException {
|
String charsetName = FileContentUtil.detectFromInputStream(new FileInputStream("D:/a.txt"));
|
System.out.println(charsetName);
|
|
Charset charset = Charsets.charset(charsetName);
|
//System.out.println(charset);
|
}
|
|
//@Test
|
public void testReadLineAt() throws IOException {
|
String file = "D:/a.txt";
|
int lineIndex = 1;
|
FileInputStream fis = new FileInputStream(file);
|
|
String expected = "第二行";
|
String read = FileContentUtil.readLineAt(fis,lineIndex);
|
Assertions.assertEquals(expected,read);
|
|
}
|
}
|