codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Reading from a file  gosi at 19:43 on Monday, December 19, 2005
 

I´m trying to make my GUI read from a file, and this code is a part of
my controlPanel class where I control all action in the GUI. I´m not sure
if I´m on the right path here, I get some error [Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException] in the line where "FileReader reader = new FileReader(file);" is.....

Hope this is enough to get some hints.

thx


public class ControlPanel extends JPanel{

static String file;
static FileWriter fw;
static BufferedWriter bw;
static PrintWriter outFile;

JButton start = new JButton ("Start");
JButton stop = new JButton("Stop");;
JButton step = new JButton("Step");


private ArrayList dataArray = new ArrayList();
private Point bestPlace = new Point();
private int scenario = 0;
private HashMap map = new HashMap();
int[] weights;
private JLabel fileName;


public ControlPanel() throws IOException

{

file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);


public void openCommand(File jonas)
{

dataArray.clear();
try
{

long length = file.length();
System.out.println(length);
FileReader reader = new FileReader(file);
String temp = Long.toString(length);
int size = Integer.parseInt(temp);
char[] cbuf = new char[size];
ArrayList list = new ArrayList();
String numbers = "";
try
{
reader.read(cbuf);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"Can´t read the file", "Error", JOptionPane.ERROR_MESSAGE);
file=null;
}

}
finally
{
System.out.print("oops");
}
}


  Re: Reading from a file  crwood at 03:05 on Tuesday, December 20, 2005
 

You had a missing curley brace after the constructor or the "openCommand" method inside of the constructor. Also needed to deal with a FileNotFoundException for the compiler.

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class CP extends JPanel
{
static String file;
static FileWriter fw;
static BufferedWriter bw;
static PrintWriter outFile;

JButton start = new JButton ("Start");
JButton stop = new JButton("Stop");;
JButton step = new JButton("Step");

private ArrayList dataArray = new ArrayList();
private Point bestPlace = new Point();
private int scenario = 0;
private HashMap map = new HashMap();
int[] weights;
private JLabel fileName;

public CP() throws IOException
{
file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
}

public void openCommand(File jonas)
{
dataArray.clear();
try
{
long length = file.length();
System.out.println(length);
FileReader reader = new FileReader(file);
String temp = Long.toString(length);
int size = Integer.parseInt(temp);
char[] cbuf = new char[size];
ArrayList list = new ArrayList();
String numbers = "";
try
{
reader.read(cbuf);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"Can´t read the file", "Error",
JOptionPane.ERROR_MESSAGE);
file=null;
}
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
finally
{
System.out.print("oops");
}
}

public static void main(String[] args) throws IOException
{
new CP();
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005