new problems trying to view the contents of a text file in JTextArea

howdy.

i am trying to view the contents of a text file in a JTextArea. The following program compiles ok. But when I run it, it doesnt display the contents of Order.txt in the text area. When I press the View Order, the number 68 appears in the text area instead of 2 Battlestar Galactica Xbox 360 Games and 12 Scooter Mind the Gap CDs. how can i modify the program so that 2 Battlestar Galactica Xbox 360 Games and 12 Scooter Mind the Gap CDs would be displayed in the text area instead of 68?

import java.awt.BorderLayout;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

publicclass ViewOrderextends JFrame{

JPanel pnlText, pnlBody, pnlFooter;

JButton btnViewOrder;

JButton btnReturnToOrderSystem;

JLabel jl;

JTextArea jtaDescription =new JTextArea();

Container contentpane;

public ViewOrder(){

super("View Order");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlText =new JPanel();

pnlBody =new JPanel();

pnlFooter =new JPanel();

jtaDescription =new JTextArea();

jtaDescription.setFont(new Font("Serif", Font.PLAIN, 14));

// Set lineWrap and wrapStyleWord true for the text area

jtaDescription.setLineWrap(true);

jtaDescription.setWrapStyleWord(true);

jtaDescription.setEditable(false);

pnlBody.add(jtaDescription);

// Create a scroll pane to hold the text area

JScrollPane scrollPane =new JScrollPane(jtaDescription);

// Set BorderLayout for the panel, add label and scrollpane

pnlBody.add(scrollPane, BorderLayout.CENTER);

jl =new JLabel("Text retrieved from file:");

btnViewOrder =new JButton("View Order");

btnReturnToOrderSystem =new JButton("Return to Order System Menu");

pnlText.add(jl);

pnlFooter.add(btnViewOrder);

pnlFooter.add(btnReturnToOrderSystem);

contentpane.add(pnlText,BorderLayout.NORTH);

contentpane.add(pnlBody,BorderLayout.CENTER);

contentpane.add(pnlFooter,BorderLayout.SOUTH);

pack();

setVisible(true);

btnViewOrder.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

String s =null;

//Read from file

try{

String inputFileName ="Order.txt";

File inputFile =new File(inputFileName);

FileInputStream in =new FileInputStream(inputFile);

byte bt[] =newbyte[(int)inputFile.length()];

int tmp = in.read(bt);

String ms = Integer.toString(tmp);

setDescription(ms);

s =new String(bt);

in.close();

}

catch(java.io.IOException ex){

System.out.println("Cannot read from file");

}

}

});

btnReturnToOrderSystem.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//OrderSystem os = new OrderSystem();

//os.setVisible(true);

}

});

}

/** Set the text description */

publicvoid setDescription(String text){

jtaDescription.setText(text);

}

publicstaticvoid main(String[] args){

new ViewOrder();

}

}

[5794 byte] By [james-mcfaddena] at [2007-11-27 10:23:06]
# 1

Don't you think you should solve one problem before moving on to the next?

georgemca at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 2

is ur file containing records,can u send the sample of ur file?

anya_aaa at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 3

Here are the records that are in Order.txt.

2 Battlestar Galactica Xbox 360 Games

12 Scooter Mind the Gap CDs

james-mcfaddena at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 4

that means ur file is a sequential acess file.u should read the contents of ur file line by line and put a newline character at the end of each line to show the contents properly in the text area.

but i don't understand from where r u getting the number 68?it doesn't exist in ur file.

anya_aaa at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 5

where is ur code to display the contents in ur text area?

byte bt[] = new byte[(int)inputFile.length()];

int tmp = in.read(bt);

i see this code and by this i understand will return the length of ur file.

anya_aaa at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 6

Your I/O is all wrong. Don't read text data as binary data. After you create a FileInputStream, wrap it in an InputStreamReader, then wrap that in a BufferedReader. Use the BR's readLine() method to get a line of text. Then pass that to the setDescription() method. Currently you're displaying the length of the file in bytes, which is where the 68 comes from.

http://java.sun.com/docs/books/tutorial/essential/io/

hunter9000a at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...
# 7

hello.

thanks for the help. i did what you told me to do as much as i possibly could. but when i run the program and press View Order this time, "null" appears in the Compile Messages section of the jGRASP console. what have i done wrong this time?

import java.awt.BorderLayout;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

public class ViewOrder extends JFrame{

JPanel pnlText, pnlBody, pnlFooter;

JButton btnViewOrder;

JButton btnReturnToOrderSystem;

JLabel jl;

JTextArea jtaDescription = new JTextArea();

Container contentpane;

public ViewOrder(){

super("View Order");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlText = new JPanel();

pnlBody = new JPanel();

pnlFooter = new JPanel();

jtaDescription = new JTextArea();

jtaDescription.setFont(new Font("Serif", Font.PLAIN, 14));

// Set lineWrap and wrapStyleWord true for the text area

jtaDescription.setLineWrap(true);

jtaDescription.setWrapStyleWord(true);

jtaDescription.setEditable(false);

pnlBody.add(jtaDescription);

// Create a scroll pane to hold the text area

JScrollPane scrollPane = new JScrollPane(jtaDescription);

// Set BorderLayout for the panel, add label and scrollpane

pnlBody.add(scrollPane, BorderLayout.CENTER);

jl = new JLabel("Text retrieved from file:");

btnViewOrder = new JButton("View Order");

btnReturnToOrderSystem = new JButton("Return to Order System Menu");

pnlText.add(jl);

pnlFooter.add(btnViewOrder);

pnlFooter.add(btnReturnToOrderSystem);

contentpane.add(pnlText,BorderLayout.NORTH);

contentpane.add(pnlBody,BorderLayout.CENTER);

contentpane.add(pnlFooter,BorderLayout.SOUTH);

pack();

setVisible(true);

btnViewOrder.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = null;

System.out.println(s + "\n");

//Read from file

try{

String inputFileName = "Order.txt";

File inputFile = new File(inputFileName);

FileInputStream in = new FileInputStream(inputFile);

BufferedReader iS = new BufferedReader(new InputStreamReader(in));

String iL = "";

iL = iS.readLine();

setDescription(iL);

iS.close();

}

catch(java.io.IOException ex){

System.out.println("Cannot read from file");

}

}

});

btnReturnToOrderSystem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

//OrderSystem os = new OrderSystem();

//os.setVisible(true);

}

});

}

/** Set the text description */

public void setDescription(String s) {

jtaDescription.setText(s);

}

public static void main(String[] args){

new ViewOrder();

}

}

james-mcfaddena at 2007-7-28 17:20:00 > top of Java-index,Java Essentials,Java Programming...