Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading & Writing Text Files

Posted on 1998-05-21
8
Medium Priority
?
152 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

705 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