Java还原图片

现在已有程序能够把一个jpg格式的图片文件,根据字节随机拆分成3个文件,其拆分过程记录在splitlog.txt文件中,splitlog.txt文件格式如下,其内容分为两列,中间用 隔开,第一列按照顺序代表接下来的内容来自哪个文件,第二列代表连续取多少个字节(已知不超过255字节):

0   59
1  47
2  26
0  34
1  80
2  95
0  107
1  52
2  198
0  180
1  120
2  179
0  169
1  47
2  80
0  132
1  83
2  39
0  195
1  151
2  63
0  56
1  79
2  187
0  194
1  181
2  152
0  180
1  57
2  145
0  24
1  68
2  8
0  137
1  76
2  97
0  109
1  176
2  112
0  110
1  173
2  63
0  126
1  68
2  66
0  90
1  47
2  20
0  187
1  150
2  84
0  15
1  30
2  128
0  37
1  3
2  116
0  171
1  75
2  191
0  122
1  127
2  13
0  96
1  140
2  30
0  187
1  194
2  168
0  79
1  116
2  162
0  153
1  177
2  9
0  190
1  44
2  149
0  184
1  196
2  116
0  47
1  117
2  147
0  109
1  34
2  18
0  88
1  85
2  120
0  19
1  120
2  179
0  170
1  39
2  20
0  107
1  18
2  97
0  113
1  154
2  159
0  19
1  119
2  16
0  161
1  150
2  51
0  57
1  28
2  196
0  56
1  16
2  100
0  183
1  80
2  91
0  193
1  160
2  111
0  187
1  136
2  138
0  40
1  185
2  31
0  120
1  79
2  88
0  47
1  123
2  27
0  129
1  45
2  34
0  68
1  116
2  35
0  119
1  176
2  61
0  26
1  120
2  37
0  79
1  19
2  35
0  98
1  119
2  75
0  120
1  24
2  139
0  76
1  194
2  196
0  118
1  19
2  143
0  42
1  95
2  58
0  198
1  190
2  153
0  162
1  158
2  60
0  54
1  99
2  175
0  178
1  104
2  15
0  57
1  158
2  170
0  121
1  59
2  66
0  39
1  50
2  65
0  183
1  176
2  168
0  184
1  198
2  184
0  4
1  121
2  81
0  49

例如: 如上表示,先从1文件取20个字节,然后从3文件取15个字节,然后再从1文件取8个字节,然后再从2文件取4个字节,以此类推。
请完成代码还原图片,提交代码及还原后的图片

import java.io.*;
import java.util.ArrayList;

public class ReviewImage {
    private static int P1 = 0;
    private static int P2 = 0;
    private static int P3 = 0;

    public static void main(String[] args) throws IOException {
        ArrayList list = new ArrayList();
        String line = null;
        File file = new File("splitlog.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader br = new BufferedReader(fileReader);
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        FileOutputStream fileOutputStream = new FileOutputStream("a.jpg", true);
        for (int i = 0; i < list.size(); i++) {
            String[] arr = list.get(i).toString().split("\t");
            byte[] bytes = new byte[Integer.parseInt(arr[1])];
            switch (arr[0]) {
                case "0":
                    FileInputStream p1 = new FileInputStream("p1");
                    p1.skip(P1);
                    p1.read(bytes);
                    P1 = P1 + bytes.length;
                    fileOutputStream.write(bytes);
                    break;
                case "1":
                    FileInputStream p2 = new FileInputStream("p2");
                    p2.skip(P2);
                    p2.read(bytes);
                    P2 = P2 + bytes.length;
                    fileOutputStream.write(bytes);
                    break;
                case "2":
                    FileInputStream p3 = new FileInputStream("p3");
                    p3.skip(P3);
                    p3.read(bytes);
                    P3 = bytes.length + P3;
                    fileOutputStream.write(bytes);
                    break;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Yajyaj123/article/details/128837819
今日推荐