Link to home
Start Free TrialLog in
Avatar of bryanberg
bryanberg

asked on

java program to browse files on windows for specific file type, then save path

Hello

Im trying to write a java program which will browse files on windows for a specific file type and then save the path

Ive done something like this using jsp

Thank you

thank you very much

Bryan
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Or are you bent on making your own file-chooser ;-) ?
What do you mean by "browse"?  If you mean like swing's JFileChooser, then just use previous posts (don't try to reinvent the wheel)!

If you mean more like MSWindows search, then maybe something like this:

String pattern;
FileFilter filter=new FileFilter() {

      public boolean accept(File pathname) {
            return pathname.getName().matches(pattern);
      }
};
public List SearchFiles(String pattern, File start)
{
      if (pattern!=null) {
            this.pattern=pattern;
      }
      File[] dirContents=start.listFiles(filter);
      List contents=new ArrayList(dirContents.length);
      for (int i = 0; i < dirContents.length; i++) {
            File eachFile=dirContents[i];
            if (eachFile.isDirectory()) {
                  contents.addAll(SearchFiles(null, eachFile);
            } else
                  contents.add(eachFile);
      }
      return dirContents;
}
Avatar of bryanberg

ASKER

I just want a button on a form that when you click it a file open window comes up with only .sqt files showing

Im trying to add this functionality to an existing program which already does stuff when a button is clicked.

I can change the jpg/gif filter to sqt once the code works but now im still getting an error

Im trying to use the snippet from

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html

I get an error from the line:

    int returnVal = chooser.showOpenDialog(parent);

Im guessing Im supposed to change parent to something else but im not sure what

Ive seen it as myWindow, and tried that, also tried using Hello.this, since thats the name of the class



import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
 import java.awt.event.*;
import javax.swing.filechooser.*;
//import ExampleFileFilter;


public class hello

{
  public static void main( String[] args ) {
try{
      //Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect.bat");  
      //final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");  
      //Process p = Runtime.getRuntime().exec("dir");

//Process p = Runtime.getRuntime().exec();



       //javax.swing.JTextArea textArea;

      //java.io.InputStream in = DTA.getInputStream();
      //java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream

//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
    //textArea.append(r + "\r\n");

      JFrame frame = new JFrame( "HelloJava" );
      JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
      JButton button = new JButton("DTA");
      button.setSize( 50, 50 );


frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      final JTextArea area = new JTextArea( );
        area.setText("Howdy\n");

 
button.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent ae) {
try{
      final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");

    JFileChooser chooser = new JFileChooser();
     // Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.addExtension("gif");
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);

//ERROR IN LINE BELOW WITH HELLO.THIS
    int returnVal = chooser.showOpenDialog(hello.this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());

 }


}

 
catch(Exception e) {
      System.out.println("no good");
    }


} });




//java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream


//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);

//javax.swing.JTextArea textArea;
//textArea.append("hello");
Container c = frame.getContentPane( );
    c.add( label,BorderLayout.NORTH);
    c.add( area,BorderLayout.CENTER);
    c.add( button,BorderLayout.SOUTH);      
      frame.setSize( 300, 300 );
    frame.setVisible( true );
  }
catch(Exception e) {
      System.out.println("no good");
    }

}}
o yeah and thanks for help

: >
whats the error?
>>  int returnVal = chooser.showOpenDialog(hello.this);

Use:  int returnVal = chooser.showOpenDialog ( frame ) ;
I changed the value to frame and now it compiles !
C:\j2sdk1.4.2_05\bin\javac -classpath C:\j2sdk1.4.2_05\dem
o\jfc\FileChooserDemo\FileChooserDemo.jar hello.java

but when i try and run the java program i still ge the error:

C:\DTASelect\test_run>C:\j2sdk1.4.2_05
Exception in thread "main" java.lang.NoClassDefFoundError: ExampleFileFilter
        at hello.main(hello.java:43).

ExampleFileFilter class is in the jar file FileChooserDemo.Jar

do i need to do another import statement ?

below is my code:

import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
 import java.awt.event.*;
import javax.swing.filechooser.*;
//import FileChooserDemo.*;


public class hello

{
  public static void main( String[] args ) {
try{
      //Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect.bat");  
      //final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");  
      //Process p = Runtime.getRuntime().exec("dir");

//Process p = Runtime.getRuntime().exec();



       //javax.swing.JTextArea textArea;

      //java.io.InputStream in = DTA.getInputStream();
      //java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream

//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
    //textArea.append(r + "\r\n");

      final JFrame frame = new JFrame( "HelloJava" );
      JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
      JButton button = new JButton("DTA");
      button.setSize( 50, 50 );


frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      final JTextArea area = new JTextArea( );
        area.setText("Howdy\n");

 
button.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent ae) {
try{
      final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");

   JFileChooser chooser = new JFileChooser();
     // Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.addExtension("gif");
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);
    //int returnVal = chooser.showOpenDialog(hello.this);
    int returnVal = chooser.showOpenDialog ( frame ) ;

    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());

 }


}

 
catch(Exception e) {
      System.out.println("no good");
    }


} });




//java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream


//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);

//javax.swing.JTextArea textArea;
//textArea.append("hello");
Container c = frame.getContentPane( );
    c.add( label,BorderLayout.NORTH);
    c.add( area,BorderLayout.CENTER);
    c.add( button,BorderLayout.SOUTH);      
      frame.setSize( 300, 300 );
    frame.setVisible( true );
  }
catch(Exception e) {
      System.out.println("no good");
    }

}}

what was the command u used to run it, it appears it got truncated above
ok so what ive got works ...  : )


C:\DTASelect\test_run>java -classpath C:\j2sdk1.4.2_05\demo\jfc\FileChooserDemo\
FileChooserDemo.jar;C:\DTASelect\test_run hello

as long as i include the paths to both the jar file AND the hello.class file

when i try using the set classpath line the examplefilefilter is not found

C:\DTASelect\test_run>set CLASSPATH = %CLASSPATH%;.;C:\j2sdk1.4.2_05\demo\jfc\Fi
leChooserDemo\FileChooserDemo.jar;.;%cd%

maybe im typing something wrong

any ideas as to how to find what value the classpath currently holds ?  and/or reset it to be the way its supposed to be

needless to say

im almost there !

all's i really need now is to return the path of the file selected

ive changed the files selected to .sqt

and

the program returns the pathname including the file selected:

       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getPath());

i just don't want the filename in there

but im almost there      & ^  )

thanks !

b
> any ideas as to how to find what value the classpath currently holds ?

echo %CLASSPATH%

or

set CLASSPATH

> i just don't want the filename in there

if you just want the parent directory, use the File's getParent() method.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial