Bilal Ahmed
Bilal Ahmed

Reputation: 37

Reading bytes from a file

I am reading from a ".264" file using code below.

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

}

Opening input and output file in HEX Editor show me some missing values, e.g. 0d (see pictures attached).

Any solution to fix this?

image1 image2

Upvotes: 0

Views: 146

Answers (1)

Siguza
Siguza

Reputation: 23830

Lose InputStreamReader and BufferedReader, just use FileInputStream on its own.
No character encoding, no line endings, just bytes.
Its Javadoc page is here, that should be all you need.

Tip if you want to read the entire file at once: File.length(), as in

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

Upvotes: 1

Related Questions