Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
tartarisfully

Using filereader and writer to copy jpg files

if you want to do something with the image, you might want to actually open it as an image instead of as text.

Talking from experience, you are more than likely to have a corrupt image as a result since most image formats have redundancies and checks to make sure it's ok.

 

If you are still set on reading as a text file, there's a way to read the file as a stream of bytes, so you wont have the problem you are currently having (which is the charset)

Recommended Posts

Posted · Original PosterOP

I'm trying to copy some files with the following code:

import java.io.FileReader;
import java.io.FileWriter;

public class File {
    public static void main(String[] args) {
        System.out.println("file location:");
        String location = new java.util.Scanner(System.in).nextLine();
        System.out.println("file destination:");
        String name = new java.util.Scanner(System.in).nextLine();
        try {
            FileReader fr = new FileReader(location);
            FileWriter fw = new FileWriter(name, true);
            int m = fr.read();
            while (m != -1) {
                fw.write(m);
                fw.flush();
                m = fr.read();
            }
            fr.close();
            fw.close();
            System.out.println("done");
        } catch (java.io.IOException e) {
            System.out.println("file not found");
        }
    }
}

Using txt files works fine, but jpg files get messed up during the process.

Is there any way to get this to work? 

Link to post
Share on other sites
16 minutes ago, tartarisfully said:

I'm trying to copy some files with the following code:

-snip-

Using txt files works fine, but jpg files get messed up during the process.

Is there any way to get this to work? 

Why are you trying to manually read it in and write it out again... O.o

Path source = Paths.get("somefile.test");
Path destination = Paths.get("somefile.test"); 
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);

 


The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites
Posted · Best Answer

if you want to do something with the image, you might want to actually open it as an image instead of as text.

Talking from experience, you are more than likely to have a corrupt image as a result since most image formats have redundancies and checks to make sure it's ok.

 

If you are still set on reading as a text file, there's a way to read the file as a stream of bytes, so you wont have the problem you are currently having (which is the charset)


The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×