您的位置:首页 > 编程语言 > Java开发

java——输入流FileInputStream

2016-07-06 20:11 246 查看

写一个简单的程序,实现从电脑中的一个盘里的文件中往程序中输入内容。

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!");
}

}

  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: