[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Reading Formatted Text File into StaticText

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
jreilly
Asked:
jreilly
  • 3
  • 3
1 Solution
 
gadioCommented:
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
 
jreillyAuthor Commented:
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
 
gadioCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gadioCommented:
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
 
jreillyAuthor Commented:
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
 
jreillyAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now