📄 ctest.java
字号:
import java.io.*;
import java.util.*;
public class cw3c
{
static String fname;
static String line;
//print method output line with print or println reserved words
public static void print(String fname)
{
int count=0;
try{
//read in file while there is end statement
//output stop when encount end reserved word
BufferedReader inFile=new BufferedReader(new FileReader(fname));
String nline;
while((nline=inFile.readLine())!=null&&(nline.equalsIgnoreCase("end"))!=true)
{
//initialise StringTokenizer
StringTokenizer nfile=new StringTokenizer(nline);
//seperate line with single word from others
if(nfile.countTokens()>1)
{
int x=nline.indexOf(" "); //get index of first space
String fStr=nline.substring(0,x); //get reserved words
String sStr=nline.substring((x+1),nline.length()); //left of line
//if-else for search print/println reserved words
//excute print/println statement
if((fStr.equals("#"))==true)
{
System.out.println(sStr);
count++;
}
else if((fStr.equalsIgnoreCase("println"))==true)
{
System.out.println(sStr);
count++;
}
else if((fStr.equalsIgnoreCase("print"))==true)
{
System.out.println(sStr);
count++;
}
else
System.out.print("");
}
//search for line with single word of println
else
{
if((nline.equalsIgnoreCase("println"))==true)
System.out.println(" ");
else
System.out.print("");
}
}
}catch(IOException e){
System.out.println("an error ["+e+"]");
}
//output final statement and count the number of line printed
System.out.println("TPL Finished ok ["+(count+1)+" lines processed]");
}
//exit method for stop application when no end statement
public static void exit()
{
System.out.println("no end Statement");
System.exit(0);
}
public static void main(String args[])
{
//initialise x value as false
boolean x=false;
boolean y;
if(args.length>=1)
{
try{
BufferedReader inFile=new BufferedReader(new FileReader(args[0]));
for(int i=0;i<args.length;i++) //use for loop get file name
fname=args[i];
//use while loop search for end statement
//get x final value
while((line=inFile.readLine())!=null)
{
//y value changes according to line content
y=line.equalsIgnoreCase("end");
//final y value depends on last line
//if end exits, y equals to true
if( y==true)
{
x=y; //x equals to true, end exits
print(fname); //invoke print method
}
}
if(x==false)
exit(); //x equals to false, invoke exit method
}catch(IOException e){
System.out.println("an error ["+e+"]");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -