Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default What's wrong with my code ?

    I want to open a folder of files.

    I know there's somethings that gonna do with this :

    Java Code:
    FileInputStream input = new FileInputStream([COLOR="YellowGreen"]selectedFile[/COLOR]);

    But if i dont use FileInputStream what else can i use inorder for me to read the files in my folder ?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    It depends on what type the files are. You'd use different tools to read different file types.
    An easy case are text files which you can read using either Scanner or FileReader/BufferedReader combination.

  3. #3
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    ok i had change my code to :

    Java Code:
    FileReader input = new FileReader(selectedFile);
    but there still error... btw the file i want to open can be open by textpad.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You'll need to post the error message you are getting and a bit more of your code.
    Before you do that, try to understand what the error message is saying and seed if you can't fix the problem on your own (The error message usually try to give you hints on how to fix the problem). Also make sure you check the API specs for the classes you are using to make sure you are passing the right types of arguments.

  5. #5
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    ok here's my code:

    The open button which enable me to choose the folder i want to open when i click on it.

    Java Code:
    private void LoadDbActionPerformed(java.awt.event.ActionEvent evt) {                                       
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File("."));
            chooser.setDialogTitle("Select configuration file");
            chooser.addChoosableFileFilter(new FileNameExtensionFilter("All files(*.*)", "mol2"));
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            chooser.setAcceptAllFileFilterUsed(false);
            File directory = new File("C:\\Documents and Settings\\student\\Desktop\\lopac");
            File[] files = directory.listFiles();
    
            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);
                }
                directory = chooser.getSelectedFile();
                selectedFile = directory;
                Database.setText(directory.getAbsolutePath());
    
            }
        }

    here's the code when for my convert button :

    Java Code:
     BufferedReader br = null;
              FileWriter writer = new FileWriter(dir);
              File directory = new File("C:\\Documents and Settings\\student\\Desktop\\lopac");
              File[] files = directory.listFiles();
              File outputFile = new File(dir);
              PrintStream output = new PrintStream(outputFile);
              
    
    
              try {
    
    			IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
    			FileReader input = new FileReader(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 : directory.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();
                    }
    
    
              }
    }

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    And what is the error message that you get?

  7. #7
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    This :

    Java Code:
    java.io.FileNotFoundException: C:\Documents and Settings\student\Desktop\lopac (Access is denied)

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Which line in your code is reported for that exception?
    Is lopac a file or a directory? Is the file you are trying to access marked as read only?

  9. #9
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    This are all the errors :

    Java Code:
    java.io.FileNotFoundException: C:\Documents and Settings\student\Desktop\lopac (Access is denied)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:106)
            at java.io.FileReader.<init>(FileReader.java:55)
            at fyp_cdk.FYP_CDKView.getDatabaseFingerprintcsv(FYP_CDKView.java:1019)
            at fyp_cdk.FYP_CDKView.Convert1ActionPerformed(FYP_CDKView.java:831)
            at fyp_cdk.FYP_CDKView.access$1300(FYP_CDKView.java:52)
            at fyp_cdk.FYP_CDKView$9.actionPerformed(FYP_CDKView.java:411)
            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)
    Lopac is a folder/directories that contain the files.

    Ya that folder/directories is marked as read only...

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheTypicalStudent View Post
    Lopac is a folder/directories that contain the files.

    Ya that folder/directories is marked as read only...
    You can't read a directory/folder as if it were an ordinary file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You can't read a directory/folder as if it were an ordinary file.

    kind regards,

    Jos
    Ok ... i try untick the read only thing but there's the same error prompting

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheTypicalStudent View Post
    Ok ... i try untick the read only thing but there's the same error prompting
    Reread what I wrote: "You can't read a directory/folder as if it were an ordinary file."; no matter whether or not you (un)tick whatever, you can't read a directory/folder as if it were an ordinary file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    You already have code to find out the contents of a directory:
    Java Code:
    File[] files = directory.listFiles();
    Why are you trying to read the directory itself (which, as has been pointed out, is not possible anyway)?

    Do not try to read in Files that have isDirectory() returning true...because they aren't files.

Similar Threads

  1. What is wrong with my code?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 10:44 PM
  2. What's wrong with my code?
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 06:00 PM
  3. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM
  4. what's wrong with my code? please help me...
    By lovely23 in forum Java Applets
    Replies: 2
    Last Post: 03-01-2009, 01:33 PM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •