[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

simple file code

I just declared 'aFile'  and now need the code  to verify that 'aFile'  exists and print out "NO such file" if it doesn't

and then I need to  open 'aFile' for input as 'InputFile'

then close 'aFile'
0
usumle
Asked:
usumle
1 Solution
 
CEHJCommented:
Check the exists() method of java.io.File
0
 
NaeemgCommented:
//if aFile is ur File Object then,

if(aFile.exists())
 System.out.println("Yes");
else
 System.out.println("No such file");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> open 'aFile' for input as 'InputFile'

What is 'InputFile'? Is it an InputStream (FileInputStream)? Here's how to read bytes from a file into a byte-array:

http://www.javaalmanac.com/egs/java.io/File2ByteArray.html

And here's how to read from it as text:

http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html
0
 
aozarovCommented:
After aFile.exists() [see CEHJ suggestion] (assuming aFile is of type java.io.File) you can also apply aFile.canRead() check to see if your program will be able to read that file (though that will also be detected by actually trying to read it via FileInputStream or a FileReader [see mayankeagle suggestion]).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
usumle,

Why did you accept that comment?

- it mentions almost the same thing which CEHJ already mentioned

- it does not answer your question completely - the other part is answered in my comment
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now