📄 textfilescannerdemo.java
字号:
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class TextFileScannerDemo
{
public static void main(String[] args)
{
System.out.println("I will read three numbers and a line");
System.out.println("of text from the file morestuff.txt.");
Scanner inputStream = null;
try
{
inputStream =
new Scanner(new FileInputStream("morestuff.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("File morestuff.txt was not found");
System.out.println("or could not be opened.");
System.exit(0);
}
int n1 = inputStream.nextInt( );
int n2 = inputStream.nextInt( );
int n3 = inputStream.nextInt( );
inputStream.nextLine(); //To go to the next line
String line = inputStream.nextLine( );
System.out.println("The three numbers read from the file are:");
System.out.println(n1 + ", " + n2 + ", and " + n3);
System.out.println("The line read from the file is:");
System.out.println(line);
inputStream.close( );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -