Results 1 to 20 of 20
Thread: root directory
- 01-02-2009, 11:09 AM #1
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
- 01-02-2009, 11:44 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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
- 01-02-2009, 11:57 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How about the getAbsolutePath()
- 01-02-2009, 11:58 AM #4
How about the getAbsolutePath()
__________________
What i did not understand?????
- 01-02-2009, 12:28 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-02-2009, 01:08 PM #6
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
- 01-03-2009, 01:36 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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 02:25 AM.
- 01-03-2009, 04:22 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-03-2009, 05:37 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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 objectLast edited by mtyoung; 01-03-2009 at 06:17 AM.
- 01-03-2009, 06:46 AM #10
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
- 01-03-2009, 08:11 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean by hard coded int? What you want to write into the file?
- 01-03-2009, 08:20 AM #12
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
- 01-03-2009, 08:29 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes you can. Using scanner read the text from the command prompt and write them into the text file.
- 01-03-2009, 08:44 AM #14
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
- 01-03-2009, 08:46 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Other than scanner I don't know any way to read the command prompt.
- 01-03-2009, 08:49 AM #16
ok
sir, thanks for spending your valuable time for me
Thanks
raghu
- 01-03-2009, 10:35 AM #17
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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...
- 01-03-2009, 11:10 AM #18
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
- 01-03-2009, 12:18 PM #19
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
- 01-06-2009, 03:11 AM #20
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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
-
Listing file system from root directory
By Java Tip in forum Java TipReplies: 1Last Post: 04-18-2009, 10:03 AM -
JSP/Servlet – getting root directory
By Java Tip in forum Java TipReplies: 1Last Post: 01-06-2009, 08:51 AM -
Find nth root of a number
By perito in forum New To JavaReplies: 1Last Post: 03-03-2008, 06:51 AM -
Root Finder for polynomials
By gibsonrocker800 in forum Advanced JavaReplies: 11Last Post: 01-03-2008, 11:26 PM -
Context Root change
By rvasu_77 in forum Advanced JavaReplies: 2Last Post: 07-12-2007, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks