6

Here's part of my code

try{

    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));

    while ((line= in.readLine())!="exit"){

    System.out.println("Enter command");
    line=in.readLine();
    Command currentCommand=new Command(line);



    File currentFile= new File(currentCommand.getLsPath());

The method currentCommand.getLsPath() returns a string, which is mendatory for the File Constracture, and still I get this error: File cannot be resolved to a type

What is the problem?

Michael Pryor
  • 25,046
  • 18
  • 72
  • 90
Unknown user
  • 44,551
  • 16
  • 38
  • 42

5 Answers5

17

Chances are you've just missed:

import java.io.File;

from the top of your source file.

You could use

 import java.io.*; 

I typically use single-type imports.

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

You need to include either

import java.io.File;

or

import java.io.*;

at the top of your source file.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

I received this error once, even though I had already imported java.io.File. I think it was some weird, temporary Eclsipse glitch. When I made other changes to the code, then saved it again, the error resolved itself.

matthewb
  • 1,323
  • 2
  • 12
  • 18
0

You most likely forgot to import java.io.File but essentially the compiler is telling you it can't find the "File" class.

See this java error page specific to "File cannot be resovled to a type" for more info.

Michael Pryor
  • 25,046
  • 18
  • 72
  • 90
0

i also got that error and by giving try catch block just before creating object of FileReader , FileWriter not before creating a variable of type FileReader or FileWriter could resolve that issue and just make sure to close the resource .

Ankit
  • 1
  • 1
    I don't think so. The solution for the type 'File' not being defined is to define it (via an import statement). – Arfur Narf Jun 20 '23 at 00:22