Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading & Writing Text Files

Posted on 1998-05-21
8
Medium Priority
?
153 Views
Last Modified: 2010-04-16
How do I write a text file keeping a user's formatting from a textbox, and then how do I read it back with the same formatting?  For example, the following should keep its line breaks (or line wrapping) and paragraph breaks.

Thanks in advance!

This is paragraph one.
This is line two of paragraph one.

This is paragraph two.
This is line two of paragraph two.
This is line three of paragraph two.
0
Comment
Question by:jreilly
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 400 total points
ID: 1221300
writing to file --->

try {
String str = textArea.getText();
FileOutputStream ofs = new FileOutputStream("mytxt.txt");
ofs.write(str.getBytes());
ofs.close();
} catch (Exception e) { e.printStackTrace();}


reading from file --->


try {
FileInputStream ifs = new FileInputStream("mytxt.txt");
byte[] datab;
ifs.read(datab = new byte[ifs.available()]);
String dataStr=new String(datab);
textArea.setText(dataStr);
} catch (Exception e) {}



0
 

Author Comment

by:jreilly
ID: 1221301
Hi,

The errors I am getting trying to test the code above are:

no method matching getBytes() found in class java.lang.String
no constructor matching String(byte[]) in class java.lang.String

I am using java 1.0.2.

Thanks again!
0
 

Author Comment

by:jreilly
ID: 1221302
Adjusted points to 100
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:evijay
ID: 1221303
I am sorry/. I thought you  are using 1.1 jdk.

You have to change the calls in this way.



        try {
        String str = textArea.getText();
        FileOutputStream ofs = new FileOutputStream("mytxt.txt");
        byte barr[] = new byte[str.length()];
        str.getBytes(0, barr.length - 1, barr, 0);
        ofs.write(barr);
        ofs.close();
        } catch (Exception e) { e.printStackTrace();}


        reading from file --->


        try {
        FileInputStream ifs = new FileInputStream("mytxt.txt");
        byte[] datab;
        ifs.read(datab = new byte[ifs.available()]);
        String dataStr=new String(datab, 0); // set hibyte to 0 unless u are using some asian language
        textArea.setText(dataStr);
        } catch (Exception e) {}
0
 

Author Comment

by:jreilly
ID: 1221304
Thank you!
0
 

Author Comment

by:jreilly
ID: 1221305
This works great for textboxes but when I try to display it as static text it doesn't retain the paragraph breaks.  Any ideas?

Thanks again!
0
 

Author Comment

by:jreilly
ID: 1221306
This works great for textboxes but when I try to display it as static text it doesn't retain the paragraph breaks.  Any ideas?

Thanks again!
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221307
I didnt get by what you mean by static text. Can you elaborate this with an example ?

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

916 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