Problem in copying the file

i had created a frame in which iam getting a source file and then copies the file to certain location...the problem is when i copy a large file it is doing well for the first time but when the second time while iam copying exception occurs like this
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
iam using the code
public static void copy(File source, File dest) throws IOException {
     FileChannel in = null, out = null;
     try {         
          in = new FileInputStream(source).getChannel();
          out = new FileOutputStream(dest).getChannel();
          long size = in.size();
          MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
          out.write(buf);
     } finally {
          if (in != null)          in.close();
          if (out != null)     out.close();
    }how to solve this

Do you simply want to copy the file. Try, not using many non-instrictive data types, because these use many extra variables and you end up using a lot more memory than you would have req.
Eg-
public void copy(File source,File dest) throws IOException{
     FileInputStream fins=new FileInputStream(source);
     byte[]b=new byte[(int)fins.length()];
     fins.read(b);
     fins.close();
     FileOutputStream fos=new FileOutputStream(dest);
     fos.write(b);
     fos.close();
}This is code is not a solution, because here i am reading all file at once. This is basically to show you a method.
In real situation you can set the size of byte[]b to 1024, and loop util read(b); method returns -1.

Similar Messages

  • Problem in copying the file to right directory

    Hi
    I am trying to copy a file uploaded by the user to a new directory. the dir structure is
    /data/temp/username/. The username dir is created at the time of upload. so if any user with the username say "bob" logs in, a directory bob is created under /data/temp.
    the file uploaded by the user needs to be stored under "bob" dir ....in my case its being stored in /data/temp.
    When i try to print file.getAbsoluetPath it shows me as the file is stored under bob....so i get /data/temp/bob/filename.doc
    but when i check the dir the file is actually stored under temp & not bob.
    Can anyone help please..
    Thanx

    Hi there
    Thanx for you reply.
    I read the snippet, but it says about Windows, I am working on Linux. Wont that make a diference when i getAbsoluetpath.. Coz the print statement shows me the path as /data/temp/username
    Also i ran a prototype of the code on windows and it seems to work fine ... like it creates a user directory under C:/Java/temp and then stores the file under the new created user directory.
    If the code would help to think in a better way heres it:
    /* tempdir is already defined in config file as /data/temp .. I am just calling it here to create
    the new directory under it.. this line works fine */
    File usrDir = new File(tempDir, username);
    boolean dirMade = usrDir.makeDir();
    String path = dirMade.getAbsolutePath();
    File fupload = new File(path, fileName);
    /* in the above line, fileName is the name of the file uploaded by client (user) */
    String filepath = fupload.getAbsolutePath();
    /* in tha bove line gives the path as /data/temp/username/filename.ext */
    but when i go to the directory its actually stored as /data/temp/username and /data/temp/filename.ext
    Any suggestions please
    Thanx

  • Problem in copy the file from WAR

    I have the following requirement.
    I have one file in WAR file which is to be copied to sme location in server during Server start after deployment.
    I am using one servlet, which is being loaded during startup. I have written the code in "init" method of servlet.
    try {
    String fontFolderString = config.getServletContext().getInitParameter("WORKING_FOLDER")+ "\\fontFolder";
    BufferedInputStream bis = (BufferedInputStream)this.getClass().getClassLoader().getResourceAsStream("\\image\\a.jpg");
    File in = new File("");
    File out = new File(fontFolderString);
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(out));
    byte inArr[] = new byte[bis.available()];
    int size = bis.read(inArr, 0, inArr.length);
    bos.write(inArr, 0, size);
    bis.close();
    bos.close();
    } catch (Exception e) {
         e.printStackTrace();
    }image folder as mention in the line "BufferedInputStream bis" is in the directlry where servlet is there.
    Here "bis" is being returned as null.
    Please provide me the pointers what is wrong above
    ~Aman

    The line of code
    getClass().getClassLoader().getResourceAsStream("\\image\\a.jpg");searches the file a.jpg inside a dicrectory image located in the classpath of the web application. For example in web-inf/classes... The position of the servlet is insignificant.
    If you are looking for the file in the web context (for example in the image folder of your web application you should use:
    config.getServletContext().getResourceAsStream("\\image\\a.jpg");

  • I had to transferr my music files from one computer to another by directly copying the files from the hard drive to a new drive.  Itunes recognized the files and I was able to load in new songs.  The new songs won't sync with ipod.  Ideas?

    To copy the files I had to remove the hard drive and direct copy them to an expansion drive on another machine.  ITunes recognized the copied files and operates correctly on the computer.  I was able to download additional songs to ITunes on the computer without problem.  The IPod however will not recognize the new songs, does an abreviated sync process and gives a message that the IPod cannot sync because files are being used by another application.  It is also does not want to accept the disconnect command.  Any suggestions on how to solve the sync problem?

    how did you save the songs? can you play the songs? have you restarted the computer?

  • I can't open my iphoto library on an external disk (78 gb ntfs); if i copy the files on the iMac the file is smaller and not usable. how can i recover my photo?

    hi all,
    i'm using the last version of iPhoto and i save my library on a western digital external USB drive; the drive is formatted in NTFS; i usually use tuxera to access and browse the files.
    Unfortunately, i don't know why, i access this drive in read only mode, so iPhoto doesn't accept the library any more; i tried to copy the library on the iMac but the copied file is really smaller (54 gb instead on the 78 on the drive), and iphoto doesn't accept it.
    i need to find a solution and to recover my library, i've no other copies?!
    tuxera doesn't open any other ntfs drive i think the problem is there, but i have to finf other solutions to recover my photos.
    thank you all for any suggestion.

    This problem is entirely due to this:
    the drive is formatted in NTFS
    iPhoto needs to have the Library sitting on disk formatted Mac OS Extended (Journaled). Users with the Library sitting on disks otherwise formatted regularly report issues including, but not limited to, importing, saving edits and sharing the photos.
    Do you get any error messages when you try and copy the file back to your Mac?
    Do you have a back up?

  • Restoring my mac book pro after copying the files off via target disk

    Hi guys any help would be great.
    I had a problem with my hard drive in my early MBP, after seeing an apple genius, who said it was mostly likely a software issue and wiping the disk and reinstalling everything shoudl fix it.
    So as i didnt have an up to date time machine back up, i used the target disk utility to copy the files from the drive over onto another mac (actualy on a networked harddrive)
    no i have completed a wipe, and reinstalled the operating system (snow leapeard as thats the last disc i had - MBP was on Lion prior to the issues)
    now i want to restore all my files from before.
    what is the best way to do this?
    can i just drag and drop each file from the networked hard drive back into the coresponding ones on my MBP?
    thanks

    Only your users files inside music, pictures, movies documents etc folders.
    Nothing in Library, nor Applcations, you do that you hose the account and the apps won't work anyway, at least most of them.
    or i will have to reinstall the aplications but i tunes and so on will be able to find the files?
    yes and reinport music into iTunes so it goes into the new itunes library file, ditto with iPhoto Library (old) righ click and "show package contents" and import the Originals folder contents into new iPhoto library.
    support files have paths in their files that go to the path on the origanl system, you changed that so the paths are off, so you import the original files again.

  • I have bought a photoshop elements and have installed it on my MacBookPro. I have just bought an iMac and what to install on this computer as well. The iMac does not have a disc drive. I have copied the file onto a remote hard drive and tried to install b

    I have bought a photoshop elements and have installed it on my MacBookPro. I have just bought an iMac and what to install on this computer as well. The iMac does not have a disc drive. I have copied the file onto a remote hard drive and tried to install but it thinks I have a pirate copy. What can I do?

    If you click the link in my first post in this thread, then click "still need help? Contact us" it takes you to a page that looks exactly like the page where you started, but isn't. Select PSE for the program you want help with, click "Activation and Signing In" for the problem area and you will see this:
    The gray area below the blue bar is a link to start a chat.

  • How can I copy my Itunes-library from Iphone to my new Mac, as I forgot to copy the files from the old PC, and now it is to late. I only have music on my Iphone.

    How can I copy my Itunes-library from Iphone to my new Mac, as I forgot to copy the files from the old PC, and now it is to late. I only have music on my Iphone4. Thank you.
    gunnarfromhovik

    For iTunes purchases only..
    Connect the iPhone to your Mac, launch iTunes.
    If you haven't done this yet, go to the iTunes menu bar click Store / Authorize This Computer
    Now from the iTunes menu bar again, click File / Transfer Purchases From...

  • My itunes cannot be update on my windows xp. so i delete my itunes and install new, but now i got new problems, it says "The file itune.library.itl cannot be read because it was created by newer version of itunes." so how can it be solved?

    my itunes cannot be update on my windows xp. so i delete my itunes and install new, but now i got new problems, it says "The file itune.library.itl cannot be read because it was created by newer version of itunes." so how can it be solved?

    Hello Bebyida,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at:
    http://www.apple.com/itunes/download/
    Best of luck,
    Mario

  • Problem while dowloading the file from Application Server

    Dear Experts,
                 I am facing the Problem while downloading the file from Application server.
    We done the automatic function while saving the invoice, this will create an idoc, and this idoc is written in the Application Server.
    I am running the Transaction AL11 and select the record, and from menu --> List, i am downloading into TXT format.
    But for some segments, the length is long, and so the last 3 to 4 fields values are not appearing in the File. Even though i am unable to view the values in the file before downloading. But i can view in IDOC.
    Please help me to solve this issue.
    Thanks & Regards,
    Srini

    but our user will use the Txn. AL11 and they will download from there
    Educate the user On a serious note, tell him this is not how data from app server should be downloaded. You can ask him to talk to the basis team to provide him access to the app server folder where the file is being stored.
    I can set the Variant and put this in background, But always the file name will be change, Like we use Time stamp in the File name.
    You can't automate this process by scheduling in BG mode. This is because the in BG mode you can't dwld the file to presentation server.
    Hope i'm clear.
    BR,
    Suhas

  • I just purchased Adobe Photoshop Elements 13 but my computer is not recognizing my CDRom and I cannot download the program. I tried copying the files on the program disc to my hard drive but get a message the installer will not work. I have the redemption

    I just purchased Adobe Photoshop Elements 13 but my PC is not recognizing my CDRom and I cannot download the program. I tried copying the files on the program disc to my hard drive but get a message the installer will not work. I have the redemption code and serial numbers. Can I download the program online somewhere? If I download a trial version for instance will it allow me to enter my serial number and use it for the full program?

    Downloads available:
    Suites and Programs:  CC 2014 | CC | CS6 | CS5.5 | CS5 | CS4 | CS3
    Acrobat:  XI, X | 9,8 | 9 standard
    Premiere Elements:  13 | 12 | 11, 10 | 9, 8, 7 win | 8 mac | 7 mac
    Photoshop Elements:  13 |12 | 11, 10 | 9,8,7 win | 8 mac | 7 mac
    Lightroom:  5.6| 5 | 4 | 3
    Captivate:  8 | 7 | 6 | 5
    Contribute:  CS5 | CS4, CS3
    Download and installation help for Adobe links
    Download and installation help for Prodesigntools links are listed on most linked pages.  They are critical; especially steps 1, 2 and 3.  If you click a link that does not have those steps listed, open a second window using the Lightroom 3 link to see those 'Important Instructions'.

  • I can not do the update , what should I do to fix this error ? "There was a problem with downloading the file . For tips on troubleshooting , please go to Customer Support . ( Error code : 204 ) ." thanks

    I can not do the update , what should I do to fix this error ?
    "There was a problem with downloading the file . For tips on troubleshooting , please go to Customer Support . ( Error code : 204 ) ." thanks

    Hi,
    Please refer to the help document below:
    Error downloading, installing, or updating Creative Cloud applications
    Regards,
    Sheena

  • Problem regarding Configuring the File Adapter

    Hi,
    I am making a Flat File to XML File Scenario. I have made all the Necessary developments and the Routings in the Integration Repository and the Integration Directory. The Sender Communication Channel Uses the Message Protocol "File Content Conversion". All the required steps have been done. We have installed the System and also the Basis Team has done the Post Installation Activities.
    <b> The Problem is that the "File is not getting picked up". When checking the RWB and henceforth the Adapter Monitoring we see that the File Adapter is disabled and the XI and the BC adapters are green. Previously we were on SP9 and now we are on SP14. The SAP Basis is at 640 and SP9 and the SAP ABAB is on 640 and SP9.</b>
      We are also unavailable to see the File Communication Channels that we have created. When the Tcode "sxi_cache is run it is giving an error that Cache not refreshed andon refreshing various times too the Cache is not Refreshed.
    Please Suggest us what we should do. Eagerly waiting for a reply and Thanking in Anticipation. Do we need to make some for configurations for that.
    Warm Regards
    Chakra and Somnath

    Hi Somnath,
    If the sender file adapter is up and running and even after that if you find some problem, the first thing you need to do is check if your file has been picked. The easiest way to check the same is to set the mode to DELETE, that is the file gets deleted as soon as it is picked. If your file has not been picked then the reasons for the same could be the following.
    - The path that you specified while configuring the sender communication channel would have been wrong.
    - While specifying the file name take care as it is case sensitive.
    - If you require content conversion then make sure that the parameters entered at the time of configuration are all correct.
    Please go thru the following links to get a very clear picture of your requirement.
    http://help.sap.com/saphelp_nw04/helpdata/en/e3/94007075cae04f930cc4c034e411e1/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/44/6c9442c22d1131e10000000a1550b0/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/8c/607fe4756b654ab9f420097c29f6e9/frameset.htm
    Also make sure that your content conversion is in sequence wrt Datat Type Sturcture.
    Go through these blogs below for more detail on the same:
    /people/sap.user72/blog/2005/01/06/how-to-process-csv-data-with-xi-file-adapter
    /people/shabarish.vijayakumar/blog/2006/02/27/content-conversion-the-key-field-problem
    I hope this would help you solve your problem.
    Regards,
    abhy
    Message was edited by: Abhy Thomas

  • Problem in opening the file

    I have a file I have problems in opening the file
    The file exists in this link
    http://www.multiupload.com/ZOQT6RW8PK
    When I open the file shows the following messages
    I used All Adobe Reader versions, But no avail
    I need a quick solution
    Waiting for your reply

    You may contact the support of the creator of this plugin.

  • How i will run copy the file in oracle 7.3 ?

    how i will run copy the file in oracle 7.3.
    new version of oracle copy function is available in util_file but in oracle 7.3 this is not available.here i am using 7.3.
    please help
    Thanks
    Deepak S.

    The technique used to be to send messages from your procedure to a waiting shellscript listener using DBMS_PIPE. You would write a script (could be ksh, Perl, Ruby etc) on the server with a loop in which it connects to the database and waits for DBMS_PIPE messages. When it gets one, it executes the command that was contained in the message and loops around to continue waiting again.
    7.3 is really ancient though. If you're running it at all this far out of support it should really just be ticking over and not subject to new development.

Maybe you are looking for

  • Can someone please help me decide on a new plan?

    I'm currently on BT Total Broadband Option 1. I'm currently out of contract.  The plan itself is £13.83 but I'm constantly going over my 10GB usage allowance so seeing additional fees of £5.30-£10.60 per month, I need a plan with higher usage allowan

  • Need a test for fetch into from a cursor or an alternative.

    I've got a small oddity. Ultimately it's two cursors that are exactly the same except a part of the WHERE clause. Depending on how many records I am going through (based on a counter) I need to use one cursor or the other as the outer loop, to then p

  • Itunes 11 releease date for windows 8?

    Hi does anyone know when the release date is after being delayed from october to november.... appreciate reply!

  • Acrobat X Pro Serial Number Not working

    Hello, I'm trying to install a new copy of Acrobat X Pro, and am getting an invalid serial number message. Can you advise?

  • How to download driver for OLD sony ericsson???

    after several years sony ericsson known worlwide, now disappear and SONY standing alone..brrrr But, i have a problem. I need to download driver for my mobile brodband called SONY ERCSSON MD-300 because my old file is gone and i didnt found it. Please