0

我有一个图像文件,其末尾附加了另一个文件,由字符串分隔符分隔。我想要做的是将java中的两个文件分开,将附加到末尾的文件写入它自己的文件中,我尝试了一些解决方案,但是它们要么损坏了文件,要么效率低下。有人可以指出我正确的方向吗?

这是迄今为止我最好的解决方案,它几乎可以工作,但会稍微损坏文件。

public class FileExtractor {

    private static final String START_OF_FILE_DATA = "SOFD34qjknhwe3rjkhw";

    public void extractFile(String[] files)
    {
        try 
        {
            String first = readFileToString(files[0]);
            Pattern p1 = Pattern.compile(START_OF_FILE_DATA + "(.*)" + START_OF_FILE_DATA + "(.*)", Pattern.DOTALL);
            Matcher matcher1 = p1.matcher(first);
            String filename = "";
            if(matcher1.find())
            {
                filename = matcher1.group(1);
            }
            else
            {
                //throw exception of corrupted file
            }
            FileOutputStream out = new FileOutputStream(new File("buildtest/" + filename));
            out.write(matcher1.group(2).getBytes("cp1251"), 0, matcher1.group(2).length());
            for (int i = 1; i < files.length; i++) 
            {
                String content = readFileToString(files[i]);
                Pattern p = Pattern.compile(START_OF_FILE_DATA + "(.*)", Pattern.DOTALL);
                Matcher matcher = p.matcher(content);
                if(matcher.find())
                {
                    out.write(matcher.group(1).getBytes("cp1251"), 0, matcher.group(1).length());
                }
                else
                {
                    //throw exception of corrupted file
                }
            }
            out.close();
        } 
        catch (IOException e) 
        {
            System.out.println(e.getMessage());
        }
    }

    private String readFileToString(String file)
    {
        byte[] buffer = new byte[(int) new File(file).length()];
        BufferedInputStream f = null;
        try {
            f = new BufferedInputStream(new FileInputStream(file));
            f.read(buffer);
        } 
        catch (Exception e)
        {

        }
        finally 
        {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException ignored) {
                }
            }
        }
        String ret = "";
        try
        {
            ret = new String(buffer, "cp1251");
        }
        catch(Exception e)
        {

        }
        return ret;

    }
4

3 回答 3

1

Scanner用方法做到这一点useDelimiter()。基本上:

Scanner in = new Scanner(new File(your_file_name));
in.useDelimiter(START_OF_FILE_DATA);

String first = in.next();   // Read the first part
String seconds = in.next(); // Read the second part

// Save the separate files
于 2012-04-06T03:27:24.720 回答
1

我建议将文件作为字节数组而不是字符串来操作。所以你需要找到字节序列从哪里开始。

byte[] fileData = // read the file into a byte array
byte[] separator = separatorString.getBytes();
int index = 0;
for (;;) {
    int start = index;
    index = findIndexOf(fileData, separator, start);
    if (index == -1) break;
    byte[] nextImage = new byte[index - start + 1];
    System.arrayCopy(fileData, start, nextImage, 0, nextImage.length);
    saveAsImage(nextImage);
    index += separator.length;
}

当然你需要实现findIndexOf(byte[] where, byte[] what, int startIndex)(看看String.indexOf实现)。我希望它有所帮助。

于 2012-04-06T03:25:22.517 回答
1
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.common.io.Files;

    public class FileExtractor {

    private static final int START_OF_FILE_DATA = 0x1C;
    private static final String TEST_FILE_NAME = "test.txt";

    public static void main(String[] args) throws IOException {//test
        String separator = String.valueOf((char) START_OF_FILE_DATA);
        String bigFile = "file one" + separator + "second file" + separator + "file No. 3";
        Files.write(bigFile.getBytes(), new File(TEST_FILE_NAME));//create big file in project directory

        new FileExtractor().extractFile(TEST_FILE_NAME);
    }

    public void extractFile(String bigFile) {
        try (FileInputStream fis = new FileInputStream(bigFile);) {

            List<byte[]> files = new ArrayList<byte[]>();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int in;
            while ((in = fis.read()) != -1) {//read 1 byte from file until the file ends
                if (in == START_OF_FILE_DATA) {//START_OF_FILE_DATA have length 1 byte. For longer you need to remake it.
                    files.add(baos.toByteArray());
                    baos.reset();
                }
                baos.write(in);//beware, START_OF_FILE_DATA will be included in the file
            }

            files.add(baos.toByteArray());

            for (byte[] file : files)
                System.out.println("next file:\n" + new String(file));

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

输出:
下一个文件:
文件一个
下一个文件:
第二个文件
下一个文件:
文件号 3

于 2016-09-09T16:05:51.900 回答