我有一个图像文件,其末尾附加了另一个文件,用字符串分隔符分隔。我想要做的是将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;
}
答案 0 :(得分: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
实现)。我希望它有所帮助。
答案 1 :(得分: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
答案 2 :(得分:0)
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号文件