Solved

Reading Formatted Text File into StaticText

Posted on 1998-05-26
6
201 Views
Last Modified: 2010-04-16
The code below (from a previously answered question by Evijay) 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!



        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
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
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 50 total points
ID: 1221464
The code that you are using will probably not work for large texts the reason is that the available method is: "Returns the number of bytes that can be read from this input stream without blocking". For large texts you will probably get a small section of the text's beginning. Also you are reading bytes and then converting them to string. This conversion is probably your problem. Its much better to use Object writing and reading for a String. This way you can be sure that you get back exactly what you wrote. Here is a sample made from your code (run in an appletviewer):


//----------- readwrite_text.java ---------------
import java.applet.Applet;
import java.awt.*;
import java.io.*;

public class readwrite_text extends Applet {
    TextArea textArea;
    Button read, write;

    public void init() {
        textArea = new TextArea(20,20);
        read = new Button("Read");
        write = new Button("Write");
        add( textArea);
        add(read);
        add(write);
    }


    public boolean action( Event ev, Object what ) {
        if( ev.target == read ) {
            //reading from file --->


            try {
                FileInputStream ifs = new FileInputStream("mytxt.txt");
                ObjectInputStream ois = new ObjectInputStream(ifs);
                String str = (String)ois.readObject();
                textArea.setText(str);
            } catch (Exception e) {}
        }
        if( ev.target == write ) {
            try {
                String str = textArea.getText();
                FileOutputStream ofs = new FileOutputStream("mytxt.txt");
                ObjectOutputStream oos = new ObjectOutputStream(ofs);
                oos.writeObject(str);
                oos.close();
                ofs.close();
            } catch (Exception e) { e.printStackTrace();}
        }
        return false;
    }
}


0
 

Author Comment

by:jreilly
ID: 1221465
I am receiving the error class ObjectInputStream not found in type declaration.  I am importing java.io.*;  I am using JDK1.0.2.  (See previous question answered by evijay).
0
 
LVL 6

Expert Comment

by:gadio
ID: 1221466
I see. If you are using 1.0.2 then you can not use my answer (you should allways add the version when you ask...). I'll give you a version for 1.0.2 in soon.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:gadio
ID: 1221467
This should work on jdk1.0.2. Let me know if its ok.


//------------------------------
import java.applet.Applet;
import java.awt.*;
import java.io.*;

public class readwrite_text extends Applet {
    TextArea textArea;
    Button read, write;

    public void init() {
        textArea = new TextArea(20,20);
        read = new Button("Read");
        write = new Button("Write");
        add( textArea);
        add(read);
        add(write);
    }


    public boolean action( Event ev, Object what ) {
        if( ev.target == read ) {
            //reading from file --->


            try {
                FileInputStream ifs = new FileInputStream("mytxt.txt");
                DataInputStream dis = new DataInputStream(ifs);
                String str = dis.readUTF();
                textArea.setText(str);
            } catch (Exception e) {}
        }
        if( ev.target == write ) {
            try {
                String str = textArea.getText();
                FileOutputStream ofs = new FileOutputStream("mytxt.txt");
                DataOutputStream dos = new DataOutputStream(ofs);
                dos.writeUTF(str);
                dos.close();
                ofs.close();
            } catch (Exception e) { e.printStackTrace();}
        }
        return false;
    }
}

0
 

Author Comment

by:jreilly
ID: 1221468
Thank you for the answer!

Sorry I did not indicate the version.  I thought my reference to evijay's previously answered question for me would explain that.  I put the version in that one.


0
 

Author Comment

by:jreilly
ID: 1221469
Hi,

I tried the code above.  When the file is saved, the paragraph breaks are kept.  However, when the file is read into the text field it is not maintaining the paragraph breaks.

Thanks in advance!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:

691 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