写一个简单的程序,实现从电脑中的一个盘里的文件中往程序中输入内容。
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");
// FileInputStream 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();
} }
在F盘写一个简单的txt文件如下
程序运行后:
结果显示:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
} }