Link to home
Start Free TrialLog in
Avatar of AmolDesai23
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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What do you mean by 'locate'?
Avatar of Ajay-Singh
Ajay-Singh

This should work:


        File file = new File("test");
        String[] list = file.list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".txt");
            }
        });
Avatar of AmolDesai23

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).
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(inputStreamReader);

               while ( (line = bufferedReader.readLine()) != null ){
                    if( !line.endsWith("Permission denied") )
                            break;
                }

             }catch (Throwable t) {
                   t.printStackTrace();
             }

            return line;
      }
Owenli,

Can you plz explain me this command: "/usr/bin/find /export -type d -name testDir?

thanks for your help.
ASKER CERTIFIED SOLUTION
Avatar of Weiping Du
Weiping Du
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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.
Thanks Owenli and Ajay.