reading from text file

B

Brandon McCombs

Hello,

I'm trying to create a simple "notepad" type program. I'm reading a
text file and eventually I want to get the contents into the TextArea
widget except that I can't get the actual contents of the file for some
reason. I'm able to get the filename and path and it is marked as
available. I tried the Sun tutorial that is a simple app of the
following:

import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("in.txt");
File outputFile = new File("out.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
System.out.println(c);
out.write(c);
in.close();
out.close();
}
}

I've tried to implement the meat of that into my own app but I don't
know how to get the data that is coming out of read() into the TextArea
since its append() method requires a String.

Does anyone know how to make that type of a conversion?
thanks
 
K

Knute Johnson

Brandon said:
Hello,

I'm trying to create a simple "notepad" type program. I'm reading a
text file and eventually I want to get the contents into the TextArea
widget except that I can't get the actual contents of the file for some
reason. I'm able to get the filename and path and it is marked as
available. I tried the Sun tutorial that is a simple app of the
following:

import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("in.txt");
File outputFile = new File("out.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
System.out.println(c);
out.write(c);
in.close();
out.close();
}
}

I've tried to implement the meat of that into my own app but I don't
know how to get the data that is coming out of read() into the TextArea
since its append() method requires a String.

Does anyone know how to make that type of a conversion?
thanks

There are several ways. You could read the file as characters with
FileReader.read(char[] buffer) and then use the String constructor
String(char[] buffer, int offset, int count) or use a BufferedReader and
read in Strings directly.

BufferedReader br = new BufferedReader(new FileReader("filename"));
String str;
while ((str = br.readLine()) != null)
// append the string to the TextArea

The other option that looks a lot like what you have would be:

StringBuffer sb = new StringBuffer();
int c;
while ((c = in.read()) != -1)
sb.append(c);
String string = sb.toString();
// set your TextArea's string here

Appending Strings to Strings is fairly slow and if it can be avoided it
should. If you are only reading a dozen or so strings it won't matter
and then code simplicity is more important than speed. If you have to
do that use the StringBuffer as it is much faster.
 
T

Thomas Weidenfeller

Brandon said:
I'm trying to create a simple "notepad" type program.

Look into the demo/jfc/Notepad/ directory of your Sun JDK installation.

/Thomas
 
N

ninhoa

Use a BufferedReader over the FileReader, it has a readLine() method
that returns a String.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top