在servlet中创建,写文件 和在别的地方操作是一样的哦

解决方案 »

  1.   


    public class WriteFile {
        
    public boolean startWrite() {
    String path = "";
    FileWriter fw = null;

    try {
    //path=ClassLoader.getSystemResource(".").toString();
    path = this.getClass().getResource(".").getPath();
    System.out.println("path " + path);
           fw = new FileWriter(path + "test.txt");
        fw.write("wubai test");
        fw.close();
        return true;
    }
    catch(Exception e){
    System.out.println("write file error");
    e.printStackTrace();
    return false;
    }
    }
    public static void main(String[] args) {
    WriteFile w = new WriteFile();
    w.startWrite();
    }
    }在servlet中取不到路径