1

我试图创建一个 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

问号从哪里来?我该如何处理?

4

1 回答 1

0

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

于 2013-11-27T17:02:28.617 回答