AmolDesai23
asked on
Sample code to access a file on Unix from a Java program on windows
Hello Experts,
This i guess a pretty simple question for you all.
Can anybody provide me sample java code to locate a folder (say, "test") and locate a ".txt" file in it on Unix and display its path? Java program will be written in Windows and then ported on to Unix box.
Thanks for your help.
This i guess a pretty simple question for you all.
Can anybody provide me sample java code to locate a folder (say, "test") and locate a ".txt" file in it on Unix and display its path? Java program will be written in Windows and then ported on to Unix box.
Thanks for your help.
What do you mean by 'locate'?
This should work:
File file = new File("test");
String[] list = file.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWit h(".txt");
}
});
File file = new File("test");
String[] list = file.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWit
}
});
ASKER
Thanks Ajay... let me try this out.
Shortly ... you cannot do that.
If no bridge between Windows and Unix File System is installed, you have no way of doing that, because Windows FS is not compatible with Unix FS. You would need some bridging functionality installed on either the Unix machine (Samba for example) or on Windows (for example some SFTP implementation).
If no bridge between Windows and Unix File System is installed, you have no way of doing that, because Windows FS is not compatible with Unix FS. You would need some bridging functionality installed on either the Unix machine (Samba for example) or on Windows (for example some SFTP implementation).
AmolDesai, this sample java code should work for you....
public static void main (String args[]) {
String s1 = "/usr/bin/find /export -type d -name testDir\r\n";
String result = findResult(s1);
if(result != null && result.length() != 0)
{
System.out.println(result) ;
String s2 = "/usr/bin/find " + result + "/ -type f -name fileName.txt\r\n";
result = findResult(s2);
if(result != null && result.length() != 0)
System.out.println(result) ;
}
}
public static String findResult(String str)
{
String line = null;
try{
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec(str);
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStream Reader);
while ( (line = bufferedReader.readLine()) != null ){
if( !line.endsWith("Permission denied") )
break;
}
}catch (Throwable t) {
t.printStackTrace();
}
return line;
}
public static void main (String args[]) {
String s1 = "/usr/bin/find /export -type d -name testDir\r\n";
String result = findResult(s1);
if(result != null && result.length() != 0)
{
System.out.println(result)
String s2 = "/usr/bin/find " + result + "/ -type f -name fileName.txt\r\n";
result = findResult(s2);
if(result != null && result.length() != 0)
System.out.println(result)
}
}
public static String findResult(String str)
{
String line = null;
try{
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec(str);
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStream
while ( (line = bufferedReader.readLine())
if( !line.endsWith("Permission
break;
}
}catch (Throwable t) {
t.printStackTrace();
}
return line;
}
ASKER
Owenli,
Can you plz explain me this command: "/usr/bin/find /export -type d -name testDir?
thanks for your help.
Can you plz explain me this command: "/usr/bin/find /export -type d -name testDir?
thanks for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Shortly ... you cannot do that.
You can - instead of running find command (or any other os dependent command), you can use list() function on the file. But you can write a recursive function to walk through the directory tree.
You can - instead of running find command (or any other os dependent command), you can use list() function on the file. But you can write a recursive function to walk through the directory tree.
ASKER
Thanks Owenli and Ajay.