Results 1 to 8 of 8
  1. #1
    flaca is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default changing current directory

    hi,

    need help here and new to Java.

    i need to change my current directory to another path using java code.
    how can i do this.

    i know how to find out what my current path is but having trouble changing the path to another directory

    thanks

  2. #2
    mrcheeks is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default

    Use System.getProperty("user.dir"); and System.setProperty("user.dir", "mydirectoryPath");

  3. #3
    flaca is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default

    works. thank you

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    How were you able to demonstrate that the current directory was changed?
    By changing the current directory I understand you to mean that the OS changes the location where you would write a file without a path. I wrote a test program to see and it looks like the OS has NOT changed the current directory. Java has changed a value in its properties table but left the OS alone.
    Java Code:
    //WriteData.java
    // Test writing some primitives to disk
    
    import java.io.*;
    
    public class WriteData2 {
    	public static void main(String[] args) {
            System.out.println("CD=" + System.getProperty("user.dir"));
            System.setProperty("user.dir", "D:/JavaDevelopment/Testing/xxx");  // change CD
            System.out.println("CD=" + System.getProperty("user.dir"));
            try {
                DataOutputStream dos = new DataOutputStream(
                                new FileOutputStream("WriteData2.dat"));
                dos.writeFloat(1.0F);        // 4
                dos.close();
    
                // Now test where file is
                File testFile = new File("WriteData2.dat");
                System.out.println("testFile=" + testFile. getAbsolutePath());
                testFile = new File("TestReading.txt"); // this is in xxx
                System.out.println("testFile=" + testFile. getAbsolutePath());
                // Now try to read it
                FileInputStream fis = new FileInputStream(testFile);
                byte[] buf = new byte[10];
                fis.read(buf);
                fis.close();
                for(int i=0; i < buf.length; i++) {
                   System.out.print(buf[i] + " ");
                }
                System.out.println();
            }catch(Exception ex) {
                System.out.println("Got ex " + ex);
            }
    	}// end main()
    }/* Output
    Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment\Testing\;D:\JavaDevelopment;. WriteData2
    
    Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment\Testing\;D:\JavaDevelopment;. WriteData2
    
    CD=D:\JavaDevelopment\Testing
    CD=D:/JavaDevelopment/Testing/xxx
    testFile=D:\JavaDevelopment\Testing\xxx\WriteData2.dat	 <<<< NOT THERE, its in Testing
    testFile=D:\JavaDevelopment\Testing\xxx\TestReading.txt	 <<< This is where it is, BUT see next line
    Got ex java.io.FileNotFoundException: TestReading.txt (The system cannot find the file specified)
    
    0 error(s)
    */

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Better to mark this thread if you solve the problem flace. It's really helpful to all other members.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    I don't think he's solved it yet. Try this and see what happens:

    System.setProperty("user.dir", "Hello from Mars"); // change CD
    Last edited by Norm; 08-16-2008 at 03:41 PM.

  7. #7
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    I don't think we can change the current directory.
    This post might be of help -
    Java Applications and the "Current Directory"

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 02:13 AM
  2. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:14 AM
  3. Current month
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:40 AM
  4. How to get the current working directory in EJB?
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-19-2007, 04:24 PM
  5. How can i get current directory?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •