Said first create test.txt in the E drive, says "hello world"
Then write the test class as follows FileIOTest
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileIOTest { public static void main(String[] args) { File file = new File("E:/test.txt"); FileInputStream is = null; try { is = new FileInputStream(file); int temp; while ((temp = is.read()) != -1) { System.out.print((char) temp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Results are as follows