Solved

Reading & Writing Text Files

Posted on 1998-05-21
8
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 100 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
Industry Leaders: 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!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

623 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