简体   繁体   中英

How to read a binary file, modify the same file and save the file with the modifications?

I am currently working a project and we have divided it in modules, in one of them, we have a file ( .exe ) extension. I decided to open it in binary format and read the contents of it, modify them. But, I am not to modify the changes and save it in the same file. When I am trying to do so, it says 0KB. It's working perfectly fine when using two files.

Here is the source code :

public static void main(String[] args) {

    String strSourceFile="E:/e.exe";
    String strDestinationFile="E:/f.exe";
    try
    {
            FileInputStream fin = new FileInputStream(strSourceFile);

            FileOutputStream fout = new FileOutputStream(strDestinationFile);

            byte[] b = new byte[1];
            int noOfBytes = 0;

            System.out.println("Copying file using streams");

            while( (noOfBytes = fin.read(b)) != -1 )
            {   
                    fout.write(b, 0, noOfBytes);
            }
            System.out.println("File copied!");

            //close the streams
            fin.close();
            fout.close(); 

Use RandomAccessFile or You can also create a new file with your changes save it and delete the original one. Once original file is deleted then rename this new file to the original one.

You are trying to read and write the same file with the input and the output stream so the file is not getting copied while you try to do it with the same file. Instead, use a middle file as the buffer, as in the below code I have used the f.exe as the middle buffer, next I have copied the data from the buffer file again to the original file jar.exe, at last you need to delete the buffer file.

Here is the below code :

        String strSourceFile = "C:/jar.exe";
        String strDestinationFile = "C:/f.exe";

        FileInputStream fin = new FileInputStream(strSourceFile);

        FileOutputStream fout = new FileOutputStream(strDestinationFile);

        byte[] b = new byte[1];
        int noOfBytes = 0;

        System.out.println("Copying file using streams");

        while ((noOfBytes = fin.read(b)) != -1) {
            fout.write(b, 0, noOfBytes);
        }

        fin.close();
        fout.close();
        String strDestinationFile1 = "C:/jar.exe";
        FileInputStream fin1 = new FileInputStream(strDestinationFile);
        FileOutputStream fout1 = new FileOutputStream(strDestinationFile1);

        while ((noOfBytes = fin1.read(b)) != -1) {
            fout1.write(b, 0, noOfBytes);
        }
        System.out.println("File copied!");

        //close the streams
        fin1.close();
        fout1.close();

        File file = new File("C:/f.exe");
        file.delete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM