Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:50:19+00:00 2026-06-13T16:50:19+00:00

I have program in which I have to load a PNG as a String

  • 0

I have program in which I have to load a PNG as a String and then save it again, but after I save it it becomes unreadable. If I open both the loaded PNG and the saved String in the editor, I can see that java created linebreaks all over the file. If this is is the problem, how can I avoid this?

    public static void main(String[] args)
    {
        try
        {
            File file1 = new File("C://andim//testFile.png");
            StringBuffer content = new StringBuffer();
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(file1));
            String s = null;         
            while ((s = reader.readLine()) != null) 
            {
                content.append(s).append(System.getProperty("line.separator"));                      
            }
            reader.close();
            String loaded=content.toString();
            File file2=new File("C://andim//testString.png");
            FileWriter filewriter = new FileWriter(file2);
            filewriter.write(loaded);           
            filewriter.flush();
            filewriter.close();
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:50:20+00:00Added an answer on June 13, 2026 at 4:50 pm

    I have program in which I have to load a PNG as a String and then save it again, but after I save it it becomes unreadable.

    Yes, I’m not surprised. You’re treating arbitrary binary data as if it’s text data (in whatever your platform default encoding is, to boot). It’s not. Don’t do that. It’s possible that in some encodings you’ll get away with it – until you start trying to pass the string elsewhere in a way that strips unprintable characters etc.

    If you must convert arbitrary binary data to text, use base64 or hex. If possible, avoid the conversion to text in the first place though. If you just want to copy a file, use InputStream and OutputStream – not Reader and Writer.

    This is a big general point: keep data in its “native” representation as long as you possibly can. Only convert data to a different representation when you absolutely have to, and be very careful about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which creates JButtons which are then added to a JPanel
I have a program which embeds both python2 and python3 interpreters. The libpython shared
I have a simple program which starts n threads and create some load on
I'm having a problem with a program in which I have to load images
I have a php program which load 3 css files like this: <link rel=stylesheet
I have a program which will dynamically set an iframe src to load pages.
I have a program which needs to load some text word for word into
I have got a dll that I load in my program which reads and
Hi I have a program which is goin to basically load news articles from
I have a C# program which calls a C++ dll using DLLImport(stdcall). But using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.