/*
* 项目根路径下有个questions.txt文件内容如下:
5+5 [ 5, 5]
150-25
155*155
2555/5
要求:读取内容计算出结果,将结果写入到results.txt文件中
*/
public class Test5 {
public static void main(String[] args) throws IOException {
//高效字符流来读取文件
BufferedReader br = new BufferedReader(new FileReader("questions.txt"));
//创建集合对象
ArrayList<String> als = new ArrayList<String>();
//读数据
String line;
while ((line = br.readLine()) != null) {
//我把这些读到的数据写到集合中
als.add(line);
}
//获得集合的0号索引元素
String str1 = als.get(0);
//切割这个元素
String[] split1 = str1.split("\\+");
//String ==> int
int result1 = Integer.parseInt(split1[0]) + Integer.parseInt(split1[1]);
//写数据 字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
//那我就拼字符串
bw.write(str1+"="+result1 + "");
bw.newLine();
//获得集合的1号索引元素
String str2 = als.get(1);
//切割这个元素
String[] split2 = str2.split("\\-");
//String ==> int
int result2 = Integer.parseInt(split2[0]) - Integer.parseInt(split2[1]);
//那我就拼字符串
bw.write(str2+"="+result2 + "");
bw.newLine();
//获得集合的2号索引元素
String str3 = als.get(2);
//切割这个元素
String[] split3 = str3.split("\\*");
//String ==> int
int result3 = Integer.parseInt(split3[0]) * Integer.parseInt(split3[1]);
//那我就拼字符串
bw.write(str3+"="+result3 + "");
bw.newLine();
//获得集合的3号索引元素
String str4 = als.get(3);
//切割这个元素
String[] split4 = str4.split("\\/");
//String ==> int
int result4 = Integer.parseInt(split4[0]) / Integer.parseInt(split4[1]);
//那我就拼字符串
bw.write(str4+"="+result4 + "");
bw.newLine();
br.close();
bw.close();
}
}