Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading from a file

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    Raleigh, NC
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Reading from a file

    I'm working on a project for class that reads a txt file of movie-titles, genres, and quantity into a dvd rental system. Using a GUI I have the user select the txt file to be read, check to make sure it's a txt file, and then send it to a constructor to parse information. I'm getting the error response "Bad file name".

    Here's the GUI code:
    public MyFlixGUI() throws MissingFileException {
    		JFileChooser fc = new JFileChooser();
    		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    		int returnVal = fc.showOpenDialog(this);
    		if (returnVal == JFileChooser.APPROVE_OPTION) {
    			File file = fc.getSelectedFile();
                            rentals = new DVDRentalSystem(file.getName()); 
    		}
    		else
    			throw new MissingFileException("No file selected.");    	
    	}

    Here's the constructor:
    public DVDRentalSystem(String filename){
    		inventory = new Linklist();
    		reserve = new Linklist();
    		athome = new Linklist();
    		try{
    			FileReader fr = new FileReader(filename);
    			BufferedReader input = new BufferedReader(fr);
    			String line = input.readLine();
    			while (line != null){
    				Scanner scan = new Scanner(line);
    				int inv = scan.nextInt();
    				String genre = scan.next();
    				String name = scan.nextLine();
    				addToInventory(new Film(name,genre,inv));
    				line = input.readLine();
    			}
    		}
    			catch (FileNotFoundException e) {
    				System.err.println("Bad file name");
    			}
    			catch (IOException e) {
    				System.err.println("Exception in reading from file"); 
    			}
     
    	}


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading from a file

    Take a look at the API for File...the getName() returns the name of the file, but you will need the full path. Look into using the getAbsolutePath() function.

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 4th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Reading from a file

    If you don't get any further with the getAbsolutePath() function, please post something we can compile. Thanks
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  2. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 03:43 AM
  3. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 03:14 AM
  4. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM