Solved

Sample code to access a file on Unix from a Java program on windows

Posted on 2006-07-17
9
225 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:AmolDesai23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17123109
What do you mean by 'locate'?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17123129
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");
            }
        });
0
 

Author Comment

by:AmolDesai23
ID: 17123146
Thanks Ajay... let me try this out.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 12

Expert Comment

by:enachemc
ID: 17123209
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).
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17124647
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;
      }
0
 

Author Comment

by:AmolDesai23
ID: 17125748
Owenli,

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

thanks for your help.
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 125 total points
ID: 17126696
/usr/bin/find --  find is a binary utility and usually located /bin,  /usr/bin or /usr/sbin folder. It is used to search the entire filesystem for one or more files that you specify to look for. This is very helpful when a file has been "lost".

/export -- is an examle I gave you, it means I want to do a search only inside /export or its sub directory.  if you use "/" it means search a file from root of filesystem, will search every folder or sub folder. It cost your time and performance.
 
-type d -- search a directory name only(-type f means search for a file only)

-name testDir -- a filename or folder name you want to search
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17127372
> 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.
0
 

Author Comment

by:AmolDesai23
ID: 17129585
Thanks Owenli and Ajay.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question