file io - What's wrong with this code in java that uses FileReader? -


i have been trying learn filereader , hence wanted test out. created class constructor takes in string(name of file) , creates file , reads , prints first character out, code not working , showing errors. java code.

package test_3;  import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception;  public class files {     public files(string s) throws filenotfoundexception, ioexception{         file f = new file(s);         filereader fr = new filereader(f);         system.out.println(fr.read());     }      public static void main(string args[]) throws filenotfoundexception,   ioexception{         files myfile = new files("input.txt");     } } 

this error information

exception in thread "main" java.io.filenotfoundexception: input.txt (the system cannot find file specified)     @ java.io.fileinputstream.open0(native method)     @ java.io.fileinputstream.open(unknown source)     @ java.io.fileinputstream.<init>(unknown source)     @ java.io.filereader.<init>(unknown source)     @ test_3.files.<init>(files.java:11)     @ test_3.files.main(files.java:16) 

because file cannot found. should path java looking file, so.

system.out.print(system.getproperty("user.dir")); 

and place "input.txt" within directory (the directory printed when code ran).

alternatively, use full absolute path input.txt


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -