• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Writing String Data to a File

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm interested in writing string data to a file in UTF-8 format. The following code is intended to do this, but only writes the first line of the string to the file:


try {
Writer out = new BufferedWriter(new OutputStreamWriter
(new FileOutputStream("fileName.txt"),"UTF8"));
out.write(stringData);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}

I'd appreciate any suggestions on how this code might be modified to write the entire string to a file in UTF-8 format.

Thanks.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That'll write the whole String to the file. Of course, if anything goes wrong, you'll never hear about it due to those empty catch blocks. I hope your real code logs the exceptions or does something with them so that if there's a problem, you hear about it -- instead of, as in this case, assuming your code is not doing the right thing.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string data is thousands of characters in length. However, all that is being written to the file is the following:

<?xml version="1.0" encoding="UTF-8"?>


It's actually XML data. Here's an excerpt of the input string ...

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"><fo:layout-master-set><fo:simple-page-master margin-bottom="0.0in" margin-right="0.0in" margin-top="0.0in" margin-left="0.0in" page-height="11in" page-width="8.5in" master-name="Letter-p-1"><fo:region-body margin-bottom="1.2in" margin-top="1.5in" margin-right="0.4in" margin-left="1.3in" column-count="1"/><fo:region-before extent="1.3in"/><fo:region-start extent="1.3in"/><fo:region-after extent="1.2in"/><fo:region-end


While editing the file, the following message appears at the bottom:

"A line cannot be longer than 2048 characters."


Is there anyway that the code might be modified so that it writes all of the XML data to the file?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Sounds like Java's writing the String just as it apparently exists in your Java code: the (short) XML header line, followed by a very long single line of XML. Then whatever you're using to open the file -- whatever is displaying that "line too long" message -- is choking on that very long line. The file is fine -- it's whatever you're using to view it that's not.

So there are two alternatives: In Java, break the long line into pieces before printing it out. This might be hard to do correctly; maybe it could be generated to use multiple lines in the first place?

The second alternative, the one I'd use personally, is to stop using whatever editor or file viewer is complaining about the long line. Many editors will simply wrap long lines, or let them disappear off the right margin.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same question to more than one forum; see our explanation here. I've deleted the other copy of this thread.

I'm a little vexed that you felt you had to repost this question elsewhere; I thought the issues were well spelled out here. If you want your editor (some version of vi, apparently) to be able to handle the file, then you need to break the long line up into shorter lines. It would be best to do this upstream -- i.e., at the point where the very long line is generated. Put some line breaks into the String as its being created.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should have provided further details. The vi editor is not a concern. That was used only that to check the contents of the file. The file does not need to be parsed. The file is used as input for third party software (Antenna House) which uses it to generate a PDF file. I'm therefore looking to write all of the XML data to the file without resorting to parsing. (The XML data is created using complicated logic and modifying this for parsing is not feasible.)

Thanks.
 
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about the editor, if you "type" or "cat" the file, is the file complete? We are trying to confirm that Java did successfully write the file, it's just that your editor can't read it.

Henry
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I 'type' the file, a message says 'file not found'. If I 'cat' the file, about 95 percent of the data is displayed. (About the 5 percent of the data is missing from the top of the file.) Might there be some means of writing all the data to the file? (The file is only 24K bytes.)

Thanks.
 
Henry Wong
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Scott:
If I 'type' the file, a message says 'file not found'. If I 'cat' the file, about 95 percent of the data is displayed. (About the 5 percent of the data is missing from the top of the file.) Might there be some means of writing all the data to the file? (The file is only 24K bytes.)

Thanks.



You probably got the whole file, the first 5 percent probably just scrolled off of the display. Anyway, to confirm just cat the file and pipe it to more -- so that you can see the whole file.

cat xxx.xml | more

Henry
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was mistaken. Apparently all of the data was written to the file since the vi editor displays the top line and the cat editor displays the bottom line plus 95 percent of the data (up to about the 10th line from the top). Is there a means of displaying all of this data, or is there simply too much that can be displayed by any editor, including cat?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Emacs would have no problems displaying the whole file.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic