导入包
import java.io.*;
调用PrintWriter的构造方法可能会抛出某种异常。java强制要救编写代码来处理这类异常,public static void main(String[] args )throws Exception//抛出PrintWriter调用异常{
示例代码如下:
//文件输入和输出
import java.io.*;
public class j04{
public static void main(String[] args )throws Exception{
File f04 = new File("j003.txt");
if(f04.exists() ){
System.out.println( "file is already exists" );
System.exit(0);
}
PrintWriter tofiles = new PrintWriter(f04);
tofiles.print("hello world");
tofiles.println( " this is file input and output" );
tofiles.print(3334);
tofiles.println(232.23);
tofiles.close();
}
}