Solved

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

Posted on 2006-07-17
9
223 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
  • 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
Technology Partners: 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!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: anonymous class 4 37
Need Help! Getting a syntax error and don't understand why 3 38
Java Eclipse Loop 3 29
Java pass by reference 3 13
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

740 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