Saving file, but jpg and gif comes out garbled

Would someone please help me? I'm at my wit's end here. I'm writing a program that reads a file and saves it, but it needs to read a maximum of 512 bytes before it starts to write. It needs to work with both text files and images.
- It works fine with text files.
- It works fine with bmp images.
- It works fine with jpg and gif images under 512 bytes.
- With a jpg or gif over 512 bytes, it comes out all garbled, but the same file size as the original.
One thing is really confoozling me. When the image comes out garbled, I tried changing the files for both the original and the copy into text files by changing the file extension to .txt. Both the text files were identical! So if the data is identical, why is the copy not showing the same image as the original?
import java.io.*;
     public class SaveFile{
          public static final int BUFSIZE = 512;
          public static final String LIBRARY = "C:\\";
          static int totalRead = 0;  //Total number of bytes in buffer (Max BUFSIZE).
          static int fileSize = 0;  //Size of whole file.
              static String fileName = "";  //Name of file.
             static char[] data = new char[BUFSIZE];  //The data in the file.
          public static void main(String[] args){
                        //Bytes already read by the buffer.
                  int charsRead = BUFSIZE; 
                      //Takes name of file from main argument.
                     for(int i=0; i < args.length; i++)
                         fileName = fileName + args;
//Name of file including directory.
          String fetchFile = LIBRARY + fileName;
          File file1 = new File(fetchFile);
          * Reads a string from the file to data (char array).
               BufferedReader reader =
new BufferedReader(new FileReader(file1), BUFSIZE);
               BufferedWriter writer =
new BufferedWriter(new FileWriter(fileName));
               //Will continue as long as buffer is full.
               while (charsRead == BUFSIZE){ 
                    charsRead = reader.read(data, 0, BUFSIZE);
                    if (charsRead != -1) {
                         totalRead = charsRead;
                         write(writer);
                         fileSize = fileSize + totalRead;
               reader.close();
               writer.close();
          System.out.println((fileSize) + " bytes read and saved as " + fileName);
     * Writes data to a new file.
     public static void write(BufferedWriter writer) {
               for (int j = 0; j < totalRead; j++) {
                    writer.write(data[j]);
I stripped away the directory selection and error handling to make it simpler.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Cadvan1123 wrote:
I think the best choice is to use a ByteArrayOutputStreamWrong. You do not need a ByteArrayOutputStream
and I think that it uses ASCII encodingPossible, but you can't just make that assumption. On windows for instance, there's a good likelihood it could use ANSI. And BTW, ASCII is for the most part dead, as it has been superseded by UTF-8.
Check out t [jacobs.io.DataFetcher in TUS|http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/] for a threadable and optimal/close-to-optimal solution for reading in binary files.
Edited by: tjacobs01 on Mar 20, 2010 2:56 PM

Similar Messages

  • I have a disk in my mac and came not get it out?  I use the command E key to eject but it does note come out?

    I have a disk in my mac but it does note come out useing command e to eject? how do i get it out or do I have to get it jixed?

    Force eject a stuck cd/dvd
    First try the normal methods to remove the disc. Drag its icon to the Trash can in the Dock or select 'Eject' from the File menu.
    If you are running a virtual machine, e.g. VMFusion, ensure that the CD is disconnected from the virtual machine. This will sometimes allow the CD to now show up in Mac OS X.
    Shut down the computer and start up whilst holding down the mouse button. This may take some time, but keep your finger on the mouse button right up until the disc comes out or the log-in screen has appeared.
    If you have Toast Titanium installed on your computer, choose EJECT DISC from the menubar.
    Sometimes you can successfully use the eject disc button in iTunes even if the disc is not visible to the Finder
    Open Disc utility and choose the disc you wish to eject in the left-hand pane, then click on the Eject button.
    Some Macintoshes have a paperclip hole that you can insert a straightened paperclip into, manually triggering the eject mechanism.
    Open Terminal and type "drutil tray eject" to eject the disc/tray, and "drutil tray close" to close the tray.
    If your computer has an eject button on the keyboard, restart the computer holding down the Option key. When the startup disk selection screen appears, let go of the option key and press the keyboard's eject button.
    Source: http://guides.macrumors.com/Force_Eject_a_Stuck_CD_or_DVD

  • System Language is English but all documents and reports come out in French

    System Language is English but all documents and reports come out in French. How do I get the system to switch all of the documents and reports to print in the English layouts?
    thx, Richard.

    Richard,
    Probably this is due to what Dan has pointed out.
    You can open the PLD template and go to Print Layout Design menu on the Top and click Display document properties.  Check the language properties.
    Suda

  • Hey every one ,i bought a new ipad 2 last month,i am not using it  so much but  sometimes it does respond,hang's up,all games and app come out of there folder . can you plz help me

    hey every one ,i bought a new ipad 2 last month,i am not using it  so much but  sometimes it does respond,hang's up,all games and app come out of there folder . can you plz help me.my email id is [email protected]

    Try a reset. Hold the Sleep and Home button down for about 10 seconds until you see the Apple logo. Ignore the red slider.
    Don't leave your email address on a public forum

  • Hi, it's my first time to add an item in my cart and saved it, but haven't checked it out yet. Does that mean I will already be charged for the item?

    Hi, it's my first time to add an item in my cart and saved it but haven't checked it out yet. Does that mean that I will already be charged for the item?

    No. Not until you check out.

  • Exporting (in H.264 format in HD 720p 23.976 preset) and audio comes out separate while video file is a .m4v instead of .mp4

    Using Premier Pro CC 2014.
    Exporting (in H.264 format in HD 720p 23.976 preset) and audio comes out separate while video file is a .m4v instead of .mp4.
    Anybody know why audio and video are exporting separately? I can't figure out why. Help is much appreciated.

    Thank you for the quick response. Unfortunately, that doesn't fix it.
    I have the same issue when I try to export it through Adobe Media Encoder. I even tried to go through using After Effects and export through Adobe Media Encoder with the same results. I'm wondering if I might be better off uninstalling then reinstalling all my adobe programs.

  • I am trying to save a photoshop CS6 psd file to jpg and get an option jpg, jpg2000 or jpg stereo, I choose jpg and then Mac 256 colours, normal or interlaced.  I just want regular jpg with a high resolution.  What am I doing wrong, never had this problem

    I am trying to save a photoshop CS6 psd file to jpg and get an option jpg, jpg2000 or jpg stereo, I choose jpg and then Mac 256 colours, normal or interlaced.  I just want regular jpg with a high resolution.  What am I doing wrong?  , never had this problem before.  Help!!! 

    Your description of the issue is not clear:
    What format, bit depth and color was your original file "from the net"?  It sounds like it was a Color Indexed GIF, which would cause what you are describing.
    In the Macintosh Finder, select said "net image" and do a Get Info on it (Command+i). When the Info box appears in the Finder, take a screen shot and upload it here by using the little camera icon in the Reply Editor in the forum's web interface.
    If the image is a 256-color GIF, remember the rule:  GIGO = garbage in, garbage out.

  • JPGs and GIFs in Coverflow not showing

    For some reason the cover view in the Finder is no longer showing previews of JPGs and GIFs, but is only showing the generic icons for each.
    Any clues as to what's causing that and how to fix it?
    Cheers.

    Try deleting the Finder.plist preference:
    /Users/Kenny/Library/Preferences/com.apple.finder.plist
    Then relaunch the Finder.
    Thanks ... Ken

  • After updating os to 10.9.1can't open pdf files.  I get a message saying It may be damaged or use a file format that Preview doesn't recognise. I can still read older saved files but no new ones arriving by email.  Any help would be appreciated.

    After updating os to OSX 10.9.1 I can't open any new pdf files.  I get a message saying It may be damaged or use a file format that Preview doesn’t recognise. I can still read older saved files but no new ones arriving by email.  Also can't attach a pdf to an email to send out.  Any help would be appreciated.

    Back up all data before making any changes. Please take each of the following steps until the problem is resolved.
    Step 1
    If Adobe Reader or Acrobat is installed, and the problem is just that you can't print PDF's displayed in Safari, you may be able to print by moving the cursor to the the bottom edge of the page, somewhere near the middle. A black toolbar may appear under the cursor. Click the printer icon.
    Step 2
    There should be a setting in its preferences of the Adobe application such as Display PDF in Browser. I don't use those applications myself, so I can't be more precise. Deselect that setting, if it's selected.
    Step 3
    If you get a message such as ""Adobe Reader blocked for this website," then from the Safari menu bar, select
    Safari ▹ Preferences... ▹ Security
    and check the box marked
    Allow Plug-ins
    Then click
    Manage Website Settings...
    and make any required changes to the security settings for the Adobe PDF plugin.
    Step 4
    Triple-click anywhere in the line of text below on this page to select it, the copy the selected text to the Clipboard by pressing the key combination command-C:
    /Library/Internet Plug-ins
    In the Finder, select
    Go ▹ Go to Folder
    from the menu bar, or press the key combination shift-command-G. Paste into the text box that opens (command-V), then press return.
    From the folder that opens, move to the Trash any items that have "Adobe" or “PDF” in the name. You may be prompted for your login password. Then quit and relaunch Safari.
    Step 5
    The "Silverlight" web plugin distributed by Microsoft can interfere with PDF display in Safari, so you may need to remove it, if it's present. The same goes for a plugin called "iGetter," and perhaps others — I don't have a complete list. Don't remove Silverlight if you use the "Netflix" video-streaming service.
    Step 6
    Do as in Step 4 with this line:
    ~/Library/Internet Plug-ins
    If you don’t like the results of this procedure, restore the items from the backup you made before you started. Relaunch Safari.

  • How to save a jpg and gif at same pixel size?

    This doesn't seem to be very convenient.. I need two file types of same image, identical dimensions..
    Thx

    I don't have AI 13 (CS3), but in AI 12 (CS2), I get the same pixel dimensions for both .jpg and .gif versions created via Save for Web (using your 350x200 example).
    To report oddball forum software behavior, post over at Forum Comments. Don't hold your breath.

  • Can no longer copy and past jpg and gifs into InDesign from c:\user\doc\pics

    Here is the error message we get when doing a copy and past from the hard drive to InDesign.
    Error Message from InDesign:
    Adobe InDesign may not support the file format, a plug-in that supports the file format may be missing...
    First, do I need to add a plug-in to be able to copy and past jpgs and gifs?
    Second, we have always used clip organizer with no problem. Ability to copy and paste jpg & gif suddenly stopped. We get a box but no image ont the page. Have already tried dumping temp internet files through internet explorer. Have already un & re installed InDesign and repaired and fixed everything I could think of.
    Third, we can use the place command to place jpgs but can not place gifs.
    William (Helping Mrs. White)

    It sounds as if Mrs White needs to search visually for graphics and then place them. She can either set Windows Explorer
    to thumbnail view and drag a graphic on to an InDesign page - this will in effect be placing the graphic, not copying
    and pasting, and is the right way to go. Or she can use File > Place, and set the view in the Explorer window that she
    selects from in the Place command to a thumbnail view to see the graphics.
    GIFs can be placed. They are not a recommended format, but if you are placing clip art then you may have no option.
    So, in answer to your original question, no, you don't need a plug-in to place JPEGs and GIFs. But note that is place,
    not copy and paste.
    k

  • Something keeps trying to download on my mac and I don't know what it is. It is not in the apple store and just comes out nowhere and request for my password to download something and I don't know what it is. How to make this stop?

    Something keeps trying to download on my mac and I don't know what it is. It is not in the apple store and just comes out nowhere and request for my password to download something and I don't know what it is. How to make this stop? It pops up every single day.

    Erica,
         I can, with 99.99% certainty, tell you that you are absolutely right in not wanting to download or install this "Helper," whatever it is (but we can be equally certain it would not "help" anything).
         I cannot comment as to Oglethorpe's recommendation of 'adwaremedic'; I am unfamiliar with it.  His links to the Apple discussion and support pages warrant your time and attention.
         It might be really simple -- Trying looking in your Downloads folder, trash anything that you don't know with certainty is something you want to keep, and then Secure Empty your Trash. Then remove the AdBlock extension, LastPass, and Web of Trust extensions to Safari and re-boot. If the issue goes away, still be extraordinarily careful in the future.
         Unfortunately, it's probably not going to be that simple to get rid of, in which case I'd then try the line by line editing in HT203987. 
         I have no further suggestions (other than a complete wipe and re-install...but that's a pain because trying to restore from Time Machine would simply ... restore the Mal).
       For the rest of us, please post when you find a solution.
         Best.
         BPW
      (Also, try to edit your second post -- black out your last name on the screenshot and re-post it for others)

  • How can I save an object Image in a file with .jpg or .gif format?

    Greetings
    I'm programming a Ray Tracer but now I need to know like save an object Image in a file with .jpg or .gif format. Could anyone help me, please? It's very important to me do this as soon as possible. Thanks.

    In 1.4 you have ImageIO available ... javax.imageio.ImageIO.write is the method to call.
    - David

  • If I buy LR5 and LR6 comes out, can I upgrade for free?

    I need to upgrade from LR4 to accommodate my new camera. I hear LR6 is coming. If I buy LR5 and LR6 comes out within x amount of time, can I upgrade for free from LR5 to LR6 for free? I heard this somewhere and want to be sure. 

    The answer is that Adobe normally gives free upgrades if you buy the earlier version within 30 days of the release of the new version. But since no one knows when the new version will be released, you basically have to guess.

  • Overclock past 3315Mhz and static comes out speakers

    i overclocked my processor to 3406Mhz, i new it could go that speed because i had done it before, but i was listening to music at the same time. when it got to 3406Mhz the music stoped and static replaced it.
     i quickly set it to it's orignial speed. i then slowly increased to the speed, with the music playing, and found out that this only happened when it got passed 3315Mhz.
    i tried overclocking the CPU first at 3406Mhz then clicked play but instead of static comming out my speakers my computer reset itself
    can anyone suggest why it does this and tell me how to solve it.
    thanks

    Possibly the CPU - at those settings it becomes sensitive to EMI (Electro-Magnetic Interference). I know that when I have another energy source going as I'm bumping my 64 on the up - i.e., home stereo cranking, I have difficulties maintaining or reaching previously attainable settings.

Maybe you are looking for