Write a simple program that allows you to enter content into a program from a file in a disk on your computer.
Package Com.liaojianya.chapter5;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import java.util.scanner;/** * This program demonstrates Scanner class * @author LIAO Jianya * */public class Scannertest2{public static void Main (string[] args) throws Filenotfoundexception{//file file = new file ("f:\\", "HelloWorld.txt");//fileinpu TStream in = new FileInputStream (file); FileInputStream in = new FileInputStream ("F:\\helloworld.txt"); Scanner s = new Scanner (in), while (S.hasnextline ()) {System.out.println (S.nextline ());} S.close ();}}
Write a simple TXT file on the F drive as follows
After the program runs:
The results show: public class helloworld{public static void Main (string[] args) {System.out.println ("Hello world!");} }
java--input Stream FileInputStream