2012-04-06 15 views
0

イメージファイルの最後に別のファイルが追加され、文字列区切り文字で区切られています。私がやろうとしているのは、Javaの2つのファイルを別々のファイルに書き込んで、それを自分のファイルに追加することです。しかし、いくつかのソリューションを試しましたが、ファイルが破損してしまったり、誰かが正しい方向に私を向けることができますか?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; 

    } 
+0

することができますどのようにあなたがそれをやろうとしているかを示すコードを投稿してください。 – casablanca

+1

文字列が追加されたときに有効な画像ファイルであると思われますか? –

+0

これまでのコードを追加しました。 – Yawn

答えて

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

あなたは(ちょうどString.indexOf実装に見てみましょう)findIndexOf(byte[] where, byte[] what, int startIndex)を実装する必要があります。私はそれが助けて欲しい

+0

「スキャナ」を使わないのはなぜですか? – Jon

+1

はイメージファイルであり、文字列ではないためです。あなたが文字列として扱うときに壊れてしまいます。 –

+0

本当ですか?私はこのような画像を扱うことを覚えていますが、それはPHPだったかもしれません。あなたは一度に画像全体を読むことができるので、私はそれが壊れないと確信しています。 – Jon

1

ScanneruseDelimiter()メソッドでこれを行います。基本的には:

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 
+0

バイト配列に読み込む方法はありますか?文字列に入れるとデータが破損する傾向があります。 – Yawn

+0

場合によってはうまくいく可能性があります。しかし、他の場合には、イメージ部分から 'デリミタ 'を読むことができます(バイナリであり、' delimiter'バイナリ表現に相当するバイナリを含むかもしれません)。 –

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

出力:
次のファイル:
は ファイル1
次のファイル:
秒ファイル
次のファイル:
ファイル番号3

関連する問題