Results 1 to 1 of 1
- 02-23-2012, 09:30 PM #1
Member
- Join Date
- Oct 2011
- Location
- New Jersey
- Posts
- 44
- Rep Power
- 0
Qn: Getting Around Windows File Lockup Property -- using FileHandling in Java
I am encountering a problem in developing a software for my business. The task I am trying to achieve is that, I need to update a file (.txt) while that file is being constantly accessed by another program.
The ultimate setting is this: I will have a software running, constantly reading the content of a feed file (call it info.txt). I have another script running, constantly updating the feed file (info.txt).
I realized the serious conflict in using the file after I implemented the above setting. With the software (call it AAA) running, I can't make an edit to info.txt even manually. I open up info.txt, make a change, and click save, windows return an error message: "The process cannot access the file because it is being used by another process." If I try to delete info.txt, the error message is "the action can't be completed because the file is open in AAA.exe".
When I run my (java) script that constantly updates info.txt, the file IO exception is:
java.io.FileNotFoundException: info.txt (The process cannot access the file because it is being used by another process)
The software AAA is not that well developped. (It's a foreign software to my enterpise so I can't modify its behavior/source code.) The way it is accessing files locks up the file completely. I thought of temperarily pointing away the reference to info.txt in the software's profile files to allow a temparory edit, but this approach would fail because with AAA running (and I need it to), I can't make any change to any file it's using.
I consulted one of the main developer of software AAA. He acknowledges this is a problem right now, he would improve it in next release. But meanwhile I would like the set up to work. He told me he programs with .NET, and he provided a line that could help me to get around my problem:
FileStream fs = new FileStream(@"info.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
Supposedly, by supplying these arguments, this filestream are allowed to be shared on both writes and reads.
I am a java programmer, and I've had no experience with .NET. However I did some research and learning, I realized his code is the syntex of C#, which runs in the CLR of .NET framework. I set up C# on my computer and studied some C# beginer tutorial, and comes up with the following script, hoping to solve my problem.
If it's really true that C# allows to change the access property of a file at a fundamental level, I would open up the filestream of info.txt, allowing it to be shared with both read and write. Once I do that, I can run my script of updating info.txt, and then run the software AAA that keeps refreshing info.txt.
Trying to achieve the above implementation, I come up with a C# script:
================================================
using System;
using System.IO;
class CopyFeed
{
static void Main()
{
string fileName1 = "info.txt";
string fileName2 = "info2.txt";
FileStream fs = new FileStream(@fileName1, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = null;
// attempting to unlock part of the file, didn't work
/*
string contents = File.ReadAllText(@fileName2);
fs.Unlock(0, contents.Length * 2);
*/
while (true)
{
Console.WriteLine("Copying Feed...");
string contents = File.ReadAllText(@fileName2);
try
{
sw = new StreamWriter(fs);
sw.Write(contents);
}
catch (Exception e)
{
Console.WriteLine("CopyFeed :: " + e.Message);
}
finally
{
if (sw != null)
{
sw.Flush();
//sw.Close();
}
}
Console.WriteLine("Pausing For 2 sec...");
System.Threading.Thread.Sleep(2000);
}
}
}
================================================
Supposedly, I will have my java script constantly updating info2.txt . Then I will have this C# script constantly copying the entire content of info2.txt into info.txt.
(The above script is still buggy, because all it does is keep appending to the file info.txt. I need to find a way that I can clear the content of file at the start of every iteration and just copy over the content of file2. Right now if I execute the script, it will create a infinitely large file as time goes.)
I wish to seek help with this post:
Based on the description above, could I find a solution using Java? does Java have file handling capability that interacts with Windows system in a more fundamental level and unlocks the file? Or does any 1 have any better suggestions?
I've been working on this problem for the past few days and am really stuck. I would sincerely thank any one who offers any insight!
Similar Threads
-
java don't read property file
By laviper in forum Java AppletsReplies: 0Last Post: 10-13-2011, 01:52 AM -
Java and file Windows file permissions
By danborgir in forum Advanced JavaReplies: 1Last Post: 01-11-2011, 07:04 PM -
FileHandling
By er1c550n20 in forum New To JavaReplies: 1Last Post: 03-12-2010, 07:34 AM -
File Property
By Juggler in forum New To JavaReplies: 0Last Post: 08-10-2008, 07:23 AM -
Property File
By Peter in forum JDBCReplies: 2Last Post: 07-04-2007, 03:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks