?
Solved

Reading Formatted Text File into StaticText

Posted on 1998-05-26
6
Medium Priority
?
202 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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
Course of the Month11 days, 12 hours left to enroll

752 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