supman
V2EX  ›  问与答

同样是输出内容到文件 为啥 java 下成功 但是 servlet 下就不行了呢?

  •  
  •   supman · Jan 26, 2016 · 1684 views
    This topic created in 3794 days ago, the information mentioned may be changed or developed.

    如果是 javac 编译 然后运行的话, 就会在根目录出现创建的文件, 可是通过 jsp 调用这个 servlet 就失败了。。 这是什么原因呢?

    public  static void writeToFile(String data)throws Exception{
        {
            String filename= "abc";
            FileWriter fw = new FileWriter(filename,true); //the true will append the new data
            fw.write(data);//appends the string to the file
            fw.close();
        }
    }
    
    3 replies    2016-01-26 12:46:29 +08:00
    supman
        1
    supman  
    OP
       Jan 26, 2016
    难道是路径名字的问题?
    zts1993
        2
    zts1993  
       Jan 26, 2016
    到时把异常打出来看呐。。。。
    Cloudee
        3
    Cloudee  
       Jan 26, 2016 via iPhone
    你用相对路径的话,在 servlet 容器里运行文件所在的路径可能跟你预期的不太一样,也可能容器没权限写那个目录,总之你先用这个 filename 来 new 一个 File 对象,把绝对路径打出来看看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1287 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 59ms · UTC 17:26 · PVG 01:26 · LAX 10:26 · JFK 13:26
    ♥ Do have faith in what you're doing.