Reading a File in Java |
![]() ![]() |
Reading a File in Java |
Apr 25 2006, 08:27 AM
Post
#1
|
|
|
Member ![]() ![]() Posts: 34 OS: Windows XP |
CODE public static void main(String[] args) throws IOException, ClassNotFoundException { // Variables ObjectInputStream fileIn; ObjectOutputStream fileOut; String fileName = "Assigment2File.dat"; // File Name to Read Message tempMessage; int noMessages = 0; int menuChoice = 0; // Creates and Instance of PriorityQueue PriorityQueue pQueue = new PriorityQueue(); while (menuChoice != 7) { menu(); System.out.println("Please choose your desired task"); menuChoice = Text.ReadInt(in); if (menuChoice == 1) { try { //Open the desired file fileIn = new ObjectInputStream(new FileInputStream(fileName)); //read number of objects in file noMessages = fileIn.readInt(); tempMessage = null; for (int i = 0; i < noMessages; i++) { tempMessage = (Message) fileIn.readObject(); System.out.println(tempMessage.getMessage()); i = i + 1; } fileIn.close(); } catch (Exception e) { System.out.println("File input error"); } } System.out.println(pQueue.count()); clearscreen(); } From all the lecture notes i have read, this should allow me to read from the file, and print the contents to screen, but it dosen't. Any ideas? |
|
|
Apr 27 2006, 01:18 AM
Post
#2
|
|
|
Member ![]() ![]() Posts: 18 OS: Windows Vista Buisness(currently main OS), openSUSE 10.2 & Windows XP SP 2 |
try using the full path of the file if you didn't created it throught a java progam
do you have access rights to the file? by the way is a good idea if you use an ArrayList to store all the messages and read/write only it This post has been edited by VMM VMX: Apr 27 2006, 01:36 AM |
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies / Views | Topic Information | |||||
|---|---|---|---|---|---|---|---|
![]() |
2 / 775 | 30th November 2005 - 03:36 PM Kevin39 started - last by Kevin39 |
|||||
![]() |
2 / 292 | 30th December 2005 - 10:19 AM complete started - last by Hai Mac |
|||||
![]() |
7 / 419 | 25th December 2008 - 03:20 AM vally started - last by vally |
|||||
![]() |
3 / 130 | Today, 03:39 AM Metallica started - last by Metallica |
|||||
|
Time is now: 6th January 2009 - 01:31 PM |
| Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk. |