0

i want read text file in java8, i am getting error "Type mismatch: cannot convert from FileReader to Reader". If I change Reader class to FileReader than I get error "The constructor BufferedReader(FileReader) is undefined" My statements are

Reader fr = new FileReader("testfile.txt");
BufferedReader br =  new BufferedReader(fr);

Please suggest

10
  • 4
    Please provide a minimal reproducible example. My guess is that you've got a class called FileReader, so that the FileReader your code refers to isn't actually java.io.FileReader. Commented Dec 1, 2017 at 8:15
  • @JonSkeet more than a guess, I'd wager quite a bit that this is the only possible explanation for the two errors that the OP describes. Commented Dec 1, 2017 at 8:15
  • I have used import java.io.*; Commented Dec 1, 2017 at 8:18
  • 2
    @ErwinBolwidt: It's possible they have a Reader class instead of a FileReader class ;) (The conversion would be equally infeasible...) Commented Dec 1, 2017 at 8:18
  • 1
    I have used import java.io.* Well, show us the code. What you have there isn't enough. Commented Dec 1, 2017 at 8:19

2 Answers 2

1

To confirm that you are having a class with the name FileReader, just use the full class name in the code :

java.io.Reader fr = new java.io.FileReader("testfile.txt");
java.io.BufferedReader br =  new java.io.BufferedReader(fr);

This will assure that you use the specific class and not a yourPackage.FileReader class.

Then, since only FileReader seems to be problematic, you can clean it a bit like :

import java.io.*

...

Reader fr = new java.io.FileReader("testfile.txt");
BufferedReader br =  new BufferedReader(fr);

Only specifying the FileReader full name.

NOTE:

using Class.GetPackage, you should find out which class you are using.

System.out.println(FileReader.class.getPackage());

Explanation:

JLS - 7.5. Import Declarations

The scope and shadowing of a type or member imported by these declarations is specified in §6.3 and §6.4.

6.4.1. Shadowing

A package declaration never shadows any other declaration.

A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:

  • any top level type named n declared in another compilation unit of p
  • any type named n imported by a type-import-on-demand declaration in c
  • any type named n imported by a static-import-on-demand declaration in c

Example

A
A.Run
A.Test
B
B.Test

In A.Run.java

System.out.println(Test.class.getPackage());

Here is the output :

  • Without import : Package A
  • Without import import B.* : Package A
  • Without import import B.Test : Package B
Sign up to request clarification or add additional context in comments.

4 Comments

This is a reasonable test, I'd like to see the OP try this as well.
@MDHUSENPAREET Did some edit to explain why using an import * will not work.
Thanks for the detailed explanation. It has given me valuable insights on packages.
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); This throws a null pointer exception. Any pointers how to eliminate this error? Exception in thread "main" java.lang.NullPointerException at java.io.FileOutputStream.<init>(FileOutputStream.java:203) at java.io.FileOutputStream.<init>(FileOutputStream.java:101) at java.io.FileWriter.<init>(FileWriter.java:63)
0

You're probably importing something other than java.io.BufferedReader and java.io.Reader.

This works

package com.company;

import java.io.*;

public class Main {
        public static void main(String[] args) throws Exception {
            Reader fr = new FileReader("testfile.txt");
            BufferedReader br =  new BufferedReader(fr);
        }
}

Please check your imports;

6 Comments

No, that code wouldn't work: the FileReader constructor can throw an IOException which you're neither declaring nor catching, and you haven't declared a class. If you're going to provide an example you claim compiles, please make sure it actually does.
I get error "Type mismatch: cannot convert from FileReader to Reader"
The above code does compile for me (I didn't test it). The OP should show us his code because his question doesn't really make any sense.
Do you have a class called Reader or FileReader in your package directory? That seems to be the only possible thing to me now.
Also, if you've been making many small projects as part of a series of programs (say homework assignments from a class) it's possible to have an old class named FileReader still present from a previous piece of code. Might make sense to start a new project, and make sure you've got only the basic Java runtime at the start.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.