java.io.IOException: No such file or directory and URI

You need to create a new file with a file URI like file:/tmp/a.txt, but had this error:

java.io.IOException: No such file or directory
 at java.io.UnixFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(File.java:947)
 at FileTest.main(FileTest.java:7)
The directory /tmp (or java.io.tmpdir) should already exist and should have write permission for the application. So the error message may be complaining the file (a.txt) does not exist. But a.txt is the new file you are trying to create and of course it doesn't exist. Take another look at the source code:
import java.io.File;
import java.net.URI;

public class FileTest {
    public static void main(String args[]) throws Exception {
        File f = new File(args[0]);
        boolean b = f.createNewFile();
        if(b) {
            System.out.printf("Successfully created new file: %s%n", f);
        } else {
            System.out.printf("Failed to create new file: %s%n", f);
Run this class with different input, such as /tmp/a.txt, file:/tmp/a.txt:
$ java FileTest /tmp/a
Successfully created new file: /tmp/a

$ java FileTest /tmp/a
Failed to create new file: /tmp/a
# This failure is expected since /tmp/a already exists.

$ java FileTest file:/tmp/a
Exception in thread "main" java.io.IOException: No such file or directory
So the cause of the IOException is input file URI is not a valid file path. If you have a file URI, then you should use another File constructor to instantiate the File:

//to instantiate a file with URI
//TODO: need to handle java.net.URISyntaxException 
File f = new File(new URI(args[0]));

//to instantiate a filw with path string
File f = new File(args[0]);


Anonymous said...

Great blog you have! I also got computer's related blog with general how to's and I would like to make it more active. Would you like to exchange link with me? We can create "Partners" category on the blog and have a links to each other?

Have a look at: http://devmain.blogspot.com


Unknown said...

There are a number of Free High PR Directory Submission Sites List 2014 sites thank u for Directory Submission i think it will help me

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training
Great and Useful Article.

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Anonymous said...

I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game: imgtaram. What about you?

alicetaylor said...

Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!


Sophie Grace said...

I appreciate your interest and hope you keep coming back to my blog. Good luck. Go to picbear to know more top idols on instagram

Bilao said...

Thank you so much for your great sharing i like it! Keep going bro http://www.al3abwater.com I will share it now good luck.

customized gifts for him said...

accent pillow case baby burlap
accent pillow case home decor
housewarming gift

ravisynit said...

great java tips At SynergisticIT we offer the best java bootcamp

Anonymous said...

Nice post. best semi automatic washing machine reviews

aditya said...

very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

Horizontal Asymptote
utsa blackboard
Special Right Triangles
Perfect Square Trinomial Formula
anesthesia technician course syllabus
What are the Scientific Reasons for Choosing Printed Books
favourite fruit mango
bds full form
how to make your essay longer

unknown said...

HVAC & Plumbing Services
Air Star Heating guarantees reliability and quality for all equipment and services.

Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
Visit:- https://airstarheating.com

Nishant said...

Best Semi Automatic Washing Machine

Shivani said...

Thanks for sharing ! great post

Best Front Load Washing Machines
Best Laptops Under 50000