• Advertisement
Sign in to follow this  

[java] Loading in Properties

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't seem to load the data from phonebook.dat. I commented where the error is. Any help is great!
import java.lang.*;
import java.io.*;
import java.util.*;

class Console_Practice
{
	public static void main(String args[]) throws IOException
	{ 
		Properties balance = new Properties(); 
		
		// try to open phonebook.dat
		FileInputStream fin = null;
		
		try
		{
			fin = new FileInputStream("c:\\phonebook.dat");
		}
		catch (FileNotFoundException e)
		{
			System.out.println("Error opening file.");
		}
		
		// Load contents of phonebook.dat. ERROR: NOT WORKING HERE
		try
		{
			while (fin != null)
			{
				balance.load(fin);
				fin.close();
			}
		}
		catch (IOException e)
		{
			System.out.println("Error reading file.");
		}
 
 		// Try saving the phone book.
		try 
		{
			FileOutputStream fout = new FileOutputStream("c:\\phonebook.dat");
			balance.store(fout, "Telephone Book");
			fout.close();
		}
		catch (FileNotFoundException e) {}
		
		
	}
}



Share this post


Link to post
Share on other sites
Advertisement
You should post the code of your "load(FileInputStream fin)" method, in the "balance" object. Post the stack trace as well.

Share this post


Link to post
Share on other sites
@Son of Cain: dxFoo hasn't written that code: it's a method in the Java API.

@dxFoo: From the code you posted I can't really deduce where the error's coming from. Is this 'phonebook.dat' in a format that Properties expects?
You might find out more about what's going wrong, by printing the detailed message in the exception like this:

catch (IOException e)
{
System.out.println("Error reading file:" + e);
}


Edit: Oh, wait... Try removing the while(fin != null)-loop. If I recall correctly Properties.load reads in the whole file at once.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement