Skip to main content
gamedev.net
🔒 Locked

[java] Loading in Properties

Started by dxFoo 1 reply 800+ views
Original Post
dxFoo
dxFoo
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) {}
		
		
	}
}



Son of Cain
Son of Cain
You should post the code of your "load(FileInputStream fin)" method, in the "balance" object. Post the stack trace as well.
a.k.a javabeats at yahoo.ca
DaBono
DaBono
@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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.