yangys
2025-08-04 96481362fed4eab7b96cc9016ece1917b43bbcc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.springblade.mdm.utils;
 
import org.junit.jupiter.api.Test;
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);
    }
}