Jump to content

How can I open a folder ?

- - - - -

  • Please log in to reply
11 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
I got a folder of files that I want to open. How can i modify my code so that it can open up the folder instead of the file one by one ?

JFileChooser chooser = new JFileChooser();

        chooser.setCurrentDirectory(new File("."));

        chooser.setDialogTitle("Select configuration file");

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        chooser.setAcceptAllFileFilterUsed(false);


        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)

        {

            File f = new File(chooser.getSelectedFile().getAbsolutePath());

            if (f.exists())

            {

                LoadSMILEFile(chooser.getSelectedFile().getAbsolutePath());

                JOptionPane.showMessageDialog(null, "Database Files loaded", "Loaded", JOptionPane.INFORMATION_MESSAGE);

            }

            else

            {

                JOptionPane.showMessageDialog(null, "File does not exists", "Error", JOptionPane.ERROR_MESSAGE);

            }

            File file = chooser.getSelectedFile();

            String textFileContents = null;

            try {

                textFileContents = readFileAsString(file);

                selectedFile = file;

            } catch (FileNotFoundException ex) {

                Logger.getLogger(FYP_CDKView.class.getName()).log(Level.SEVERE, null, ex);

            } catch (IOException ex) {

                Logger.getLogger(FYP_CDKView.class.getName()).log(Level.SEVERE, null, ex);

            }


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
do chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
instead of Files_ONLY

The chosen file will now be a folder and not a file. To get all files form the folder:

File f = new File(chooser.getSelectedFile().getAbsolutePath());

if (f.exists()) {

   for(File file : f.listFiles() ){

          //do something with each file.

   }

}



#3
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
I had use your method... It works but however when I click my convert button for the file to convert into the stuff i want, there's errors ..

 private void OpenBaseActionPerformed(java.awt.event.ActionEvent evt) {                                         

        JFileChooser chooser = new JFileChooser();

        chooser.setCurrentDirectory(new File("."));

        chooser.setDialogTitle("Select configuration file");

        chooser.addChoosableFileFilter(new FileNameExtensionFilter("CSV file", "csv"));

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        chooser.setAcceptAllFileFilterUsed(false);

        


        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)

        {

            File f = new File(chooser.getSelectedFile().getAbsolutePath());

        if (f.exists()) {

             for(File file : f.listFiles() )

            {

                LoadSMILEFile(chooser.getSelectedFile().getAbsolutePath());

            }

               JOptionPane.showMessageDialog(null, "Database Files loaded", "Loaded", JOptionPane.INFORMATION_MESSAGE);

            }

            else

            {

                JOptionPane.showMessageDialog(null, "File does not exists", "Error", JOptionPane.ERROR_MESSAGE);

            }

             File file = chooser.getSelectedFile();

             selectedFile = file;

             Database.setText(file.getAbsolutePath());


    

        }


    }             

 private void getDatabaseFingerprintcsv(String DatabaseFingerprint) throws IOException {

         BufferedReader br = null;

          FileWriter writer = new FileWriter(directory);

          File dir = new File("C:\\Users\\david\\Downloads\\Desktop\\koi");

          File[] files = dir.listFiles();

          File outputFile = new File(directory);

          PrintStream output = new PrintStream(outputFile);




          try {


			IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();

			FileInputStream input = new FileInputStream(selectedFile);

			ISimpleChemObjectReader iSimpleChemObjectReader = new ReaderFactory().createReader(input);

			IChemFile content = (IChemFile) iSimpleChemObjectReader.read(builder.newChemFile());

			List<IAtomContainer> container = ChemFileManipulator.getAllAtomContainers(content);

			BitSet fingerprint = new Fingerprinter().getFingerprint(container.get(0));


                        writer.append("Name");


                        for( int i=1; i<1025;i++){

                            writer.append(",");

                            writer.append("fp" + i);

                        }


                       for (File moleculeFile : dir.listFiles()) {

	                            writer.append("\n");

	                            writer.append(moleculeFile.getName());

                                    writer.append(",");


                        for ( int i=0; i<fingerprint.size(); i++) {

				if (i > 0) {

					writer.append(",");

                                        writer.flush();

				}


				if (fingerprint.get(i)) {

					writer.append("1");

                                        writer.flush();

				} else {

					writer.append("0");

                                        writer.flush();

				}

                       }


                       }


		} catch (Exception e) {

			e.printStackTrace();

		}



          }

    }


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Copy paste the errors here please.

At first sight you should change

LoadSMILEFile(chooser.getSelectedFile().getAbsolutePath());

into
LoadSMILEFile(file.getAbsolutePath());
at the foreach loop.

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
   at java.io.FileInputStream.open(Native Method)

        at java.io.FileInputStream.<init>(FileInputStream.java:106)

        at fyp_cdk.FYP_CDKView.getDatabaseFingerprintcsv(FYP_CDKView.java:1066)

        at fyp_cdk.FYP_CDKView.ConvertDb2FpActionPerformed(FYP_CDKView.java:876)

        at fyp_cdk.FYP_CDKView.access$1300(FYP_CDKView.java:50)

        at fyp_cdk.FYP_CDKView$9.actionPerformed(FYP_CDKView.java:440)

        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

        at java.awt.Component.processMouseEvent(Component.java:6289)

        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

        at java.awt.Component.processEvent(Component.java:6054)

        at java.awt.Container.processEvent(Container.java:2041)

        at java.awt.Component.dispatchEventImpl(Component.java:4652)

        at java.awt.Container.dispatchEventImpl(Container.java:2099)

        at java.awt.Component.dispatchEvent(Component.java:4482)

        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)

        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

        at java.awt.Container.dispatchEventImpl(Container.java:2085)

        at java.awt.Window.dispatchEventImpl(Window.java:2478)

        at java.awt.Component.dispatchEvent(Component.java:4482)

        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)

        at java.awt.EventQueue.access$000(EventQueue.java:85)

        at java.awt.EventQueue$1.run(EventQueue.java:603)

        at java.awt.EventQueue$1.run(EventQueue.java:601)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)

        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)

        at java.awt.EventQueue$2.run(EventQueue.java:617)

        at java.awt.EventQueue$2.run(EventQueue.java:615)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)

        at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)

        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
1 line above the stuff you've pasted should be an important word
XxxException.
Like NullPointerException or FileNotFoundException ,...

#7
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
you mean this ? java.io.FileNotFoundException: C:\Users\david\Downloads\Desktop\koi (Access is denied)

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Yes. That's the interesting stuff ^^
Another question: is

File[] files = dir.listFiles();

in getDatabaseFingerprintcsv the 1066th line of the file? If not, could you tell me what the 1066th line is?

#9
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
the 1066th line of my code is
FileInputStream input = new FileInputStream(selectedFile);


#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Aha,
Java API:

Quote

FileInputStream

public FileInputStream(File file)
throws FileNotFoundException
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument.

If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.

Parameters:
file - the file to be opened for reading.
Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file.
See Also:
File.getPath(), SecurityManager.checkRead(java.lang.String)

I'm not sure what you're trying to do there with the file. As I think the classes you use at the next line using the "input" variable are selfmade.
Anyway, just know that you can't use a fileInputStream with a directory. And you're using it with a directory (I think, as "koi" has no extension)

#11
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
Ya koi is the name of my folder. So for my openbutton part is correct ? Now only left with my convert button ? I'm really stuck :(

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, you just can't use an inputFileStream in combination with a folder at line 1066. What's happening inside new ReaderFactory().createReader(input); ?
- Can't you do the stuff inside it with a regular file?
- Is createReader used elsewhere with 'normal' files, eg no directories, but just files?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users