Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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