Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-07-17
9
Medium Priority
?
228 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
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!

 
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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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