Results 1 to 2 of 2
- 06-21-2010, 11:02 PM #1
Some Basic file handling questions
hey i've just moved onto file handling but the way ive learnt to handle files seems a bit untidy and i was wondering if there is a better way to handle files compared to how i am doing it. below is my code
What i dont like about this code is that im creating 3 objects all of different classes to basically handle one file. is there not one object which can do it all?
ie check the file exists, read from it, write to it, delete it etc?
PHP Code:public class GUI extends JFrame{ private Scanner fileReader; // used for reading files private File fileHandle; //used for checking file exists private Formatter fileCreator; //used for creating files public boolean checkFile(String fileNamePassed){ boolean exists = false; try{ fileHandle = new File(fileNamePassed); if(fileHandle.exists()) exists = true; //return true if File name exists }catch(Exception e){ JOptionPane.showMessageDialog(null,e.getMessage());} return exists; } public void createFile(String fileNamePassed){ try{ if (checkFile(fileNamePassed) == false ) //If no File exists fileCreator = new Formatter(fileNamePassed); //Blank File created else JOptionPane.showMessageDialog(null, "file already exists"); }catch(Exception e){ JOptionPane.showMessageDialog(null,e.getMessage());} } public void readFile(String fileNamePassed){ try{ if (checkFile(fileNamePassed) == true) //If File exists fileReader= new Scanner(new File(fileNamePassed)); //initiate reader object //some reading code here else JOptionPane.showMessageDialog(null, "file doesn't exists"); }catch(Exception e){} } }Last edited by alacn; 06-21-2010 at 11:06 PM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-21-2010, 11:24 PM #2
Sorry. Your just beginning to get into files. There are so many classes that it will confuse you when you see them all.
Be sure to read and study the Tutorial on this one. And write LOTs of short example programs to use each of them. A technique would be to write out a file and then read it back in and display what's read or compare it against what was written. Most write/read methods should be symmetrical.
Similar Threads
-
Regarding File Handling
By ravjot28 in forum New To JavaReplies: 1Last Post: 01-20-2010, 09:15 PM -
Basic Image Questions
By DaRancor in forum AWT / SwingReplies: 6Last Post: 11-22-2009, 12:20 AM -
handling WAV file
By zellazode in forum New To JavaReplies: 1Last Post: 09-08-2009, 03:46 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks