This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

how to get a filename with fileoutputstream

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have just the fileoutputstream which is referencing a file, now i want to see whether the file has already any information in it. I wrote the following code to do that:
try {
// Thinking that ostream is the fileoutputstream that i have,
FileDescriptor fd = ostream.getFD();
if (fd.valid())
System.out.println("FD is valid");
FileInputStream istream = new FileInputStream(fd);
if (istream.available() > 0)
System.out.println("The file has " +istream.available() +" bytes to read");
else
System.out.println("The file is empty");
//istream.read();
istream.close();
ostream.close();
}
catch (FileNotFoundException fe)
{
System.out.println("No such file" +fe.toString());
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
The above code is giving me,
1. The file is empty always.
2. When i uncomment istream.read() then it is giving "ioexception:bad file descriptor" error on unix and "ioexception: Access is denied: error on NT. The reason i think is "when i open a file using ostream, the file is opened in "w"/"a" mode and hence cannot read anything".
My problem is i just have fileoutputstream and i need to check whether the file contains anything before i write into it. If only there is a way to get filename then i can probably do something like:
file.delete();
file.createNewFile()
which will ensure that i always will get a fresh copy of the file. Can somebody give me a hand in it.
Thanks
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunitha
If you are using Java1.4 then the following should work for you,

Here we can use FileChannel's size() method to see how many bytes are already there and if it returns 0 then we can assume the file is not there or having nothing in it..
Regards
Maulin
 
sunitha nerevetla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i am using java 1.3, i wish they had something very similar to that in 1.3 version. Is there any other way to achieve it.
Thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the length() method in File.
The available() method in an InputStream is rarely useful for anything, really. It can return 0 - this just means that there are 0 bytes available at that instant, maybe because they haven't yet been read from a hard drive or transmitted across a network or something. When available() == 0, that may mean the stream has ended, or it may just mean that there's a (small) delay in reading the bytes. If you call read() (or read(byte[]) or whatever) you can examine the result and find out. A -1 indicates the stream really has ended.
But that's only important if you want to actually read the info in the file. If you just want to find out the size, File's length() method is all you need.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim
But the problem, I guess, Sunitha is facing is- she doesn't have "File" object. She has the FileOutputStream object, right Sunitha?
In that case what can be done? We can get the FileDescriptor but from that how we get File object to invoke length() method...
I am thinking...
Regards
Maulin
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see - didn't read carefully enough. Well, the point about available() is still valid. I don't see any way to get the File object. However you could:
get the FileDescriptor
close the FileOutputStream
open a FileInputStream (fis) using FileDescriptor
read entire file from fis and copy to another file or byte array
close fis
open a FileOutputStream (fos) using FileDescriptor
copy old file contents to fos
write new content (whatever it is) to fos
close fos
It's a pretty ugly way to do this, but I can't see another way. How is it that you have a FileOutputStream but don't know what it's writing to, anyway?
Hmmm, there's another issue - a big one. If the original FileOutputStream was created in append mode, you're fine. But if it was created in overwrite mode - then the old file contents have already been overwritten. There's really no way to recover the original contents in this case.
 
sunitha nerevetla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, I have followed another way of doing it. I stored the filenames too in hashmap and cleaned the file before i wrote to it.
It was getting too messy without filenames.
Thanks
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!