V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
supman
V2EX  ›  问与答

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

  •  
  •   supman · 2016-01-26 12:25:52 +08:00 · 1253 次点击
    这是一个创建于 3015 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果是 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 条回复    2016-01-26 12:46:29 +08:00
    supman
        1
    supman  
    OP
       2016-01-26 12:32:07 +08:00
    难道是路径名字的问题?
    zts1993
        2
    zts1993  
       2016-01-26 12:32:35 +08:00
    到时把异常打出来看呐。。。。
    Cloudee
        3
    Cloudee  
       2016-01-26 12:46:29 +08:00 via iPhone
    你用相对路径的话,在 servlet 容器里运行文件所在的路径可能跟你预期的不太一样,也可能容器没权限写那个目录,总之你先用这个 filename 来 new 一个 File 对象,把绝对路径打出来看看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1042 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 19:02 · PVG 03:02 · LAX 12:02 · JFK 15:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.