Results 1 to 2 of 2
- 05-06-2010, 07:44 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
in = new BufferedReader( new FileReader(storageFile)) ; // error
hi, i've a code below :
/*
* loadTxt.java
*
* Created on May 7, 2010, 12:43 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package tampilan;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.*;
import java.lang.*;
/**
*
* @author nur
*/
public class loadTxt {
/** Creates a new instance of loadTxt */
public loadTxt() {
String storageFile;
ArrayList creatureList = new ArrayList() ;
BufferedReader in = null ;
try {
in = new BufferedReader( new FileReader(storageFile)) ; // error on this row :(
String line = "" ;
while( ( line = in.readLine() ) != null )
{
StringTokenizer stok = new StringTokenizer( line, "\t" ) ;
if( stok.hasMoreTokens() )
{
String alienType = stok.nextToken() ;
System.out.println(alienType);
/*AlienCreature creature = (AlienCreature)Class.forName( alienType ).newInstance() ;
creature.readFromTokenizer( stok ) ;
creatureList.add( creature ) ;*/
}
}
}
catch( Exception ex )
{
// catch all exceptions as one. This is bad form imho
ex.printStackTrace() ;
}
finally
{
try { if( in != null ) in.close() ; } catch( IOException ex ) {}
}
}
}
it show error :(
i don't know what's wrong with my code
could you help please.. ?
- 05-06-2010, 10:06 PM #2
the storageFile has not been initialized, so you can't use the string storageFile at line 30 :). i used the code above and myfile.txt was readed. the only changes i made is the assignment to storageFile and i added the main method so that i can run the class loadTxt. if you want to keep the class more generic you can pass a string that contains the path and file you want to read to the constructor and then assign the argument to storageFile.
Java Code:import java.util.ArrayList; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.*; import java.util.*; import java.lang.*; /** * * @author nur */ public class loadTxt { /** Creates a new instance of loadTxt */ public loadTxt() { String storageFile = "c:/temp/myfile.txt"; ArrayList creatureList = new ArrayList(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(storageFile)); // error on // this row String line = ""; while ((line = in.readLine()) != null) { StringTokenizer stok = new StringTokenizer(line, "\t"); if (stok.hasMoreTokens()) { String alienType = stok.nextToken(); System.out.println(alienType); /* * AlienCreature creature = (AlienCreature)Class.forName( * alienType ).newInstance() ; creature.readFromTokenizer( * stok ) ; creatureList.add( creature ) ; */ } } } catch (Exception ex) { // catch all exceptions as one. This is bad form imho ex.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException ex) { } } } public static void main(String[] args) { loadTxt l = new loadTxt(); } }
Similar Threads
-
BufferedReader error?
By Umogrim in forum New To JavaReplies: 2Last Post: 04-27-2010, 07:58 PM -
FileReader problems
By Maikl in forum New To JavaReplies: 3Last Post: 12-14-2009, 11:51 AM -
BufferedReader error cannot resolve symbol
By SwEeTAcTioN in forum New To JavaReplies: 12Last Post: 10-22-2009, 05:22 AM -
FileReader help
By emp in forum New To JavaReplies: 1Last Post: 07-28-2009, 04:41 AM -
add FileReader to GUI
By VinTiger in forum New To JavaReplies: 8Last Post: 05-11-2009, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks