📄 cw3b.java
字号:
import java.io.*;
import java.util.*;
public class cw3b
{
//declare class variable
static String fname;
static String line;
//print out file into console when there is "end" statement
//method take fname as parameter
static void print(String fname)
{
//declare count variable for counting numbers of line printed
int count=0;
try{
//use BufferedReader and FileReader read in file
BufferedReader inFile=new BufferedReader(new FileReader(fname));
String nline;
//use while loop to read in every line
while(( nline=inFile.readLine())!=null&&(nline.equalsIgnoreCase("end"))!=true)
{
System.out.println(nline);
count++;
}
}catch(IOException e){
System.out.println("an error ["+e+"]");
}
//print out final statement into console
System.out.println("TPL Finished ok ["+(count+1)+" lines processed]");
}
//method exit for stop application when there is no end Statement
private static void exit()
{
//print out "no end" error message
System.out.println("No end statement");
System.exit(0); //exit application
}
//main method
public static void main(String args[])
{
//declare boolean x,y
//initialize x value as false
boolean x=false;
boolean y;
//take commandline args as parameter
if(args.length>=1)
{
try{
BufferedReader inFile=new BufferedReader(new FileReader(args[0]));
//fname is command line args
for(int i=0;i<args.length;i++)
fname=args[i];
//use while loop to search for "end" statement
//if yes, x vulue changes to true
while((line=inFile.readLine())!=null)
{
y=line.equalsIgnoreCase("end"); //y values changes according to line content
if( y==true) //last line content decides y final values
{
x=y; //x equals to y when y is true
print(fname); //invoke print method
}
}
if(x==false) //no end statement
exit(); //invoke exit method
}catch(IOException e){
System.out.println("an error ["+e+"]");
}
}
else
System.out.println("no such file"); //no command line args print out error message
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -