File I/O Now possible, even with Applets

Do you use JavaWIDE in the classroom? Do you have some tips to share? If so, post them here.

File I/O Now possible, even with Applets

Postby Jam Jenkins » Tue Sep 08, 2009 10:05 am

As David Wheeler once said:
Any problem in computer science can be solved with another layer of indirection.


This is what JavaWIDE now does with File I/O using the following classes:
  • FileInputStream
  • FileOutputStream
  • FileReader
  • FileWriter
  • PrintStream (when used with a File object)
  • PrintWriter (when used with a File object)
  • Scanner (when used with a File object)

For file input, these classes first redirect to URL input which is what applets can normally do. It this fails, then it pops up a text box saying java.io.FileNotFoundException and gives the option to enter text directly into the textbox. Once the text has been entered and the Load File button is pressed, the input is forwarded to the right place using pipes. After this, the user has the option to upload the given file to the website, which usually requires a login and password. If the file uploads successfully (the user has the appropriate level of permissions to upload text files), then the next time the program runs it will read directly from the uploaded file.

For file output, a similar mechanism is followed. A text box appears with the text of the file that is being written. Once the file has been written completely to the text box, the user has the option of uploading the file to the server, just like with the file input options.

These mechanisms make it easier for students to experiment with File I/O because they don't have to worry about erasing files or creating some automated script that really messes up their hard drive. The manual uploading of files to the server prevents this from happening. It also helps tremendously with not having to worry so much about getting the path right and finding out how to correctly specify the relative and full path.
Jam Jenkins
Site Admin
 
Posts: 87
Joined: Tue Jun 30, 2009 1:53 pm

Re: File I/O Now possible, even with Applets

Postby Coachfitz » Wed Jan 12, 2011 1:42 pm

Jam,

I have been trying to do file input for 2 days with no success so I am contacting you. Can you give me an example of how to input a txt file of 100 integers. I have uploaded a file call Compact.txt. I have created a file called compact.java to read the file however, I keep getting a fileNotFoundException as described in this post.

Can you help a brother out?

Rob
Coachfitz
 
Posts: 2
Joined: Sat Sep 11, 2010 3:33 pm

Re: File I/O Now possible, even with Applets

Postby Jam Jenkins » Thu Jan 13, 2011 3:52 pm

I'd be glad to help. Check out http://shhs.javawide.org/index.php/Fitz ... 13/compact. The problem should now be fixed. When you try to read a file that does not yet exist on the server it pops up a window telling you this and giving you the opportunity to load the file by pasting in the contents and clicking on 'Load File'. After you do this you can also upload the file by clicking on 'Upload File' and entering your username/password. The next time you run the program after uploading the file it should just read it from the server. I hope this helps. Let me know if you need further assistance.
Jam Jenkins
Site Admin
 
Posts: 87
Joined: Tue Jun 30, 2009 1:53 pm


Return to Teaching Tips

Who is online

Users browsing this forum: No registered users and 1 guest

cron