-2

I am not able to read notepad file in java dont know what i am doing wrong

public class Reader {

    public static void Main(String str) throws IOException{

        File f = new File("e:\\test.txt");

        FileReader rea = new FileReader(f);

        BufferedReader br = new BufferedReader(rea);

        System.out.println(br.readLine());

        br.close();

    }

}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Drake
  • 17
  • 5
  • 4
    Be specific: how do you know that you can't "read notepad file"? – user100464 Jul 16 '16 at 13:44
  • 2
    BTW, you shouldn't name your classes with names already used by standard Java classes. Change your class name to something like ReaderExample. – Pshemo Jul 16 '16 at 13:45
  • You should add at least one `try/chatch`-block – pzaenger Jul 16 '16 at 13:45
  • 1
    Check out this link, http://www.homeandlearn.co.uk/java/read_a_textfile_in_java.html – Harshal Benake Jul 16 '16 at 13:45
  • 1
    Java is case-sensitive. You should name main method as `main` not `Main`. Also main method expects array of arguments `main(String[] str)` not one argument. Voting to close as typo. – Pshemo Jul 16 '16 at 13:49

1 Answers1

0

First of all you must mention what error are you getting in your question

I just copied your code and corrected it.So the errors in your code are :

  1. public static void Main

It should be public static void main (m is small)

  1. In arguments of the main method you are sending a string which is wrong. As in java ,main method has array of String as its arguments.So it should be

    public static void main(String str[])

And also make sure that the file you are reading from exists otherwise an exception will be thrown and your code won't run.

  • Not all questions belong to Stack Overflow. From: http://stackoverflow.com/help/on-topic "Some questions are still off-topic... (2) Questions about a problem that can no longer be reproduced or that was caused by a simple *typographical* error..." and `M` vs `m` and lack of `[]` is typo while rewriting example into IDE. Anyway questions which are off-topic can be automatically deleted, unless they have positively scored/accepted answer, so avoid posting answers in such questions. If you want to help OP post comment (you will be able to do it when you will get 50 reputation points). – Pshemo Jul 16 '16 at 23:20
  • Thanks for the info @Pshemo – Pushpinder Singh Grewal Jul 17 '16 at 01:54
  • Why i got negative reputation for deleting this answer? – Pushpinder Singh Grewal Jul 26 '16 at 15:53
  • If you mean this answer it is because it got +1 and -1. Each up-vote is worth 10 reputation and each down-vote -2 reputation. So you earned 10-2=8 reputation on this answer, but each post you removed no longer affects your reputation score. – Pshemo Jul 26 '16 at 16:00
  • As you said that the question is not according to the Stack Overflow standards so i thought of deleting my answer but i didn't knew that it had an upvote and a downvote also .Why am i not able to see that? – Pushpinder Singh Grewal Jul 26 '16 at 16:04
  • You will be able to see detailed scores when you will get 1000 reputation points: http://stackoverflow.com/help/privileges/established-user – Pshemo Jul 26 '16 at 16:15
  • Thank you . this problem is resolved now ... Thanks everyone – Drake Jul 28 '16 at 08:17