Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Copying a File

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Copying a File

    Hi guys it's my first time here, I hope you guys can help me out with my question.

    I'm to write a program that copies a file, I've already done that it works at copying but what I need help with is the professor wants to be able to do this from the command line:

    java FileCopy sourceFileName.txt to destinationFileName.txt

    How do i get my program to respond to such command from command prompt?
    This is my code it copies the file when it's run from Eclipse,

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    public class FileCopy
    {
    	String srcFileName;
    	String dstFileName;
    	String line;
    	BufferedReader source;
    	PrintWriter destination;
    	File fileSource;
    	File destinationFile;
     
    	public FileCopy(String src, String dest)
    	{
    		this.srcFileName = src;
    		this.dstFileName = dest;
    		fileSource = new File(this.srcFileName);
    		destinationFile = new File(this.dstFileName);
    		try 
    		{
    			this.copyFile();
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
     
    	private boolean openFile()
    	{
    		try 
    		{
    			if (fileSource.exists())
    			{
    				source = new BufferedReader(new FileReader(fileSource));
    				return true;
    			}
    		}
    		catch (FileNotFoundException e) 
    		{
    			e.getMessage();
    		}
    		return false;
    	}
     
    	private boolean copyFile() throws IOException
    	{
    		if (this.openFile() == true)
    		{
    			try 
    			{
    				destination = new PrintWriter(destinationFile);
     
    				do 
    				{
    					line = source.readLine();
    					if (line != null) 
    					{
    						destination.write(line + "\n");
    					}
    				}
     
    				while (line != null);
    				source.close();
    				destination.close();
    				return true;
    			} 
    			catch (FileNotFoundException e) 
    			{
    				e.getMessage();
    			}
    		}
    		return false;
    	}
     
    	public static void main (String[] args)
    	{
    		FileCopy newCopy = new FileCopy("sourceFile.txt", "destinationFile.txt");
    	}
     
    }

    This may not be the right section but I didn't know where else to post it can someone help me out please I really want to figure this out.


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying a File

    Try this URL for correcting the error...Jus give the source and destination file location correctly.
    https://www.blogger.com/blogger.g?bl...50094219100570

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Copying a File

    Quote Originally Posted by ubiByte View Post
    ...

    java FileCopy sourceFileName.txt to destinationFileName.txt
    See the section of Java tutorials for Command-line Arguments

    Try the following to test it:
    public class Z {
        public static void main(String [] args) {
            System.out.println("Number of arguments = " + args.length);
            for (int i = 0; i < args.length; i++) {
                System.out.println("args[" + i + "] = " + args[i]);
            }
        }
    }

    Here are a couple of runs. (Computer output is blue, my input is black)

    $ java Z
    Number of arguments = 0


    And

    $ java Z The quality of mercy is not strained
    Number of arguments = 7
    args[0] = The
    args[1] = quality
    args[2] = of
    args[3] = mercy
    args[4] = is
    args[5] = not
    args[6] = strained



    So, get the file names from the args[] array that is built by the command line.


    Cheers!

    Z

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ubiByte (October 19th, 2012)

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Copying a File

    Quote Originally Posted by Zaphod_b View Post
    See the section of Java tutorials for Command-line Arguments

    Try the following to test it:
    public class Z {
        public static void main(String [] args) {
            System.out.println("Number of arguments = " + args.length);
            for (int i = 0; i < args.length; i++) {
                System.out.println("args[" + i + "] = " + args[i]);
            }
        }
    }

    Here are a couple of runs. (Computer output is blue, my input is black)

    $ java Z
    Number of arguments = 0


    And

    $ java Z The quality of mercy is not strained
    Number of arguments = 7
    args[0] = The
    args[1] = quality
    args[2] = of
    args[3] = mercy
    args[4] = is
    args[5] = not
    args[6] = strained



    So, get the file names from the args[] array that is built by the command line.


    Cheers!

    Z
    Hmm so the code I have provided how can I put srcFileName into args[0] for example without creating a constructor. Because if i create a constructor it has already copied the file.

  6. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Copying a File

    Did you read the tutorial reference that I gave? Explains about how use the args array to read Strings from the command line?

    Your constructor takes two arguments. Instead of hard-coding the file names in main(), test to see that args.length is equal to 2. If it is, then use the value of args[0] and args[1] as arguments to the constructor.


    Cheers!

    Z

  7. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Copying a File

    Yea I am looking through the reference I tried to code you sent me also and when I type in the same code you typed in the command prompt it says couldn't find the main class do you know why that's happening?

  8. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Copying a File

    Since my code had the public static void main() method in a public class named Z, it has to be in a file named Z.java to make the java compiler happy.

    I have done this kind of thing so many times that sometimes I forget to give complete instructions. I mean, "easy" as it seems to some people, no one was born knowing this stuff, right?

    So...however it is that you build your projects, just create a new project and try the main() that I showed. Play with it until you see exactly what it is doing with the args[] array of Strings.

    Then go back to your main() and use args[0] and args[1] in your constructor.

    Cheers!

    Z
    Last edited by Zaphod_b; October 19th, 2012 at 03:52 PM.

  9. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Copying a File

    public static void main(String[] args) throws Exception
    {
    BufferedReader reader = new BufferedReader(new FileReader(args[0]));
    BufferedWriter writer = new BufferedWriter(new PrintWriter(args[1]));

    int index;
    do
    {
    index = reader.read();
    if (index != -1)
    {
    writer.write((char) index);
    }
    }
    while (index != -1);
    reader.close();
    writer.close();
    }
    this works when i do java FileCopy input.txt out.txt, how can I put in the word "to" in between so the command reads:
    java FileCopy input.txt to out.txt?

  10. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Copying a File

    The reason that I wrote a completely separate test program is so that you can play around with whatever the heck command line stuff you want. I mean, it's always OK to ask, but wouldn't it save you some time if you tried things rather than posting a question on a forum and waiting and hoping for a meaningful and timely response? I mean there are no guarantees that you will get a forum response at all. Then what?

    Anyhow...

    The program lists all of the members of the args[] array that came from the command line and it shows what each one's index is. So if you tried it with the command line that you suggest, what does it print?

    What is args[0]?
    What is args[1]?
    What is args[2]?


    Cheers!

    Z
    Last edited by Zaphod_b; October 19th, 2012 at 04:55 PM.

  11. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Copying a File

    Okay it's finally working correctly. Thank you so much for helping me understand this and guiding me through the problem Z.
    Last edited by ubiByte; October 19th, 2012 at 05:50 PM.

  12. #11
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Copying a File

    Quote Originally Posted by ubiByte View Post
    Okay it's finally working correctly.
    Thank you for the feedback.


    Quote Originally Posted by ubiByte
    Thank you...
    It was my pleasure.

    And now: Onward and upward!


    Cheers,

    Z
    Last edited by Zaphod_b; October 19th, 2012 at 07:12 PM.

Similar Threads

  1. Help me with directory copying code.
    By fredsilvester93 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 2nd, 2012, 07:09 AM
  2. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 03:41 AM
  3. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 10:04 PM
  4. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM
  5. need help copying (what is in address bar)
    By 3ammary in forum Java Networking
    Replies: 0
    Last Post: January 8th, 2011, 09:32 AM