Bamadeva
Bamadeva

Reputation: 335

File program shows error

For the first time my file program ran very well. But then after when I want to run it shows error. What might be the reason?

import java.io.FileInputStream;
import java.io.IOException;

public class FileSentenceRead {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        /*Scanner sn=new Scanner(System.in);
        System.out.println("Enter file name");
        String fname=sn.next();*/
        FileInputStream fis=new FileInputStream("textfile.text");
        int size=fis.available();
        byte bt[]= new byte[size];
        fis.read(bt);
        String pr=new String(bt);
        System.out.println(pr);
        String[] pr1=pr.split("\\s|\\.");
//      @SuppressWarnings("rawtypes")
//      HashSet set=new HashSet();
        StringBuffer s=null;
        StringBuffer ss=new StringBuffer();
        for(int i=0;i<pr1.length;i++){
            s= new StringBuffer(pr1[i]);
//          System.out.print(s.reverse()+" ");
            s=s.reverse();
            ss.append(s+" ");
        }
        System.out.println(ss);
    }
}

Here is my error message

at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

Upvotes: 0

Views: 82

Answers (3)

MaVRoSCy
MaVRoSCy

Reputation: 17839

yes Himanshu Mohta is correct. I have checked the code with

FileInputStream fis=new FileInputStream("c:\\tmp\\textfile.text");

and some text here as text in the text file and it returns :

some text here

emos txet ereh

So, nothing wrong with the code, check your filesystem folder security read/write permissions

Upvotes: 0

Kuldeep Jain
Kuldeep Jain

Reputation: 8598

How are you running your class. Your main class name is FileSentenceRead and not textfile.text as you are seeing in your error message. I ran your code it runs fine on my machine always.

May be some issue with your launch configuration or in the command you are using to run it.

Upvotes: 0

Guillaume Polet
Guillaume Polet

Reputation: 47608

Could not find the main class : textfile.text

It looks like you are not executing the correct command line. Your command line call should be something like this (assuming you are executing it from where classes are generated):

java -cp . FileSentenceRead 

or if you put it in a package:

java -cp . com.foo.mypackage.FileSentenceRead

If you are running from Eclipse, you may have put your program arguments in the VM arguments, which will produce the same error. Then just move your program arguments in the correct field and remove them from the VM arguments field.

Upvotes: 1

Related Questions