SequenceInputStream的奇怪行为

时间:2012-10-10 11:51:39

标签: encoding inputstream sequence

我尝试创建一个UTF-16编码文件的SequenceStream。

public class sequenceStream {


public static void main(String[] args) {        
    try {

        File f1 = new File("C:\\Temp\\sequence1.txt");
        File f2=  new File("C:\\Temp\\sequence2.txt");
        File f3=  new File("C:\\Temp\\sequence3.txt");
        InputStream is = getSequencedInputStream(f1,f2,f3);
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-16"));

        String line;
        int count=0;
        while((line = br.readLine()) !=null){
            count++;
            System.out.println(count + " : " + line);
        }
        br.close();

    } catch (FileNotFoundException e) {
    } catch (IOException e) {

    }


}


private static InputStream getSequencedInputStream(File ... files)  {
    Vector v = new Vector(files.length);
    for (int i=0; i<files.length;i++){
        if(files[i].exists()){
            try {
                v.add(new FileInputStream(files[i]));
            } catch (FileNotFoundException e) {     
            }
        }


    }
    return new SequenceInputStream(v.elements());
}

}

如果我运行这个,我得到了这个输出:

1 : first file, first line
2 : first file, second line?second file, first line
3 : second file, second line?third file, first line
4 : third file, second line

如果我运行它,只有一个文件它可以正常工作:

InputStream is = getSequencedInputStream(f1);

输出:

1 : first file, first line
2 : first file, second line

问号来自哪里?我怎么处理这个?

1 个答案:

答案 0 :(得分:0)

我怀疑问题出在你的文件终止上。例如,在最后一行之后是否有换行符?