PrintWriter file= new PrintWriter(new FileWriter("league1.txt",true));
         file.print(value1);
         file.print(',');
         file.print(value2);
         file.print(',');
         file.print(value3);
         file.print(',');
         file.close();

i m using this code to write in a file.. i m using this in servlet.value1, value2,value3 are Strings. and i wana to write it in file named "league1.txt". i have made this file in WEBPAGES folder in netbeans. is it right place ?? it is not writing anything to file.. please help... my web application is running fine but not file writing....HELP!!

Recommended Answers

All 13 Replies

you did not write any code to actually write anything to your file.
take a look at the PrintWriter api and check what the print method does.

also, if you're not sure whether you've placed your file in the correct place, don't create it manually, but let your application create it for you, based on the path/filename you provide.

you did not write any code to actually write anything to your file.
take a look at the PrintWriter api and check what the print method does.

also, if you're not sure whether you've placed your file in the correct place, don't create it manually, but let your application create it for you, based on the path/filename you provide.

no no ! value1,value2,value3 have certain values already in the servlet. i have read its api and as per it, i m doing correctly. tell me now..:'( and how to make a file own its own..?

you did not write any code to actually write anything to your file.
take a look at the PrintWriter api and check what the print method does.

also, if you're not sure whether you've placed your file in the correct place, don't create it manually, but let your application create it for you, based on the path/filename you provide.

pw.println("successful!!");
         league l= new league(value1,y,value3);
         leaguelist.add(l);
         PrintWriter file= new PrintWriter(new FileWriter("C:\'Users\'gourav\'Documents\'NetBeansProjects\'WebApplication2\'web\'league1.txt",true));
         file.print(value1);
         file.print(',');
         file.print(value2);
         file.print(',');
         file.print(value3);
         file.print(',');
         file.println();
         file.close();


         BufferedReader lr= new BufferedReader(new FileReader("C:\'Users\'gourav\'Documents\'NetBeansProjects\'WebApplication2\'web\'league1.txt"));
         String r;
         while((r=lr.readLine())!=null)
         {
            String [] p=r.split(",");
            pw.println(p[0]);
            pw.println(p[1]);
            pw.println(p[2]);
         }

it is writing one thing many times into the file.. what is it ??

Is writing what one thing many times? Please try to explain exactly and precisely what is happening.
You create your FilWriter in append mode (second parameter is true), so every time you run the application you get another copy of the data added to your file.

Seriously just wondering, Shouldn't C:\'Users\'gourav\'Documents\'NetBeansProjects\'WebApplication2\'web\'league1.txt actually be C:\\Users\\gourav\\Documents\\NetBeansProjects\\WebApplication2\\web\\league1.txt

Is writing what one thing many times? Please try to explain exactly and precisely what is happening.
You create your FilWriter in append mode (second parameter is true), so every time you run the application you get another copy of the data added to your file.

when i reading a data from file, it is reading it.. but why is it not showing file at that place. when i visit this path, then there is no file named this. but it is actually in working how is it possible??

Seriously just wondering, Shouldn't C:\'Users\'gourav\'Documents\'NetBeansProjects\'WebApplication2\'web\'league1.txt actually be C:\\Users\\gourav\\Documents\\NetBeansProjects\\WebApplication2\\web\\league1.txt

y escape character are wrong here? i think it is right ? i think if it wrong, then james cherill can't be left behind to point it out. i know this thing.

See stephen84s repky. You seem to be trying to use ' as a path delimiter!

See stephen84s repky. You seem to be trying to use ' as a path delimiter!

but when i do this, what he said , then file stops working... what is this now? when i replaces ' with / then it is not working means not writing data to file..?

You are using Windows, yes?
The delimiter in Windows is \
But in Java that character is used to start escape sequences, so to create String containing a \ you have to code \\, exactly as in stephen84s post.
In your first version you were using ' characters, coded in Java as \'
Because ' is a valid character in a Windows file name you were creating a file called 'Users'gourav'Document'NetBeansProjects'WebApplication2'web'league1.txt in your current working directory.
When you replace the \' with \\ the string is parsed as a full path with directory names etc, and you probably have a mistake in there somewhere (incorrect directory name, access rights problems...?).
In Windows you can also use / as a delimiter and it (usually) works, but \ is the "correct" delimiter.

Next time please try to describe your problems properly. "stops working" and "is not working" tells us nothing useful. Always post exactly what you did in your code, the full text of any Exception or other error message, and describe exactly how the output was different from what you expected. I'm not going to try to answer any more problems that are not fully described.

You are using Windows, yes?
The delimiter in Windows is \
But in Java that character is used to start escape sequences, so to create String containing a \ you have to code \\, exactly as in stephen84s post.
In your first version you were using ' characters, coded in Java as \'
Because ' is a valid character in a Windows file name you were creating a file called 'Users'gourav'Document'NetBeansProjects'WebApplication2'web'league1.txt in your current working directory.
When you replace the \' with \\ the string is parsed as a full path with directory names etc, and you probably have a mistake in there somewhere (incorrect directory name, access rights problems...?).
In Windows you can also use / as a delimiter and it (usually) works, but \ is the "correct" delimiter.

Next time please try to describe your problems properly. "stops working" and "is not working" tells us nothing useful. Always post exactly what you did in your code, the full text of any Exception or other error message, and describe exactly how the output was different from what you expected. I'm not going to try to answer any more problems that are not fully described.

sir!! but when i use \\ it is not writing it in file. means it is not even giving any error. but when i replace \\ again with \' it starts working means it is writing it file. if u saying that \\ is correct than why is this happeneing ? and also, why i m seeing any file at this place ? but i m accessing it? how ? please tell me!! please...When you replace the \' with \\ the string is parsed as a full path with directory names etc, and you probably have a mistake in there somewhere (incorrect directory name, access rights problems...?). i think answer is hidden. when i will use it, why there will problem like accesing and all? please tell

Sorry, I don't know how to explain that any more clearly. Please re-read the whole of my previous post.

Sorry, I don't know how to explain that any more clearly. Please re-read the whole of my previous post.

ohh! i really got that now. what you want to say is that ' is allowed in file name, and when i searched this file name, it was present in my computer but in any other place on which i was not expecting. oops! i got everything now! thanks u sir!! you are solution to every problem.HATS OFF FOR YOU!! thread is solved. thanks again to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.