Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple but not working????

Newbie
 
Join Date: May 2007
Posts: 10
#1: May 31 '07
hii everyone!!!
Heres a code for writing a string stored in resp_str to a file in thge path specified but I dont get the required string in the output file...I just get an empty file....
pls help me..as to why it is happening...theres no error which I get!!!
Regards Punit

Expand|Select|Wrap|Line Numbers
  1. File output=new File("D:\\Punit\\Output.txt");
  2. boolean a=output.exists();
  3. System.out.println("the file exists??"+a);       //true
  4. a=output.canWrite();
  5. System.out.println("the file can be written :"+a);    //true
  6. PrintWriter pw = new PrintWriter(new FileWriter(output));
  7. String resp_str=resp.toString();
  8. int lenStr=resp_str.length();
  9. int i=0;
  10. while(lenStr>0){
  11.     pw.write(resp_str.charAt(i));
  12.     i++;
  13.     lenStr--;
  14. }

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 31 '07

re: Simple but not working????


I moved your question from the Java Articles section to the Java Forum section.
This is the place to ask questions. The Java Articles section is for published
informative (hopefully) articles. Please post your questions in the Java Forum
section. Thanks.

kind regards,

Jos
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: May 31 '07

re: Simple but not working????


Quote:

Originally Posted by rushnitrkl22

hii everyone!!!
Heres a code for writing a string stored in resp_str to a file in thge path specified but I dont get the required string in the output file...I just get an empty file....
pls help me..as to why it is happening...theres no error which I get!!!
Regards Punit

Expand|Select|Wrap|Line Numbers
  1. File output=new File("D:\\Punit\\Output.txt");
  2. boolean a=output.exists();
  3. System.out.println("the file exists??"+a);       //true
  4. a=output.canWrite();
  5. System.out.println("the file can be written :"+a);    //true
  6. PrintWriter pw = new PrintWriter(new FileWriter(output));
  7. String resp_str=resp.toString();
  8. int lenStr=resp_str.length();
  9. int i=0;
  10. while(lenStr>0){
  11.     pw.write(resp_str.charAt(i));
  12.     i++;
  13.     lenStr--;
  14. }

Add a few debugging prints such as:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("resp_str: "+resp_str);
  2. System.out.println("length: "+lenStr);
add them just before that while loop and see what happens. You do close that
file after you've finished printing do you?

kind regards,

Jos
Newbie
 
Join Date: May 2007
Posts: 10
#4: Jun 1 '07

re: Simple but not working????


I wrote the above two linwes...
The string resp_str is displayed and the length of the file is also obtained...
but nothing gets written into the file....
Also u said about closing the file..that I m not doing as I dont knoe how to close the file....
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jun 1 '07

re: Simple but not working????


Quote:

Originally Posted by rushnitrkl22

I wrote the above two linwes...
The string resp_str is displayed and the length of the file is also obtained...
but nothing gets written into the file....
Also u said about closing the file..that I m not doing as I dont knoe how to close the file....

Closing the file is super easy. You just do ..., ah but it's there in the specs for the File class. Look it up.
Reply