tags:

views:

1605

answers:

1

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


     }

}
+1  A: 

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.

Jon Skeet
How can i specify the path in servlet? Any example. Are there any permission issues?
Jammin
You can specify it in the servlet configuration. As for whether there are any permission issues - that will depend on your environment. At the moment we can't tell, because you're swallowing the exception.
Jon Skeet
if my text file "test.txt" stored in a package name mypackage; what would the path of this file.
Jammin
You're trying to *write* a file to the file system - packages are irrelevant at that point. They're for resources/classes to *read*. If you want to write it to the location where test.txt *would* be, it will depend entirely on your deployment scenario - but I really wouldn't recommend writing files into a deployment area, as they'll be overwritten when you next deploy.
Jon Skeet
I am very thankful to you. A brief of my work:I am downloading a web page using URL class and want to store it in temporary text file . Then i want to parse information from that text file and store it to database. That's why i need a temporary text file. getabsolutepath() works ok. But i want to create a file on my own choice giving the file path. But i do not know how to specify the path. I don't care if the file overwritten on next deploy. If any better way to do it let me know.
Jammin
If you only want a temporary text file, use System.getProperty("java.io.tmpdir"). However, I wonder whether you really need the text file at all... why don't you parse it in memory?
Jon Skeet
"parse it in memory"... how can I do it? Take the whole text into String or there are better ways...
Jammin
It depends on how you've got the data to start with - but basically you can create a MemoryInputStream if you've got a byte array or a StringReader if you've got a string. That way you're basically where you would be if you opened a file.
Jon Skeet