How to copy file from  one location to another

Hi,
I am new to java, I tried the following code to move the file from one location to another
public class CopyFiles {
public String copy ( File source, File target)
throws IOException {   
FileChannel sourceChannel = null;
FileChannel targetChannel =null;
try {   
sourceChannel =new FileInputStream(source).getChannel();
targetChannel= new FileOutputStream(target).getChannel();
targetChannel.transferFrom(sourceChannel, 0,
sourceChannel.size());
finally {   
targetChannel.close();
sourceChannel.close();
return "Success";
public static void main(String [] args) throws Exception{   
File source = new File("C:\\users\\download.pdf");
File destinationFile = new File("C:\\apple\\download.pdf");
copy(source, destinationFile);
The above code is working perfectly, but I Don't want to include the file name in destination file. i.e. File destinationFile=new File("C:\\apple"), and at the same time the pdf with same name has to get stored in the destination location, how can I achieve this.

kameshb wrote:
I Don't want to include the file name in destination file. i.e. File destinationFile=new File("C:\\apple"), and at the same time the pdf with same name has to get stored in the destination location, how can I achieve this.It's not totally clear what you're saying here, but what I think you mean is that you don't want to explicitly set the destination file name--you want to just give the copy the same name as the original. Yes?
If that's the case, then break the original up into separate directory and file name portions, and then construct the destination path from the destination directory plus original file name. You can do that by manipulating the full path string, or by using the methods in java.io.File.

Similar Messages

  • Copy File from One Location to Another Location through xcopy or any

    Hi,
    I Need to copy one file from one location to another location. That file need to save in C:\Windows\System32 folder
    I need to create one .bat file and i want apply that bat as a startup script through GPO.
    I tried through XCopy if i test in machine that is working. If i tried in other machine that is not working. becoz of some Credential issue
    I tried in the script by netuse command after that my script itself not working.
    SET username=XYZ\Administrator
    SET password=abcd
    @ECHO OFF
    cmd "cd C:\"
    net use "\\10.50.5.68\c$\Windows\System32" %password% /user:domain\%username%
    :copy
    Xcopy /S /I /E D:\DLL \\10.50.5.68\c$\Windows\System32 
    Regards, Hari Prasad.D

    You cannot do that in a startup script.  You do not need to do that in a startup script.
    To add files to the system use GP Preferences.
    On Window 7 and later only the Trussted Installer is allowed to add files. (That means only Microsoft.)
    No programs or users should ever alter files in the system area.
    ¯\_(ツ)_/¯

  • How to copy file from one table to another table at another database

    I need to transfer my tables from one workspace and schema to another workspace and schema. Basically I need to create again all the tables at this new schema. How could I transfer data from tha table at old schema to the table at new schema when this table has files stored in it? (data type is blob)
    thank you so much,
    Silver

    Hello Silver,
    Depending which database you're using (if it's available) I would recommend to use datapump.
    Datapump allows you to copy an entire schema to another database, it's the "new" export/import you might now.
    Regards,
    Dimitri
    http://dgielis.blogspot.com/
    http://www.apex-evangelists.com/
    http://www.apexblogs.info/
    REWARDS: Please remember to mark helpful or correct posts on the forum

  • How to copy files from one user to another on same macbook pro

    I used the migration assistant to move files from an old windows pc to my new macbook pro.  It created a second user and put everything there.  I now can't figure out how to move them so they are under me.

    Welcome to Apple Support Communities
    The easiest way is to put your files in /Users/Shared. To access to this folder, open a Finder window, select the Go menu (on the menu bar) > Go to Folder, and type that directory.
    That folder can be read by all users, so you won't have any problem to copy your files to the other user account.
    Another option would be to copy your files to an external drive. Also, see > https://discussions.apple.com/docs/DOC-5472

  • How do you copy files from one user to another

    How do you copy files from one user to another user on the same machine?

    open your home folder in finder move file to public, log into other account, click the go tab on the finder menu, select computer, your hard drive (Macintosh HD Default)>Users, the origonal user> public and drag that file to your specified folder.
    hope it works
    Craig.

  • Copy File From One Drive to Another & Missing Keyword Tags

    Hi,
    I use LR 2.7.  I want to copy one file from one drive to another drive with the same file name.  I thought you could drag the file, but it doesn't copy over the file in the other drive, can you explain what I have to do? 
    Also, I tried to import the folder into another drive, but it doesn't copy the keyword tags.  Is there a way to do this?
    Thanks
    Barb

    Hey Barb,
       Well, if you use the Year/Month/Day/pictures_here  format, none of the folders contains a massive amount of folders.  It's not the number of folders that generates problems, it's having them all in _one_ folder that creates slow-down issues.  Imagine if you had a deck of cards; look for one card... you have to go through all 52 cards to find it.  But, if you broke it down into the 4 groups, you only have to go through 13, which is lots faster.  Now imagine going through 15000 instead of 52...
    Look at the way lightroom can automatically import by date;  it has several preset formats.  If you pick one of those, then when you import new photos, it will automatically create any missing ones, and you have lots less to do.
    Depending on how much work you have already done, you might consider making a new catalog and just importing all of your photos again, and let it create everything quickly for you.
    You could save all of your "edit" changes by writing out all of the xmp files first.  But with 15000 files in one directory, this will double it to 30000 (one per image!), but will preserve all of your edits if you choose to make a new catalog approach.
    As for combing catalogs, once you have one catalog the way you want it, you can select "import from catalog" and it will suck it into a new one, thus combining the catalogs.  The only problem I can see is that it will import it in the format you already have it, so you have to organize it manually before you import it.  Otherwise, you'll get your old format and your new format in the one catalog (but can then move pictures around if you need to).
    Personally,  I'd go the 'write xmp files, create new catalog' approach.  Then just recreate my collections, etc.
    But either way you do it, it will take some time processing, as that's a lot of files to start with!
    Ciao!
    Jason

  • How to move files from one folder to another folder in webdynpro java for sap portal

    Dear Experts,
    I wan to move files from one folder to another folder in SAP portal 7.3 programmatically in webdynpro java.
    My requirement is in my portal 1000 transport packages is their. Now i want to move 1 to 200 TP's into Archive folder.
    Can you please help me how to do in through portal or wd java ...
    Regards
    Chakri

    Hello,
    Do you know what the difference between copying a file this way :
    ** Fast & simple file copy. */
    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();
    ================SECOND WAY============
    AND THIS WAY:
    // Move file (src) to File/directory dest.
    public static synchronized void move(File src, File dest) throws FileNotFoundException, IOException {
    copy(src, dest);
    src.delete();
    // Copy file (src) to File/directory dest.
    public static synchronized void copy(File src, File dest) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dest);
    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    in.close();
    out.close();
    And which is better? I read up on what each kind of does but still a bit unclear as to when it is good to use which.
    Thanks in advance,
    JavaGirl

  • Java Program to copy file from one directory to another failing

    Hello All,
    Oracle Applications 12.1.3
    RDBMS: 11.2.0.30
    SQL*Plus: 10.1.0.5.0
    Java beginner here so help is much appreciated.  I'm have some java code that I'm using to copy a file from one directory to another in an Oracle Applications Server.  I notice that when moving to a new instnace I started to get file not found errors.  I think it's because of the directory.  My question is, does the directory in which I trying to pick up the file have to be a DBA_DIRECTORY, or a UTL_FILE directory in order for the java to find the file and move it?
    Here is my code...
    CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "COPY_FILE_JVS" AS
    import java.io.*;
    public class COPY_FILE_JVC{
      public static void copy(String SourceFile, String DestDir) {
        File source = new File(SourceFile);
        File dest = new File(DestDir);
        File cpDestFile = null;
        try {
          if (dest.isDirectory()) {
              if(source.isFile()) {
                System.out.println("File name = " + source.getName());
                System.out.println("File name dest = " + dest.getPath() );
                System.out.println("File name cpDestFile = " + dest.getPath() + File.separator + source.getName());
                cpDestFile = new File(dest.getPath() + File.separator + source.getName());
              else {
                System.out.println("Directory or File Not Found");
                return;
          BufferedInputStream  br = new BufferedInputStream (new FileInputStream (source));
          BufferedOutputStream  bw = new BufferedOutputStream (new FileOutputStream (cpDestFile));
          int read = 0;
          while((read = br.read()) != -1) {
            //System.out.println((char)read);
            bw.write(read);
          br.close();
          bw.close();
        catch (FileNotFoundException e) {
        e.printStackTrace();
        catch (IOException e) {
        e.printStackTrace();

    I get these messages when calling the java from SQL Plus as follows...
    serveroutput on size 100000;
    dbms_java.set_output(1000000);
    MY_PKG.CopyFile('/home/my_user/set_env.sh','/interface/home'); (this is call to oracle pl/sql package which calls the java.)
    PL/SQL procedure successfully completed.
    Directory or File Not Found
    PL/SQL procedure successfully completed.
    If I change the directory from /home/my_user/ to any directory that is defined in DBA_DIRECTORIES or is a UTL_FILE directory to program works.  Is it perhaps because the java is in a PL/SQL package as seen below?  The PL/SQL program runs as the APPS user and I have issued the following the command grant JAVASYSPRIV to APPS.
    Here is the PL/SQL Package....
    CREATE OR REPLACE PACKAGE BOLINF.MY_PKG AUTHID CURRENT_USER AS
    CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2);
    +++++++++++++++++++++++++++++
    CREATE OR REPLACE PACKAGE BODY BOLINF.MY_PKG  is
    CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2)
    AS LANGUAGE JAVA NAME 'COPY_FILE_JVC.copy(java.lang.String, java.lang.String)';

  • How to transfer file from one client to another client?

    hello,
    i have some questions and hope you all can help me..thanks a lot first..
    Here are the questions:
    i) How can i send a file from one client to another client using RMI?
    ii) Does the client(sender) need to send the file to server, then server save it and then send it to another client(receiver)?
    iii) If using RMI, a client can receive two files from same client(sender) or different client(sender) at a same time? how to do it? when both of the files come in from same port, how to differenciate them?
    iv) For your information, i am doing the File Transfer Server-client application which sender can send any file to other client. Can you give me any ideas? thanks..
    Last, thanks again..

    Your questions reflect some ambiguity in terms.
    "Client" and "server" are commonly used in two different senses:
    1. Technical sense: A client process makes requests, and a server process fulfills the request (provides a service).
    2. IT sense: A client computer (process) makes requests of a server (computer) process, and the server (computer) processes the request.
    In the first case, any computer might be a client, or a server, or both, depending on the processes being executed. In the second case, the computers are assigned some role.
    So: If you wanted to, you could implement client/server processes communicating between two peer computers, using RMI.
    This may not be what you wanted; if you really want to distinguish client and server computers, then the answer is that yes, you will probably put files into intermediate storage on the server computer.
    Finally, you probably do not have to worry about port conflicts if you use RMI; while the initial client server contact is established through a registry operating on a standard port, the actual RMI communications is established using random ports, one for each link.

  • Copying files from one workstation to another only works in one direction

    I could copy files from my workstation to another workstation but could not copy files from that workstation to my workstation.  There are 2 Cisco switches between these workstations.
    I found out that one switch had its workstation ports hard coded to 100Mbps but the uplink ports (on both switches) were set to auto (the uplinks were 1 Gbps ports).  This caused the copy problem.  Once the w/s ports were set to auto, the problem
    went away.  I'm just wondering if anyone else has had this problem but found that the solution was something different than what I found?
    William Fearn

    I don't think that is possible, to platform specific. You'll need to make the directories accessible without password protection, or use another way of copying the files, such as FTP. There are multiple FTP packages available for java, such as:
    http://jakarta.apache.org/commons/net/
    (of course that would require setting up some sort of FTP server)

  • How to transfer files from one partition to another on the same computer

    I had a crash could not boot back into my 8.1 upgrade. Went out bought standalone 8.1. I loaded it on HD free space now I want to transfer files from one partition to another.

    hi
    say there are two infoobjects X  and Y.
    X is existing infoobject and Y in new one.
    go to infosurce-search for X-create export datasource - map the rules and load the data.
    This is one way.
    Other way is u can use X as a reference object of Y so that both of them have same structure and both objects will share common database tables in BW which are exsting for X.
    Hope this helps.
    Kindly assign the points if it helps.

  • How to move files from  one  system  to another in plsql

    Hi all,
    I want to move file from one system to another using plsql procedure .
    Kindly let me know what package i have to use for doing this .
    Thanks,
    P Prakash
    Edited by: prakash on Jul 27, 2011 2:20 AM
    Edited by: prakash on Jul 27, 2011 2:59 AM

    BluShadow wrote:
    That's ok if you want to diet. ;)Ooh look, the strange punctuation is back. Can it be true that the forum software has caught up with the '80s?
    I almost feel like connecting to the internet using a 9600 baud modem to celebrate.
    http://upload.wikimedia.org/wikipedia/commons/5/5d/TeleGuide-terminal.jpg
    Colon dash right bracket.
    (Old habits die hard)

  • How to copy a file from one location to another ???

    Hi,
    I have a file ('D:/Pictures/1.jpg') and I want to copy this 1.jpg file to a location called 'D:/Folder1'. I tried the WebUtil_File.copy_file function, but it does not work. it returns false boolean.
    I am using windows xp and oracle 10g.
    The webutil configurations are also completed.
    This D: drive is the one which has oracle 10g application server installed.
    Am I doing anything wrong or missed?. I need to complete this urgently. but it does not work.
    Or any other way to copy a file from one place to given any path?. After this I have to renaming (may be using Rename_File function) the copied file. Please advice me.

    An alternative could be HOST command, that calls OS prompt to run a command in the server and then return the focus to the Forms:
    HOST ('copy D:/Pictures/1.jpg D:/Folder1/1.jpg');
    Edited by: Kleber M on Sep 9, 2011 12:38 PM

  • Copy file from a location to another

    I work on a Java process that must be optimized and I have some questions.
    This process includes a part of copying a file from a location from another (to be more specific, all files that exist in a directory, to copy in another directory), wich technique is more efficient and quickly? To copy using FileInputStream/FileOutputStream in Java or using a java Runtime process that calls a copy command of the Operating System?

    I work on a Java process that must be optimized and I
    have some questions.
    This process includes a part of copying a file from a
    location from another (to be more specific, all files
    that exist in a directory, to copy in another
    directory), wich technique is more efficient and
    quickly? To copy using
    FileInputStream/FileOutputStream in Java or using a
    java Runtime process that calls a copy command of the
    Operating System?Neither. Use the FileChannels in the nio package if you can:
    /* Note:  Proper resource closing and exception handling is not done here */
    try {
            // Create channel on the source
            FileChannel srcChannel = new
            FileInputStream("srcFilename").getChannel();
            // Create channel on the destination
            FileChannel dstChannel = new
            FileOutputStream("dstFilename").getChannel();
            // Copy file contents from source to destination
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
            // Close the channels
            srcChannel.close();
            dstChannel.close();
        } catch (IOException e) {
        }Spawning a Runtime process is probably the least efficient way to do it.
    - N

  • How to copy data from one table to another (in other database)

    Hi. I would like to copy all rows from one table to another (and not use BC4J). Tables can be in various databases. I have already 2 connections and I am able to browse source table using
    ResultSet rset = stmt.executeQuery("select ...");
    But I would not like to create special insert statement for every row . There will be problems with date formats etc and it will be slow. Can I use retrieved ResultSet somehow ? Maybe with method insertRow, but how, if ResultSet is based on select statement and want to insert into target table? Please point me in the right direction. Thanks.

    No tools please, it must be common solution. We suceeded in converting our BC4J aplication to PostgreSQL, the MSSQL will be next. So we want to write simple aplication, which could transfer data from our tables between these 3 servers.

Maybe you are looking for