Results 1 to 13 of 13
Thread: What's wrong with my code ?
- 03-28-2011, 07:10 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
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 ?
- 03-28-2011, 07:14 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 03-28-2011, 07:19 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
ok i had change my code to :
but there still error... btw the file i want to open can be open by textpad.Java Code:FileReader input = new FileReader(selectedFile);
- 03-28-2011, 07:29 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 03-28-2011, 07:32 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
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(); } } }
- 03-28-2011, 07:55 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
And what is the error message that you get?
- 03-28-2011, 08:44 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
This :
Java Code:java.io.FileNotFoundException: C:\Documents and Settings\student\Desktop\lopac (Access is denied)
- 03-28-2011, 08:52 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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?
- 03-28-2011, 08:56 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
This are all the errors :
Lopac is a folder/directories that contain the files.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)
Ya that folder/directories is marked as read only...
- 03-28-2011, 09:01 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2011, 09:21 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
- 03-28-2011, 09:49 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-28-2011, 11:05 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
You already have code to find out the contents of a directory:
Why are you trying to read the directory itself (which, as has been pointed out, is not possible anyway)?Java Code:File[] files = directory.listFiles();
Do not try to read in Files that have isDirectory() returning true...because they aren't files.
Similar Threads
-
What is wrong with my code?
By Solarsonic in forum New To JavaReplies: 3Last Post: 03-22-2011, 10:44 PM -
What's wrong with my code?
By Isong in forum AWT / SwingReplies: 1Last Post: 11-16-2010, 06:00 PM -
What's going wrong with this code?
By Suurisa in forum New To JavaReplies: 5Last Post: 10-19-2009, 11:59 PM -
what's wrong with my code? please help me...
By lovely23 in forum Java AppletsReplies: 2Last Post: 03-01-2009, 01:33 PM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks