ファイルを読み込み、ファイル内の表式の値を計算します.

1003 ワード

import java.io.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestMain {
	
	public static void main(String[] args) {
		TestMain tm = new TestMain();
		tm.chage();
	}
	public void chage(){
		try {
			
			/*
			 *     ,          .
			 * out.txt   :20*(100+8*3/6)
			 * in.txt   :2080.0(   )
			 */
			File read = new File("d:\\out.txt");
			File write = new File("d:\\in.txt");
			FileReader fr = new FileReader(read);
			FileWriter fw = new FileWriter(write);
			
			BufferedReader br = new BufferedReader(fr);
			BufferedWriter bw = new BufferedWriter(fw);
			
			String str = br.readLine();
			//  JDK1.6      .
			ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
			
			System.out.println(se.eval(str).toString());
			bw.write(se.eval(str).toString());
			bw.close();
			br.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}