By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Saving and Loading Issues

P: 3
Okay, so I have two methods, one to save selected arrow keys to a text file, and the other to load them as integers. Here's the first:

Expand|Select|Wrap|Line Numbers
  1. public void saveKeys()
  2. {
  3.    String out = Integer.toString(left)+"\n"+Integer.toString(right)+"\n"+Integer.toString(up)+"\n"+Integer.toString(down)+"\n";
  4.    try
  5.    {
  6.       PrintWriter save = new PrintWriter("key_selection.txt",out);
  7.    }
  8.    catch ( Exception e )
  9.    {
  10.             System.err.print(e);
  11.    }
  12. }
...which gives me this error message:

Expand|Select|Wrap|Line Numbers
  1. java.io.UnsupportedEncodingException: 37
  2. 39
  3. 38
  4. 40
  5.  
After inputting what it should have saved into the save file ("37\n39\n38\n40\n"), here is the code I made:

Expand|Select|Wrap|Line Numbers
  1. public void loadKeys()
  2. {
  3.    try
  4.    {
  5.       FileInputStream fis = new FileInputStream("key_selection.txt");
  6.       BufferedInputStream bis = new BufferedInputStream(fis);
  7.       String in = "";
  8.       while ( bis.read() > -1 )
  9.       {
  10.          in += (char)bis.read();
  11.       }
  12.       System.out.println(in);
  13.    }
  14.    catch ( Exception e )
  15.    {
  16.       System.err.println(e);
  17.    }
  18. }
...which gives me this as output:

Expand|Select|Wrap|Line Numbers
  1. 7
  2. 9
  3. 8
  4.  
  5.  
(There should be a zero on line 4, but I don't know why there isn't.)

What am I doing wrong? Please help!
Jul 24 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.