Solved

Reading Formatted Text File into StaticText

Posted on 1998-05-26
6
195 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

24 Experts available now in Live!

Get 1:1 Help Now