[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reading Formatted Text File into StaticText

Posted on 1998-05-26
6
Medium Priority
?
203 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 200 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
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!

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

649 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