Solved

Reading & Writing Text Files

Posted on 1998-05-21
8
144 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 100
0
 
LVL 4

Expert Comment

by:evijay
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:jreilly
Comment Utility
Thank you!
0
 

Author Comment

by:jreilly
Comment Utility
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
Comment Utility
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
Comment Utility
I didnt get by what you mean by static text. Can you elaborate this with an example ?

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now