?
Solved

Iam trying write a huge data which is in string str

Posted on 2005-04-18
30
Medium Priority
?
294 Views
Last Modified: 2010-03-31
Iam trying write a huge data which is in string str ..into a fie. But i can see its writing only the last line of str.

Here is the way iam doing?
try{
                   BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\users\\radhika\\java\\ouput1.txt"));
                   bw.write(str+"\n");
                   bw.close();
                 
               } catch(IOException e){ System.out.println("File Not found");}

can anyobne tell me what's wrong in the above
0
Comment
Question by:cutie_smily
  • 14
  • 9
  • 7
30 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 450 total points
ID: 13807320
What does 'str' hold?
0
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 300 total points
ID: 13807324
I think your problem preced that line.
check what str contains (System.out.println(str)) and you will see it has only the last line at this point.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807333
Please also post output of

System.out.println(str.length());
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cutie_smily
ID: 13807382
Reading one of the web page data into char array called data and storing that in str and writting str into a file. Here
 public void handleText(char[] data, int pos) {
                String str=new String(data);              
                System.out.println(data);
               try{
                   BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\users\\radhika\\java\\ouput1.txt"));
                   bw.write(str+"\n");
                   bw.close();
                 
               } catch(IOException e){ System.out.println("File Not found");}
Here is the result for str.
--------------------------------------------------------

Enter search criteria &
run preliminary search
Choose the information
that you’re looking for
Select the best options
for your search
Easy checkout
generatetAd(1)
generatetAd(2)
generatetAd(3)
Buy all People Search records for $9.95
Get all the results listed below for just $9.95. The reports instantly displays all "Available" People Search Information. Additional Reports sold separately.
Buy a 24 Hour Pass for $19.95
Buy a 3 Day Pass for $39.95
Buy a 30 Day Reunion Pass for $149.95
 Did you know that you can also
Search by SSN
 or
Maiden Name?
 
4
 Preliminary Search Results for:
displayDisplayName('1', "http://www.ussearch.com/consumer/cwf?adID=10002101&action=browseproduct&searchtab=people&pid=3064&searchPerson=ENH-13675805&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=CHICAGO&searchState=IL&searchApproxAge=22&searchStateJurisdiction=IL&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC", , '0', 'ENH-13675805', 'off'); displayAgeCityState('-', 'CHICAGO', 'IL'); displayPremiumUrls('&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=CHICAGO&searchState=IL&searchApproxAge=22&searchStateJurisdiction=IL&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC', 'ENH-13675805', '**/**/00', '5c090915434a115c2'); displayL2Result('0', 'ENH-13675805', 'off');
displayDisplayName('2', "http://www.ussearch.com/consumer/cwf?adID=10002101&action=browseproduct&searchtab=people&pid=3064&searchPerson=ENH-13675808&searchFName=&searchMName=R&searchLName=searchCity=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC, '1', 'ENH-13675808', 'off'); displayAgeCityState('-', 'LITTLE ROCK', 'AR'); displayPremiumUrls('&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC', 'ENH-13675808', '**/**/00', '51540c04140a03510'); displayL2Result('1', 'ENH-13675808', 'off');
displayDisplayName('3', "http://www.ussearch.com/consumer/cwf?adID=10002101&action=browseproduct&searchtab=people&pid=3064&searchPerson=ENH-13675807&searchFName=searchMName=R&searchLName=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC", 'RADHIKA','R','CHILKA', '2', 'ENH-13675807', 'off'); displayAgeCityState('-', 'LITTLE ROCK', 'AR'); displayPremiumUrls('&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC', 'ENH-13675807', '**/**/00', '7f474b1302437f479'); displayL2Result('2', 'ENH-13675807', 'off');
displayDisplayName('4', "http://www.ussearch.com/consumer/cwf?adID=10002101&action=browseproduct&searchtab=people&pid=3064&searchPerson=ENH-13675806&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC", , '3', 'ENH-13675806', 'off'); displayAgeCityState('-', 'LITTLE ROCK', 'AR'); displayPremiumUrls('&searchFName=RADHIKA&searchMName=R&searchLName=CHILKA&searchCity=LITTLE+ROCK&searchState=AR&searchApproxAge=22&searchStateJurisdiction=AR&searchGender=&searchZip=&vid=cfc&searchAgentNotes=PREVIEW-CFC', 'ENH-13675806', '**/**/00', '65050a021d5c4d4a8'); displayL2Result('3', 'ENH-13675806', 'off');
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807413
In that case:

 Please also post output of

System.out.println(str.trim().length());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807419
... and if that function is called several times you need to open the file in append mode
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807432
BufferedWriter bw=new BufferedWriter(new FileWriter("c:/users/radhika/java/ouput1.txt", true));
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807453
I think CEHJ got it right (you are probably calling handleText multiple times).
To open it in append more -> BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\users\\radhika\\java\\ouput1.txt", true));
Though you might also consider in this case to open it once before the loop that calls handleText and then close it after the loop (will be more efficient for big files).
0
 

Author Comment

by:cutie_smily
ID: 13807480
I opened file in append mode now the compete string is writting ...but all in one one line.
so i have written "\n" but doesn't have any effect is is this correct

 bw.write("\n");
 bw.write(str);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807487
There's also no need to create a String. The char[] can be written directly. If you need to 'keep a record' of it, append the char[] to a StringBuffer and then write once to the file at the end
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807497
bw.write("\n".toCharArray());
 bw.write(data);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807530
Though it would be easier to use a PrintWriter:

PrintWriter pw=new PrintWriter(new FileWriter("c:/users/radhika/java/ouput1.txt", true));
pw.println(data);

but it's not efficient to keep opening the file as aozarov said
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807562
You can also use the BufferedWriter write with string as argument -> bw.write(string, 0, string.length)
You also might to consider System.getProperty("line.separator") instead of "\n" in case you want "\r\n" for windows and "\n" for linux.
0
 

Author Comment

by:cutie_smily
ID: 13807570
bw.write("\n".toCharArray());
 bw.write(data);

its not working file has all data in one lline .
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807620
bw.write("\n".toCharArray()); or similar should be part of the loop (hence called for each line).
Also try using bw.write(System.getProperty("line.separator").toCharArray()); instead.
0
 

Author Comment

by:cutie_smily
ID: 13807622
File is empty if i do the above way..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807633
That must be illusory i think ;-)

Try

 bw.write("\r\n".toCharArray());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807652
There actually should be no need for adding line breaks, since they probably should not have been stripped out of the char[]
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807660
bw.write("\r\n".toCharArray()); vs bw.write(System.getProperty("line.separator").toCharArray());
I don't think that is your problem (System.getProperty("line.separator") should be cleaner)
I guess now you are not writing your str (and just ouputing EOL)...
0
 

Author Comment

by:cutie_smily
ID: 13807666
The above one also Doesn't work file is  empty.
If we use string instead of char array how do we insert new lines??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807669
If the code *is* being called once per line

bw.write(System.getProperty("line.separator"));

should put them back in properly. If it isn't being called once per line, then something has stripped out the original line breaks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807688
>>If we use string instead of char array how do we insert new lines??

There's no need to create a String as i mentioned above. Inserting new lines is best done with a PrintWriter as i said before
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807732
IOW your code should look like this:

public void handleText(char[] data, int pos) {
      pw.println(data);
}

although there is the question of what 'pos' is for, which might be part of the problem ...
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807744
CEHJ is suggesting you to wrap BufferedWriter with PrintWriter  so you will not need to worry about new lines (by calling println instead of write).
0
 

Author Comment

by:cutie_smily
ID: 13807818
bw.write("\r\n".toCharArray());

does work now . Before iwas not closing Bufferedwriter ..

Can you tell me what this mean i know we are converting \n to char array what is \r mean??
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807914
\r\n is the EOL (new line) for windows where it is just \n for Linux and \r for Mac.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13807950
http://www.hardwarebook.net/table/ascii.html
ascii 10  = \n
ascii 13  =  \r
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13808014
:-)

Using a PrintWriter will save you having to concern yourself with platform line break forms
0
 

Author Comment

by:cutie_smily
ID: 13808350
Thanks aozarov
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13808426
:-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question