Results 1 to 2 of 2
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    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

  1. Regarding File Handling
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 09:15 PM
  2. Basic Image Questions
    By DaRancor in forum AWT / Swing
    Replies: 6
    Last Post: 11-22-2009, 12:20 AM
  3. handling WAV file
    By zellazode in forum New To Java
    Replies: 1
    Last Post: 09-08-2009, 03:46 PM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

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