Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Reading a File in Java


  • Please log in to reply

#1
zell_ffhut

zell_ffhut

    Member

  • Member
  • PipPip
  • 34 posts
Im getting a File Access error with the following 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?
  • 0

Advertisements


#2
VMM VMX

VMM VMX

    Member

  • Member
  • PipPip
  • 18 posts
try using the full path of the file if you didn't created it throught a java progam :blink:

do you have access rights to the file? :whistling:

by the way is a good idea if you use an ArrayList to store all the messages and read/write only it

Edited by VMM VMX, 27 April 2006 - 01:36 AM.

  • 0

#3
PoRco

PoRco

    Member

  • Member
  • PipPip
  • 14 posts
I really haven't reached that far yet in what I'm learning in Java...but maybe the file isnt in your compilation foldeR?
  • 0

#4
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
As VMM mentioned. Try including the full absolute path of the file that you are trying to read. Touch wood it should work.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP