6 Replies - 1779 Views - Last Post: 08 May 2011 - 12:09 PM Rate Topic: -----

#1 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Reading and writing to a file

Posted 06 May 2011 - 03:41 PM

Hello

I am writing to a file using streams (I am using eclipse).
But when I write once to a file, and try to write to it again, it gives me an IOException. I have to refresh the file everytime before I can write to it again, which really is very impracticle.

Everytime I write to the file, and open it via eclipse, it says:
Resource is out of sync with the file system:'/MyProjects/file' (This is my project directory).
Press 'F5' or select File > Refresh to refresh the file.

Tried to google this and found out that many people have experienced the same thing, and the only solution I could find to it, was to make eclipse refresh the file automatically. It does refresh it, but I still only can write to the file once, then I get an IOException.
Is there anyone out there who found a solution to this?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading and writing to a file

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Reading and writing to a file

Posted 06 May 2011 - 04:40 PM

I have never had this problem. Post your code, so we can see if your doing everything correctly.
Was This Post Helpful? 0
  • +
  • -

#3 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Reading and writing to a file

Posted 07 May 2011 - 01:22 AM

I will post it when I get home.
But I'm sure the code is correct. I forgot to mention that, on my mac it works fine, there I can write to the file several time without having to refresh. It is just on my windows 7 stationary the problem occurs.
------------------------------UPDATE----------------------------
Ok, here is the code
	private File file = null;
	private BufferedWriter writer = null;
	private BufferedReader reader = null;
	private Scanner scanner = null;
	
	public FileHaantering()
	{
			file = new File("savings");	
		try {			
			scanner = new Scanner(file);
			writer = new BufferedWriter(new FileWriter(file, true));
			reader = new BufferedReader(new FileReader(file));
			
		} catch (IOException e) {
			System.err.println("Could not initialize writer");
		}
	}
	
	public void writeToFile(String msg){
		try {
			writer.write(msg);
			writer.newLine();
			writer.flush();
			writer.close();
		} catch (IOException e) {
			System.err.println("Could not write to file");
		}
	}


This post has been edited by MorRomio: 07 May 2011 - 01:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Reading and writing to a file

Posted 07 May 2011 - 02:43 AM

Can you have the same file open for writing and reading at the same time? I don't think so. Even if it's possible, it's not good practice. I suggest you open the file for writing, write to it, and close it. Then when you need to read from it, open it for reading, read from it, and close it.

The error you're getting is Eclipse's way of protecting you from using a file that is in an uncertain state - open for reading, open for writing, closed, changed, same as before, etc. - or letting you know that Eclipse is uncertain about the state of the file.

P.S. - you don't need to flush() before closing a file. Closing includes the flush().
Was This Post Helpful? 0
  • +
  • -

#5 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Reading and writing to a file

Posted 07 May 2011 - 02:58 AM

View PostGregBrannon, on 07 May 2011 - 02:43 AM, said:

Can you have the same file open for writing and reading at the same time? I don't think so. Even if it's possible, it's not good practice. I suggest you open the file for writing, write to it, and close it. Then when you need to read from it, open it for reading, read from it, and close it.

The error you're getting is Eclipse's way of protecting you from using a file that is in an uncertain state - open for reading, open for writing, closed, changed, same as before, etc. - or letting you know that Eclipse is uncertain about the state of the file.

P.S. - you don't need to flush() before closing a file. Closing includes the flush().

So how would you handle this? Are you thinking about threads? I guess that's the way to avoid interference of the file if one person is trying to read the file while another is writing to it?

If not threads are the best solution here, then I have no idea.

I actually do have a read method, it looks like this
	public String readFromFile() throws FileNotFoundException{
		String msg = null;
		while(scanner.hasNext()){
			msg += scanner.nextLine();			
		}
		return msg;
	}


But I don't call this method at the same time as I'm writing to the file. Is it still a problem?
And if you guys know a better implementation of how to implement a read from file method, please share. This is just what I can come up with.

This post has been edited by MorRomio: 07 May 2011 - 03:03 AM

Was This Post Helpful? 0
  • +
  • -

#6 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Reading and writing to a file

Posted 07 May 2011 - 01:54 PM

No one has any tip?
Does the code look right?
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Reading and writing to a file

Posted 08 May 2011 - 12:09 PM

For reading and writing to a file look these two posts:
DIC tutorial by dragon-slayer
Java tutorial by Oracle
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1