try
{
         String filePath = "D:\\tomcat\\webapps\\myweb\\new.txt";
File new_file = new File(filePath);
PrintWriter pnWriter = null;
if(new_file.exists()){//添加到文件尾
new PrintWriter(new FileOutputStream(new_file,true));
}else{//新建文件内容
new PrintWriter(new FileOutputStream(new_file,true));
}
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}

解决方案 »

  1.   

    package NewFile;
    import java.io.*;
    public class NewFile
    {
    public static void main(String[] args)
    {
    try
    {
    boolean flag = true;//ÓÃÓÚÅжÏÊÇ·ñ´æÔÚ¸ÃÎļþ String filePath = "D:\\tomcat\\webapps\\myweb";
    File new_file = new File(filePath);
    if (!fileDir.exists()) {//½¨Á¢D:\tomcat\webapps\mywebĿ¼
    fileDir.mkdirs();
    }
    String[] fileNames = fileDir.list();
    for (int i = 0; i < fileNames.length; i++) {
    if (fileNames[i].equals("new.txt")) {
    flag = false;
    break;
    }
    }
    if (flag) {
    PrintWriter pnWriter = new PrintWriter(new FileOutputStream("D:\\tomcat\\webapps\\myweb\\new.txt"));
    }
    }
    catch(FileNotFoundException ex)
    {
    System.out.println(ex.getMessage());
    }
    }
    }