The Artima Developer Community
Sponsored Link

Java Answers Forum
why could not i use createNewFile( ) method in java io

1 reply on 1 page. Most recent reply: Nov 6, 2002 8:23 AM by Daniel Ray

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
shi bo luo

Posts: 5
Nickname: seafarer
Registered: Nov, 2002

why could not i use createNewFile( ) method in java io Posted: Nov 6, 2002 4:58 AM
Reply to this message Reply
Advertisement
dear sir,

i wanted to use the method createNewFile( ) in class File to create a new file,but got the error message during compling,the code is listed below:

import java.io.*;

public class FileTest{
static void makeDir(String parent,String child){
File f=new File(parent);
f.mkdirs();
File subf=new File(parent,child);
subf.createNewFile();

}
public static void main(String[] args){
makeDir(args[0],args[1]);

}
}


error message:
FileTest3.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
subf.createNewFile();

please help me! thank you!


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: why could not i use createNewFile( ) method in java io Posted: Nov 6, 2002 8:23 AM
Reply to this message Reply
import java.io.*;

public class FileTest{
static void makeDir(String parent,String child){
File f=new File(parent);
f.mkdirs();
File subf=new File(parent,child);

try{
subf.createNewFile();
} catch(IOException ioe){
ioe.printStackTrace();
}
}

public static void main(String[] args){
makeDir(args[0],args[1]);

}
}

/*
Look at your java reference for createNewFile(). Notice that it throws an
IOException. You need to catch it.

createNewFile public boolean createNewFile()
throws IOException

Another option is to throw it yourself.

public class FileTest throws IOException {
static void makeDir(String parent,String child){
File f=new File(parent);
f.mkdirs();
File subf=new File(parent,child);

subf.createNewFile();

}

I wouldn't do that here though. If FileTest were outside of the main .. say you
had multiple classes .. then this would be okay with a try catch in the constructor
or method that calls the FileTest object.


*/

Flat View: This topic has 1 reply on 1 page
Topic: Boolean Previous Topic   Next Topic Topic: Drag and Drop Saving....

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use