Jump to content

Reading a Text file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
jmikeydarby1

jmikeydarby1

    Newbie

  • Members
  • Pip
  • 9 posts
I'm having huge trouble with this for some reason. I have a text file called route 1.txt

All I want it to do is read the file and print off. The java file and the txt file are in the same folder. I'm using Netbeans. Most of this code is copy and pasted...but it refuses to work so far. I need help as to why it will not work.

public class FileRead

{

    public static void main(String[] args)

    {

        File file = new File("route 1.txt");

        StringBuffer contents = new StringBuffer();

        BufferedReader reader = null;

 

        try

        {

            reader = new BufferedReader(new FileReader(file));

            String text = null;

 

            // repeat until all lines is read

            while ((text = reader.readLine()) != null)

            {

                contents.append(text)

                    .append(System.getProperty(

                        "line.separator"));

            }

        } catch (FileNotFoundException e)

        {

            e.printStackTrace();

        } catch (IOException e)

        {

            e.printStackTrace();

        } finally

        {

            try

            {

                if (reader != null)

                {

                    reader.close();

                }

            } catch (IOException e)

            {

                e.printStackTrace();

            }

        }

        

        // show file contents here

        System.out.println(contents.toString());

    }

}

Here's what I'm getting from Netbeans.

run:

java.lang.NoClassDefFoundError: pokemontest/FileRead (wrong name: Pokemontest/FileRead)

        at java.lang.ClassLoader.defineClass1(Native Method)

        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)

        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

Could not find the main class: pokemontest.FileRead.  Program will exit.

Exception in thread "main" 

Exception in thread "main" Java Result: 1

BUILD SUCCESSFUL (total time: 2 seconds)



#2
justinb09

justinb09

    Newbie

  • Members
  • PipPip
  • 27 posts
this is because yor class file name is not declared proper it should be the same as what you save your code as

#3
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Looks like the file names right, but your not in a package.

Have your got a package statement at the top of your java file?

#4
justinb09

justinb09

    Newbie

  • Members
  • PipPip
  • 27 posts
actualy that is correct that maybe the biggest error

#5
jmikeydarby1

jmikeydarby1

    Newbie

  • Members
  • Pip
  • 9 posts
Yea the package is correct. I'm getting no errors in Java Syntax. It just will not work.

#6
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Are you sure the package is right? From your code, I don't see a package line.

Also, check if you named the file correctly.

#7
jmikeydarby1

jmikeydarby1

    Newbie

  • Members
  • Pip
  • 9 posts
no I fixed it. My package was named right...What was wrong is that I had two files in the package with a main Class. That is what caused all my problems. Thanks to anyone who was trying to help me.