I am trying to read my text file "inputFile.txt" but system shows the error below. Can anyone help me to solve this error? Thank you!
error: incompatible types: FileInputStream cannot be converted to Reader Yylex yy = new Yylex(fin);
//create file object
File infile = new
File("C://name//test_jflex//inputFile.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(infile);
Yylex yy = new Yylex(fin);
Yytoken t;
while ( (t = yy.yylex()) != null )
System.out.println(t);
fin.close();
}
The two java.io.Reader and java.io.FileInputStream are incompatible. This is because FileInputStream works with bytes and Reader is interface for character streams. If you go to https://docs.oracle.com/javase/8/docs/api/java/io/FileInputStream.html you will see that FileInputStream does not implement Reader. That why you need to choose either use Reader + his implementation classes or use FileInputStream.
Example with FileInputStream: https://www.mkyong.com/java/how-to-read-file-in-java-fileinputstream/
Example with Reader's one implementation java.io.BufferedReader: How to use Buffered Reader in Java
P.S. Please close all the streams appropriately. Your fin.close(); should not be closed in try part!
Good Luck!