0

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • You are reading a binary file in a text mode. In the latter, lines are separate by `0d` characters (CR). – mins Apr 25 '15 at 07:57
  • How to read it properly then? – Bilal Ahmed Apr 25 '15 at 08:03
  • 2
    The short answer is *you open it as a byte stream* and there is an example here (it stores the bytes in memory, you need to write them in a file): [File to byte\[\] in Java](http://stackoverflow.com/questions/858980/file-to-byte-in-java). But beyond that you should clarify your need, other solutions may be better optimized. – mins Apr 25 '15 at 08:25
  • Don't post picture of text. Post the text. Waste of your time and our bandwidth. – user207421 Apr 25 '15 at 09:34
  • The easiest way to read all the bytes from a file is with `java.nio.file.Files.readAllBytes`. Also here is an advice for posting screenshots (if you really have to) - Select a window and press alt-print to capture a screenshot of only that window. Then paste that into paint and save it as png (not jpg) and afterwards send it through tinypng.com. That way your images will be very small. The screenshots you posted would probably be around 40 kb in size plus have better quality than your jpgs. But don't try to convert a jpg to png. That would result in a huge image instead. – Tesseract Apr 25 '15 at 10:05

1 Answers1

1

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()
Siguza
  • 21,155
  • 6
  • 52
  • 89