简体   繁体   中英

How to create or write text file in servlet?

i have tried this code. I didn't create any file. I am doing this in netbeans where server is glassfish v2
public void create_file(String file_name) { FileWriter file_stream; BufferedWriter out_stream;

    try
    {
        file_stream= new FileWriter(file_name);
        this.out_stream = new BufferedWriter(file_stream);

    }
    catch (Exception e){//Catch exception if any


     }

}

I suspect you're passing in a relative filename - and chances are the working directory isn't what you expect it to be. I suggest you use an absolute filename, or find out an appropriate directory to create the file relative to.

It would also help if you didn't swallow exceptions - if there's something going wrong, the exception is trying to tell you about it, but you're ignoring it completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM