Obtaining a binary file and saving it again

0

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.

    
asked by anonymous 02.12.2017 / 13:06

3 answers

0

My problem was when I created the file. After I used "ISO-8859-1" it worked.

outputStream.write(content.getBytes("ISO-8859-1")); 

This "ISO-8859-1" solved my problem!

    
04.12.2017 / 11:15
1

Probably the reading is missing something, you can try to read the content using this:

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

...

Path p = FileSystems.getDefault().getPath("", "C:\farol.jpg");
byte [] fileData = Files.readAllBytes(p);

And then burn:

import java.io.FileOutputStream;

...

FileOutputStream stream = new FileOutputStream("C:\farolNOVO.jpg");
try {
    stream.write(fileData);
} finally {
    stream.close();
}

I tested with this:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.FileOutputStream;

public class Foo
{
    public static void main(String args[]) throws IOException
    {
        Path caminho = Paths.get("c:\farol.jpg");

        byte [] fileData = Files.readAllBytes(caminho);

        FileOutputStream stream = new FileOutputStream("c:\farolNOVO.jpg");
        try {
            stream.write(fileData);
        } finally {
            stream.close();
        }
    }
}

Copying files

However I find something quite redundant having to read the contents and copy the file, since it will be an indenting copy, if this is really the goal, just copy it and use File.copy .

  • static long copy(InputStream in, Path target, CopyOption... options)

    Copies the bytes of an input stream to a file

  • static long copy(Path source, OutputStream out)

    Copies the bytes of a file to an outputstream.

  • static Path copy(Path source, Path target, CopyOption... options)

    Copy a file to another file

Use example to replace if file already exists in folder:

import java.nio.file.StandardCopyOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;

...

Path caminho = Paths.get("c:\farol.jpg");
Path destrino = Paths.get("c:\farolNOVO.jpg");

Files.copy(caminho, destrino, StandardCopyOption.REPLACE_EXISTING);
    
02.12.2017 / 13:44
0

Java 6 or less

In earlier or equal versions of Java 6 you can do this:

File original = new File("c:\farol.jpg");
if(!original.exists()) {
    throw new IOException("O arquivo especificado não existe!");    
}

File novo = new File("c:\farolNOVO.jpg");
if(novo.exists()) {
    throw new IOException("Já existe um arquivo com esse nome");
 }

original.renameTo(novo);

Java 7

In equal or greater versions of Java 7 you can do this:

File original = new File("c:\farol.jpg");
if(!original.exists()) {
    throw new IOException("O arquivo especificado não existe!");    
}

File novo = new File("c:\farolNOVO.jpg");
if(novo.exists()) {
    throw new IOException("Já existe um arquivo com esse nome");
 }

Files.move(original.toPath(), 
        novo.toPath(), 
        StandardCopyOption.REPLACE_EXISTING);
    
02.12.2017 / 13:48