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?
Reading and writing to a file
Page 1 of 16 Replies - 1302 Views - Last Post: 08 May 2011 - 12:09 PM
Replies To: Reading and writing to a file
#2
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.
#3
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
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
#4
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().
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().
#5
Re: Reading and writing to a file
Posted 07 May 2011 - 02:58 AM
GregBrannon, 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().
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
#6
Re: Reading and writing to a file
Posted 07 May 2011 - 01:54 PM
No one has any tip?
Does the code look right?
Does the code look right?
#7
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
DIC tutorial by dragon-slayer
Java tutorial by Oracle
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|