Solved

Reading & Writing Text Files

Posted on 1998-05-21
8
145 Views
Last Modified: 2010-04-16
How do I write a text file keeping a user's formatting from a textbox, and then how do I read it back with the same formatting?  For example, the following should keep its line breaks (or line wrapping) and paragraph breaks.

Thanks in advance!

This is paragraph one.
This is line two of paragraph one.

This is paragraph two.
This is line two of paragraph two.
This is line three of paragraph two.
0
Comment
Question by:jreilly
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 100 total points
ID: 1221300
writing to file --->

try {
String str = textArea.getText();
FileOutputStream ofs = new FileOutputStream("mytxt.txt");
ofs.write(str.getBytes());
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);
textArea.setText(dataStr);
} catch (Exception e) {}



0
 

Author Comment

by:jreilly
ID: 1221301
Hi,

The errors I am getting trying to test the code above are:

no method matching getBytes() found in class java.lang.String
no constructor matching String(byte[]) in class java.lang.String

I am using java 1.0.2.

Thanks again!
0
 

Author Comment

by:jreilly
ID: 1221302
Adjusted points to 100
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221303
I am sorry/. I thought you  are using 1.1 jdk.

You have to change the calls in this way.



        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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jreilly
ID: 1221304
Thank you!
0
 

Author Comment

by:jreilly
ID: 1221305
This 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!
0
 

Author Comment

by:jreilly
ID: 1221306
This 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!
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221307
I didnt get by what you mean by static text. Can you elaborate this with an example ?

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected HttpURLConnection connection behavior 2 86
recursion example 16 111
eclipse formatting 6 73
object oriented programming comparison 5 55
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

15 Experts available now in Live!

Get 1:1 Help Now