LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2007, 12:45 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
Java and ls


Hi there
I have a directory with lot of files without any sub-folders, and i want to process in an easy was all the files in that folder. Can anyone suggest a way to do that in java. What ideally I want is a method like ls.

Thanks
 
Old 11-14-2007, 03:07 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

http://java.sun.com/j2se/1.5.0/docs/...a/io/File.html
says to me that File.listFiles() will return an array of all the files in a directory, e.g.
Code:
File mydir = new File("/path/to/the/directory");
String[] contents = mydir.listFiles();
Dave
 
Old 11-15-2007, 11:24 AM   #3
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Hi again and thanks for your reply.
I would like for a second time to ask what can i do for getting the current path of the folder that I compile my Java files.

Thanks
 
Old 11-16-2007, 01:03 PM   #4
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
You mean your current directory?

import java.io.File;


File current = new File (System.getProperty("user.dir"));

You can test with
System.out.println(file);

It's also a good idea to build in some extra code to verify whether your app isn't erroneously pointing to a non-existent path:

if (!current.exists()){
System.out.println("Directory not found");
System.exit(1);
}

will bring the application down without any ugly error messages if it fails to locate the required directory. You could also throw a FileNotFoundException but throwing exceptions tends to kill performance so the first approach should be preferred in most cases.

Last edited by jay73; 11-16-2007 at 01:05 PM.
 
Old 11-16-2007, 05:48 PM   #5
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Dear jay73

Thanks for your reply. I strongly believe that you can advise me on the following. Setting the case that there is a C program that can be executed with a Java Runtime command and its output in my case actually an Image, will be redirected back to the Java Program something like a scrollable Frame. Could you or anyone please recommend a way of doing that. I would also like to know if it is efficient.

Thanks
 
Old 11-16-2007, 07:04 PM   #6
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
So if I understand correctly, you want your application to call native i.e. compiled C code? If so, yes, that is possible through JNI. But it's not something that I would encourage you to dabble in while you're still learning the basics. For one thing because it adds a whole new level of complexity to the extent that you also need to be familiar with the fundamentals of C coding. For another because it can have some undesired restrictions. Applets, for example, can't access C code for security reasons. And bundling your applications with platform dependent calls inevitably means that you'll break portability; you would then need to recompile (and possibly rework) your C code for every targeted operating system.

Frankly, I'd much rather write a 100% java application if all I wanted to do is load an image. I remember writing a picture viewer on one occasion that was less than 100 lines of code. Sure, it was rather primitive but more than good enough. I think you may want to have a look at Java Swing if your need to learn about GUIs, buttons, icons, text and password fields, menus, scrollbars, what not.
 
Old 11-17-2007, 12:19 PM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Quote:
Originally Posted by manolakis View Post
Dear jay73

Thanks for your reply. I strongly believe that you can advise me on the following. Setting the case that there is a C program that can be executed with a Java Runtime command and its output in my case actually an Image, will be redirected back to the Java Program something like a scrollable Frame. Could you or anyone please recommend a way of doing that. I would also like to know if it is efficient.

Thanks

Java can display pictures (not all formats) itself.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
LXer: Java Look-Alike Project Pushed Sun To Make Java Open Source LXer Syndicated Linux News 0 11-15-2006 03:54 AM
LXer: Java news met with cautious optimism in free Java community LXer Syndicated Linux News 0 11-14-2006 10:21 PM
Firefox refuses to load Java jnlp files - plugin and java ok Melsync Linux - Software 1 06-25-2006 04:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration