Results 1 to 20 of 20

Thread: root directory

  1. #1
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Question root directory

    Hi all,
    MY NEW YEAR GREETINGS:)

    I am writing code to search a file for that i need to display the path of the file where it is located
    Take a look at my code
    DeterminePath.java


    import java.io.File.*;
    import java.io.*;


    public class DeterminePath
    {
    private static void dirlist(String fname)
    {
    File fileName = new File(fname);
    String fName = fileName.getName();
    String parentPath = fileName.getPath();
    File parentDir = fileName.getParentFile();
    String path = fileName.getAbsolutePath();
    File[] root = fileName.listRoots();
    String currentdir = System.getProperty("user.dir");
    System.out.println("Original Filename = " + fName);
    System.out.println("Parent Path = " + parentPath);
    System.out.println("Parent Directory = " + parentDir);
    System.out.println("Root Dir = " + path);
    System.out.println("Currentdir = " + currentdir);
    System.out.println("roots are :");
    for (int i=0;i<root.length ;i++ )
    {
    System.out.println("root= " + root[i]);
    }

    }

    public static void main(String args[])throws IOException
    {
    System.out.println("Enter FileName");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String fname = br.readLine();

    dirlist(fname);
    }
    }


    It is retrieving the path of the file DeterminePath.java but i need to retrieve the path of the file stored in the variable fname
    how can i do this please help me on this regard
    Thanks,
    raghu

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    listRoots() is static method, File.listRoots() is ok

    i suggest to get the root directory by File.listRoots()
    and try to search all folder and compare the file names
    after you match the file name and type, you create a File object and get its path

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

  4. #4
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    How about the getAbsolutePath()
    __________________
    What i did not understand?????

  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

    Using that you can find the complete path of the specified file. That's what you want to find, if I'm correctly get you.

  6. #6
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default reply

    exactly!!!!!!
    I want to do the same
    And now I have one more issue
    As I am developing a client server architecture I want run the program from the client on server capture the output and send it to the client
    my code is working fine with the first part that is i am able to get the file from the client and then compile and run on server
    Now i have to capture the output and send it to the client
    how can i do that dynamically
    i.e output of every program must be sent to the client
    as far as my knowledge goes i am thinking of storing the output in a temp file and then send it to the client is it possible if so how
    Please Don't mind i am very much confused as i am new to java



  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you dont know the location of file, you cant create a correct File instant and getAbsolutePath()
    Last edited by mtyoung; 01-03-2009 at 03:25 AM.

  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

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    so... accounting to my suggestion in above, you may need to loop for all File.listRoots() to find out the file(s),
    then create a correct File object
    Last edited by mtyoung; 01-03-2009 at 07:17 AM.

  10. #10
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Thanks,

    Thanks to this forum.Your answers are very much helpful to me
    I am able get roots using File.listRoots
    now i have another issue in front of me
    i am trying to capture the output of a java program generated at the command prompt directly into a temp file for this i am using Dataoutputstream using this i can write the data into a file which is hard coded int the program but how to get the dynamically into the file
    Please Help

    thanks
    raghu

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

  12. #12
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Hi,
    hard code in the sense i have directly given the text to be written in the file i.e everytime i run my code the text "hello sir"
    will be written into the file but i need to write the text dynamically i.e ,the text generated at the command prompt has to be directly written into the file
    Can I do that ???
    Thanks,
    raghu

  13. #13
    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

    Yes you can. Using scanner read the text from the command prompt and write them into the text file.

  14. #14
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Thanks,
    Can't we do this without using scanner
    Pardon my ignorance!!!!!!
    i'm just curious to know is there any logic other than using scanner
    Thanks,
    raghu

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

  16. #16
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    ok
    sir, thanks for spending your valuable time for me


    Thanks
    raghu

  17. #17
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    output of a java program generated
    output of a java program generated?
    is this java program code by you?
    if yes, you can modify then program to output thing to System.out and fileWriter...

  18. #18
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    output of a java program generated?
    is this java program code by you?
    if yes, you can modify then program to output thing to System.out and fileWriter...

    actually my code is

    import java.io.*;

    public class FileOutput
    {

    public static void main(String[] args)
    {

    FileOutputStream fos;
    DataOutputStream dos;
    try
    {

    File file= new File("D:\\file\\new1.java");
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    dos.writeChars("\n Hello sir");

    } catch (IOException e)
    {
    e.printStackTrace();
    }

    }

    }
    here i can write the string "Hello sir" into the file but i need to capture the output dynamically


    thanks,
    raghu

  19. #19
    raghu9198's Avatar
    raghu9198 is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Dear Eranga,
    As you said i have used scanner but it is not retrieving the file take a look at my code


    import java.lang.Compiler;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    class Server
    {

    public static void main(String[] args) throws Exception
    {
    Socket s;
    ServerSocket ss=new ServerSocket(3000);
    Thread mt;
    while(true)
    {
    System.out.println("server is waiting....");
    s=ss.accept();
    System.out.println("Client is connected....");
    mt=new Thread(new MyThread(s));
    mt.start();
    System.out.println("Thread started....");
    }
    }
    }
    class MyThread extends JFrame implements Runnable,ActionListener
    {
    Socket s;
    Container c=getContentPane();
    Container c1=getContentPane();
    JLabel l1,l2;
    JTextArea text1,text2;
    JButton compile,execute;
    InputStream is;
    DataInputStream dis;
    String fname,name;
    File f;
    FileOutputStream os;
    MyThread(Socket s)
    {
    this.s=s;
    }
    public void run()
    {
    this.c1.setLayout(new FlowLayout());
    l1 = new JLabel();
    l1.setText("you have received a file:");
    text1 = new JTextArea(2,20);
    //save = new JButton("save");
    compile = new JButton("compile");
    execute = new JButton("execute");
    c1.add(l1);
    c1.add(text1);
    //c1.add(save);
    c1.add(compile);
    c1.add(execute);
    //save.addActionListener(this);
    compile.addActionListener(this);
    execute.addActionListener(this);
    setSize(400,400);
    setVisible(true);
    setTitle("File Sharing");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try
    {
    is=s.getInputStream();
    dis=new DataInputStream(s.getInputStream());
    fname=dis.readUTF();
    text1.append(fname);
    System.out.println("file name received");

    // text1.replaceRange(" ",0,fname.length());
    f=new File(fname);
    name=f.getName();
    System.out.println("file name "+name);
    f=new File("d:/s/invoke/"+name);
    os=new FileOutputStream(f);

    int i;
    while((i=is.read())!=-1)
    {
    os.write(i);
    }


    dis.close();
    os.close();

    }
    catch (Exception e)
    {
    }
    }
    public void actionPerformed(ActionEvent ae)
    {

    if(ae.getActionCommand()=="compile")
    {

    try
    {
    String[] commands = {"cmd", "/c", "start", "compile.bat",name};
    Runtime.getRuntime().exec(commands);

    }
    catch(Exception e)
    {
    System.out.println("Error");
    }
    System.out.println("compiled");

    }
    if(ae.getActionCommand()=="execute")
    {
    String ext =name.substring(0,name.indexOf("."));
    System.out.println(ext);

    try
    {
    String[] commands = {"cmd", "/c", "start", "execute.bat",ext};
    Runtime.getRuntime().exec(commands);

    Scanner sc =new Scanner(System.in);
    String s=sc.next();
    System.out.println(s);


    }
    catch(Exception e)
    {
    System.out.println("Error");
    }
    System.out.println("executed");
    }
    }
    }


    Thanks,
    raghu

  20. #20
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    browsing the net, find this
    When Runtime.exec() won't - JavaWorld

    and you may read the following codes
    program get the inputStream of process and output to System.out
    Java Code:
    public static void main(String args[])
    	{
    		try
    		{
    			String[] commands = {"rar", "a", "-agyyyymmdd_hhmm(a)", "-m5", "-msjpg", "f:\\T src"};
    			
    			Runtime rt = Runtime.getRuntime();
    			Process proc = rt.exec(commands);
    			InputStream stderr = proc.getInputStream();
    			InputStreamReader isr = new InputStreamReader(stderr);
    			BufferedReader br = new BufferedReader(isr);
    			String line = null;
    			System.out.println("<output>");
    			while ((line = br.readLine()) != null)
    				System.out.println(line);
    			System.out.println("</output>");
    			int exitVal = proc.waitFor();
    			System.out.println("Process exitValue: " + exitVal);
    		
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    			System.out.println("Error");
    		}
    		System.out.println("compiled");
    	}

Similar Threads

  1. Listing file system from root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 04-18-2009, 11:03 AM
  2. JSP/Servlet getting root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-06-2009, 09:51 AM
  3. Find nth root of a number
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-03-2008, 07:51 AM
  4. Root Finder for polynomials
    By gibsonrocker800 in forum Advanced Java
    Replies: 11
    Last Post: 01-04-2008, 12:26 AM
  5. Context Root change
    By rvasu_77 in forum Advanced Java
    Replies: 2
    Last Post: 07-12-2007, 11:18 AM

Posting Permissions

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