0

文字列区切り記号で区切られた別のファイルが最後に追加された画像ファイルがあります。私がやろうとしているのは、Javaで2つのファイルを分離して、最後に追加されたファイルを独自のファイルに書き込むことです。いくつかの解決策を試しましたが、ファイルが破損するか、絶望的に非効率的でした. 誰かが私を正しい方向に向けてもらえますか?

これは私がこれまでに持っている最良の解決策です。ほとんど機能しますが、ファイルがわずかに破損します。

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
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());
        }
    }
}

出力:
次のファイル:
ファイル 1
次のファイル:
2 番目のファイル
次のファイル:
ファイル番号 3

于 2016-09-09T16:05:51.900 に答える
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 に答える