This discussion is archived
2 Replies Latest reply: Dec 1, 2009 10:37 AM by 843807 RSS

Cannot read .txt file in Netbean

843807 Newbie
Currently Being Moderated
Hi all!
I am trying to read string from text file but i do not know why in netbean result is "File not found !" (file not found exception)
but in JCreator result is all text in file.
here my code
package HEQTCSDDemo;

public class LoadFile
{    
     //java.io.File file;
     java.io.BufferedReader buffer;     
    public void openFile(String filePath)
    {            
        try{
        buffer = new java.io.BufferedReader(new java.io.FileReader(filePath));
        }catch(java.io.FileNotFoundException e)
        {
            System.out.println("File not found!");
            System.exit(1);
        }
    }
    public String[] readFile()
    {
        String allSchedules = new String();
        String[] scheSet;
        String line;
        try
        {           
           while((line = buffer.readLine()) != null)
           {
               allSchedules += line;
           }
        }catch(java.io.IOException ioe)
        {
            ioe.printStackTrace();
        }
        catch(java.util.NoSuchElementException nse)
        {
            System.err.println( "File improperly formed." );
            System.exit( 1 );
        }
        catch ( java.lang.IllegalStateException stateException )
        {
           System.err.println( "Error reading from file." );
           System.exit( 1 );
       }
        java.util.StringTokenizer scheSetToken = new java.util.StringTokenizer(allSchedules, ";");
        int numberSchels = scheSetToken.countTokens();
        scheSet = new String[numberSchels];
        int count = 0;
        while(scheSetToken.hasMoreTokens())
           scheSet[count++] = scheSetToken.nextToken();
        return scheSet;
    }
    public void closeFile() throws java.io.IOException
    {
        if(buffer != null)
            buffer.close();
    }
    public static void main(String args[])
    {
        LoadFile lf = new LoadFile();
        lf.openFile("Lich.txt");
        String[] result = lf.readFile();
        for(int i = 0; i < result.length; i++)
            System.out.println(result);
}
}
and "Lich.txt"  file like
monday;
tuesday;
wednesday;
Edited by: phamthao on Nov 26, 2009 8:06 PM

Edited by: phamthao on Nov 26, 2009 8:29 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 1. Re: Cannot read .txt file in Netbean
    793415 Pro
    Currently Being Moderated
    Subject: Cannot read .txt file in Netbean

    Did you mean Netbeans? Or was it some other IDE that we (on the forums) do not support?
    phamthao wrote:
    ..I am trying to read string from text file but i do not know why in netbean result is "File not found !" (file not found exception)
    but in JCreator result is all text in file.
    Obviously the two environments are different. But since nobody here provides support for Netbean, netbean, Netbeans or JCreator, you are out of luck unless you intend to figure this out using either:
    a) The SDK tools used from the command line, or
    b) An Ant build script, which is usually just a thin veneer over the SDK tools we might use from the command line.
  • 2. Re: Cannot read .txt file in Netbean
    843807 Newbie
    Currently Being Moderated
    The most common problem for not being able to find a file, is that it is not in the correct location. You have 2 scenarios that will play out for you:

    1 - you do not have the file in the root project folder or a subfolder with appropriat path or supply a fully qualified path to the folder (ie your location if wrong).

    2 - you are trying to JAR the file and use the included copy of the file, but you need to use loadresources to read from your file that is included in your JAR.