📄 writefileexample.java
字号:
import java.io.*;
import java.util.*;
class wordAna1{
String temp=""; //用来存入临时字符串
public wordAna1(){
/**通过BufferedReader函数从外部读入txt文件,运行完后输出vector*/
File file=new File("d:\\text.txt");
Vector vector1 = new Vector();
try {BufferedReader input=new BufferedReader(new FileReader(file));
String text;
while((text=input.readLine())!=null)
vector1.add(text);
} catch(Exception ex){}
/**因为引入的vector前后分别有一个”[]”,通过substring函数截取从第二个到倒数第二个的字符存入t
emp*/
temp=vector1.toString();
String tempStr=temp.substring(1,temp.length()-1);
char []methodCha=tempStr.toCharArray();
temp="";
/**开始处理函数,通过一个while循环循环整个函数*/
int I=0;
while(I<methodCha.length)
{
if(methodCha[0]==61&&methodCha[1]==61)
System.out.println("< 1,"+methodCha[0]+methodCha[1]+">");
/**判断是否为界符或运算符,如果是则输出对应序号*/
else if(((methodCha[I]>=33&&methodCha[I]<=47)
||(methodCha[I]>=58)&&methodCha[I]<=63)
&&methodCha[I]!=44)
{System.out.println("< "+((int)methodCha[I]-32)+","+methodCha[I]+">");I++;}
/**判断是否为字母(包括大小写)或数字,如果是通过一个while循环将字符存入temp字符串中*/
else if(((48<=methodCha[I]&&methodCha[I]<=57)
||(65<=methodCha[I]&&methodCha[I]<=90)
||(97<=methodCha[I]&&methodCha[I]<=122)
))
{while((48<=methodCha[I]&&methodCha[I]<=57)
||(65<=methodCha[I]&&methodCha[I]<=90)
||(97<=methodCha[I]&&methodCha[I]<=122)
)
{temp+=methodCha[I];
I++;
}
/**判断temp是否为空,如果非空,则说明有字符存入temp字符串中,*如果第一个字符为字母,则说明temp所存入的是一变量名或关键字*/
if(!temp.equals(""))
{char []tempCha=temp.toCharArray();
if((65<=tempCha[0]&&tempCha[0]<=90)
||(97<=tempCha[0]&&tempCha[0]<=122))
{if(isKey(temp)!=0)System.out.println("< "+temp+","+ isKey(temp)+ ">");
else System.out.println("< ID ,"+temp+" >");
}
/**如果temp的长度小于2,说明存入的是数字0,输出*/
else if(temp.length()<2)System.out.println("< NUM,"+temp+" >");
/**其它情况为整数,输出,将temp清空*/
else System.out.println("<NUM,"+temp+" >");
temp="";
}
}
/**非上述情况,为防止死循环,直接I++跳出,进入下次循环*/
else I++;
}
}
/**isKey函数用来判断是否为关键字,如果是返回相应序号,假则返回0*/
public int isKey(String str)
{
int flag=0;
if(str.equals("if")) flag=5;
if(str.equals("else")) flag=6;
if(str.equals("while")) flag=7;
if(str.equals("do")) flag=8;
if(str.equals("begin")) flag=9;
if(str.equals("call")) flag=10;
if(str.equals("const")) flag=11;
if(str.equals("odd")) flag=12;
if(str.equals("end")) flag=13;
if(str.equals("procedure")) flag=14;
if(str.equals("read")) flag=15;
if(str.equals("var")) flag=16;
if(str.equals("write")) flag=17;
return flag;
}
public static void main(String args[])
{ System.out.println("===============================");
System.out.println("===============================");
System.out.println("==....ID.. 代表...字符串......==");
System.out.println("==....NUM.. 代表...十进制数字..==");
System.out.println("==....界符位于左边,右边为对应序号....==");
System.out.println("==....运算符位于左边,右边为对应序号....==");
System.out.println("==....关键字位于左边,右边为对应序号==");
System.out.println("===============================");
System.out.println("===============================");
wordAna1 wordAnalasis= new wordAna1();
try
{
File aFile=new File("WriteExample.txt"); //指定文件名
//建立输出流
FileOutputStream out= new FileOutputStream(aFile);
byte[] b=new byte[4096];
String str=("< ID ,"+" >");
b=str.getBytes(); //进行String到byte[]的转化
out.write(b); //写入文本内容
}
catch (IOException e)
{
System.out.println(e.toString());
}
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -