Solved

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

Posted on 2006-07-17
9
218 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now