从文件中读取字节

时间:2015-04-25 07:48:53

标签: java fileinputstream inputstreamreader

我正在使用以下代码阅读“.264”文件。

public static void main (String[] args) throws IOException
{

    BufferedReader br = null;try {
        String sCurrentLine;
        br = new BufferedReader(new InputStreamReader(new FileInputStream("test.264"),"ISO-8859-1"));
        StringBuffer stringBuffer = new StringBuffer();
        while ((sCurrentLine = br.readLine()) != null) {
            stringBuffer.append(sCurrentLine);      
        }
        String tempdec = new String(asciiToHex(stringBuffer.toString()));
        System.out.println(tempdec);
        String asciiEquivalent = hexToASCII(tempdec);
        BufferedWriter xx = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator/Desktop/yuvplayer-2.3/video dinalized/testret.264"),"ISO-8859-1"));
        xx.write(asciiEquivalent);
        xx.close();
    }catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

在HEX编辑器中打开输入和输出文件会显示一些缺失值,例如: 0d(见附图)。

解决此问题的任何解决方案?

image1 image2

1 个答案:

答案 0 :(得分:1)

失去InputStreamReaderBufferedReader,只需自己使用FileInputStream 没有字符编码,没有行结尾,只有字节 它的Javadoc页面是here,应该就是你所需要的。

提示您是否要一次阅读整个文件:File.length(),如

File file = new File("test.264");
byte[] buf = new byte[(int)file.length()];
// Use buf in InputStream.read()