I am doing a test with a simple code, but this one is giving error. I want to simply get the contents of a binary file which is an image (jpg), and save it again with another name.
My code is this:
String content = null;
FileInputStream in = new FileInputStream(new File("C:\farol.jpg"));
StringBuilder builder = new StringBuilder();
int ch;
while ((ch = in.read()) != -1) {
builder.append((char) ch);
}
in.close();
content = builder.toString();
BufferedWriter out = new BufferedWriter(new FileWriter("C:\farolNOVO.jpg"));
out.write(content);
out.close();
The image "farolNOVO.jpg" is being created, with the correct size and the same size as the original, but it looks strange.
Theoriginalimageisthis:
Hasanyoneeverhadthisproblem?
NOTE1:Thiscodeisworkingfor.txtfilesNOTE2:Iamusingjava1.6(Icannotupdateitforcompatibilityissueswithotherthingsaroundhere)
Takingothertestshere,Ifoundonethatworkedinparts:
StringFILE_ORINNAL="C:\farol.jpg";
String FILE_NOVO = "C:\faro_novo.jpg";
InputStream inputStream = new FileInputStream(FILE_ORINNAL);
OutputStream outputStream = new FileOutputStream(FILE_NOVO);
int byteRead;
while ((byteRead = inputStream.read()) != -1) {
outputStream.write(byteRead);
}
The problem is that this is reading and passing straight to the outputStream. I wanted to make a "getContentFileBinary" method that returns the String of the binary statement. Then I want to do another "getContentFileBinaryBase64" method
This is my ultimate goal.