
正文
实时解析java代码 java解析视频流
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java字符串如何解析成能运行的java代码?
java字符串如何解析成运行的java代码
有些情况下,不得不动态运行Java代码,以便提供更加灵活的方式,以下代码可参考(在JDK 1.5+平台上运行通过):
public static void main(String[] args) {
int i = 10;
String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";
code += "for(int i=0;i" + i + ";i++){";
code += " System.out.println(Math.pow(i,2));";
code += "}";
try {
run(code);
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized static File compile(String code) throws Exception {
File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));
file.deleteOnExit();
// 获得类名
String classname = getBaseFileName(file);
// 将代码输出到文件
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(getClassCode(code, classname));
out.close();
// 编译生成的java文件
String[] cpargs = new String[] { "-d",
System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",
file.getName() };
int status = Main.compile(cpargs);
if (status != 0) {
throw new Exception("语法错误!");
}
return file;
}
private static synchronized void run(String code) throws Exception {
String classname = getBaseFileName(compile(code));
new File(System.getProperty("user.dir")
+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")
.deleteOnExit();
try {
Class cls = Class.forName(classname);
Method main = cls.getMethod("method", null);
main.invoke(cls, null);
} catch (Exception se) {
se.printStackTrace();
}
}
private static String getClassCode(String code, String className) {
StringBuffer text = new StringBuffer();
text.append("public class " + className + "{\n");
text.append(" public static void method(){\n");
text.append(" " + code + "\n");
text.append(" }\n");
text.append("}");
return text.toString();
}
private static String getBaseFileName(File file) {
String fileName = file.getName();
int index = fileName.indexOf(".");
String result = "";
if (index != -1) {
result = fileName.substring(0, index);
} else {
result = fileName;
}
return result;
}
相关问答
Q1: 急求java代码的详解析 bt_9.setOnClickListener(new OnClickListener(){ public void onClick(View v){ str
Button19.setOnClickListener(new View.OnClickListener() {//添加一个OnClickListener接口的匿名实例/对象
public void onClick(View v) {//当click事件发生时会调用这个onclick方法
// TODO Auto-generated method stub
if (str_result!=""){//1实时解析java代码:判断字符串是否与空串常量的内存地址相同实时解析java代码,凡是在源代码中出现的"xxxxx"实时解析java代码,统统属于全局静态常量。字符串比较千万别用“==”“!=”。
double b = Double.parseDouble(str_result);
//2:把str_result的字符串转为double类型的数据
str_result = ""+b*b;
//3:把str_result设为b的平方。""+数值型 ——〉转为字符串类型
entry.setText(String.valueOf(str_result));
//4:把entry的显示文字设为str_result,也即原来的输入的数字的平方。String.valueOf用得有点多余。
}
}
}
//以下的看来是对上面那段毛病代码的修正。
if(str_oper.equals("*")){//对代码1的修正,这才是判断字符串内容是否相同。
g=Double.parseDouble(str_display.toString());//跟代码2一个意思,只是转换的对象是一个字符串缓冲区里的字符串。这里不明白为什么要使用字符串缓冲区。
str_result=String.valueOf((b*g));
//等价于代码3。两者都对。
entry.setText(str_result);
//等价于代码4。
str_display=new StringBuffer("");
//为str_display开辟一个新的字符串缓冲区。字符串缓冲区的作用是避免每一次字符串拼接修改都得new一个字符串对象。这里是干吗用的,由于没看到完整程序,不清楚。
Q2: Java代码解析
这个都是java 8里面新增的东西。以前的版本java是没有 :: 的,新版本的作用和c++中的 :: 类似。
如:A,B表示两个类,在A,B中都有成员member。那么
A::member就表示类A中的成员member
B::member就表示类B中的成员member
tbPlanProcessStatusList.forEach(this::confirmPlanProcessStatus);
他代替了java 8之前版本的遍历,即增强for循环等等。
forEach代表了循环
方法引用是使用两个冒号::这个操作符号。
可以去看看以下的例子或者去了解以下Lambda表达式:
Q3: java解析lrc文件代码
lrc可以通过如下util工具类进行转换,如果想知道结果是否读取的有问题,可以直接用记事本打开lrc文件的,之后和输出结果比对一下就行。
package com.routon.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
/**
* parse lrc file tool
* eg:
* utilLrc lrc = new utilLrc("/sdcard/test.lrc");
* get song name : String title = lrc.getTitle();
* get performer name : String artist = lrc.getArtist();
* get album name: String album = lrc.getAlbum();
* get lrcmaker name: String lrcMaker = lrc.getLrcMaker();
* get song list: ListStatement list = lrc.getLrcList();
*
* @author xuweilin
*
*/
public class utilLrc {
private static String TAG = "utilLrc";
public class Statement {
private double time = 0.0; //time, 0.01s
private String lyric = ""; //song word
/*
* get time
*/
public double getTime() {
return time;
}
/*
* set time
*/
public void setTime(double time) {
this.time = time;
}
/*
* set time.format:mm:ss.ms
*/
public void setTime(String time) {
String str[] = time.split(":|\\.");
this.time = Integer.parseInt(str[0])*60+Integer.parseInt(str[1])+Integer.parseInt(str[2])*0.01;
}
/*
* get lrc word
*/
public String getLyric() {
return lyric;
}
/*
* set lrc word
*/
public void setLyric(String lyric) {
this.lyric = lyric;
}
}
private BufferedReader bufferReader = null;
private String title = "";
private String artist = "";
private String album = "";
private String lrcMaker = "";
private ListStatement statements = new ArrayListStatement();
/*
*
* fileName
*/
public utilLrc(String fileName) throws IOException{
FileInputStream file = new FileInputStream(fileName);
bufferReader = new BufferedReader(new InputStreamReader(file, "utf-8"));
readData();
}
/*
* read the file
*/
private void readData() throws IOException{
statements.clear();
String strLine;
while(null != (strLine = bufferReader.readLine()))
{
if("".equals(strLine.trim()))
{
continue;
}
if(null == title || "".equals(title.trim()))
{
Pattern pattern = Pattern.compile("\\[ti:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
title=matcher.group(1);
continue;
}
}
if(null == artist || "".equals(artist.trim()))
{
Pattern pattern = Pattern.compile("\\[ar:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
artist=matcher.group(1);
continue;
}
}
if(null == album || "".equals(album.trim()))
{
Pattern pattern = Pattern.compile("\\[al:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
album=matcher.group(1);
continue;
}
}
if(null == lrcMaker || "".equals(lrcMaker.trim()))
{
Pattern pattern = Pattern.compile("\\[by:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
lrcMaker=matcher.group(1);
continue;
}
}
int timeNum=0;
String str[] = strLine.split("\\]");
for(int i=0; istr.length; ++i)
{
String str2[] = str[i].split("\\[");
str[i] = str2[str2.length-1];
if(isTime(str[i])){
++timeNum;
}
}
for(int i=0; itimeNum;++i)
{
Statement sm = new Statement();
sm.setTime(str[i]);
if(timeNumstr.length)
{
sm.setLyric(str[str.length-1]);
}
statements.add(sm);
}
}
sortLyric();
}
/*
* judge the string is or not date format.
*/
private boolean isTime(String string)
{
String str[] = string.split(":|\\.");
if(3!=str.length)
{
return false;
}
try{
for(int i=0;istr.length;++i)
{
Integer.parseInt(str[i]);
}
}
catch(NumberFormatException e)
{
Log.e(TAG, "isTime exception:"+e.getMessage());
return false;
}
return true;
}
/*
* sort the word by time.
*/
private void sortLyric()
{
for(int i=0;istatements.size()-1;++i)
{
int index=i;
double delta=Double.MAX_VALUE;
boolean moveFlag = false;
for(int j=i+1;jstatements.size();++j)
{
double sub;
if(0=(sub=statements.get(i).getTime()-statements.get(j).getTime()))
{
continue;
}
moveFlag=true;
if(subdelta)
{
delta=sub;
index=j+1;
}
}
if(moveFlag)
{
statements.add(index, statements.get(i));
statements.remove(i);
--i;
}
}
}
/**
* get title
* @return
*/
public String getTitle(){
return title;
}
/**
* get artist
* @return
*/
public String getArtist(){
return artist;
}
/**
* get album
* @return
*/
public String getAlbum(){
return album;
}
/**
* get lrc maker
* @return
*/
public String getLrcMaker(){
return lrcMaker;
}
/**
* get song list
* @return
*/
public ListStatement getLrcList(){
return statements;
}
}
关于实时解析java代码和java解析视频流的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






