Welcome Guest ( Log In | Join )

Discover the best free computer help!
Learn more about Geeks to Go by taking the tour. Want to ask a question, reply to a topic, or remove all advertising? It's easy, fast and free. Join today!
Spyware, virus, trojan, fake security or privacy alerts? Please start with our malware cleaning guide.
     
 
Reply to this topicStart new topic
Reading a File in Java
zell_ffhut
post Apr 25 2006, 08:27 AM
Post #1


Member
**
Posts: 34
OS: Windows XP



Im getting a File Access error with the following code -

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?
Go to the top of the page
 
+Quote Post
VMM VMX
post 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 wink.gif

do you have access rights to the file? huh.gif

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies / Views Topic Information
No New Posts   2 / 775 30th November 2005 - 03:36 PM
Kevin39 started - last by Kevin39
No New Posts   2 / 292 30th December 2005 - 10:19 AM
complete started - last by Hai Mac
No New Posts   7 / 419 25th December 2008 - 03:20 AM
vally started - last by vally
No New Posts   3 / 130 Today, 03:39 AM
Metallica started - last by Metallica

RSS 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.