其实很简单 写入数据 读取就行
创建工具类Context
package com.test;
import java.io.IOException;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
public class Context {
public static Map getSessionMap() {
return FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap();
}
public static Map getRequestParameterMap() {
return FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap();
}
public static FacesContext getContext() {
return FacesContext.getCurrentInstance();
}
public static ExternalContext getExterContext() {
return FacesContext.getCurrentInstance().getExternalContext();
}
public static void redirect(String path) {
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(
path);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
文件源操作
package com.test;
import java.io.*;
import javax.servlet.ServletContext;
import com.test.Context;
public class ReadWriteFile {
public static BufferedReader bufread;
// 指定文件路径和名称
private static ServletContext servletContext = (ServletContext) Context.getExterContext().getContext();
private static String path = servletContext.getRealPath("/")+"\\chartdata\\kenter.txt";
private static File filename = new File(path);
private static String readStr = "";
private static String webPath = "chartdata/kenter.txt";
/**
* creat
* @throws IOException
*/
public static void creatTxtFile() throws IOException {
System.out.println(path);
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!");
}
}
/**
* read
* @return
*/
public static String readTxtFile() {
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read + "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return readStr;
}
/**
* 以UTF-8的格式写入文件
*
* @param fileBody
* @return
*/
public static boolean writeUTFFile(String fileBody) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(filename);
osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(fileBody);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* replace
* @param oldStr
* @param replaceStr
*/
public static void replaceTxtByStr(String oldStr, String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setWebPath(String webPath) {
this.webPath = webPath;
}
public static String getWebPath() {
return webPath;
}
}
seam控制的bean
package com.bean;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.*;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import com.test.ReadWriteFile;
@Name("userBean")
@Scope(ScopeType.CONVERSATION)
public class UserBean implements Serializable {
private static final long serialVersionUID = -1836688779616652076L;
private String name;
private String job;
private String chartdata;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@SuppressWarnings("deprecation")
@Create
public void init() {
System.out.println("******");
String chartdata;
chartdata = "&title=测试一下呢个图表,{font-size:35px; color: #800000}&"
+ "\r\n";
chartdata += "&y_legend=CM,12,#736AFF&" + "\r\n";
chartdata += "&y_ticks=5,10,10&" + "\r\n";
chartdata += "&bar=50,#9933CC,铺柱形图,12&" + "\r\n";
chartdata += "&bar_2=50,#339966,绿色柱形图,12&" + "\r\n";
chartdata += "&line_dot_3=3,#CC3399,线条图,12,5&" + "\r\n";
chartdata += "&line_hollow_4=2,0x80a033,乱来线条图,12,6&" + "\r\n";
chartdata += "&values=3,20,5,5,4,5,2,2,5&" + "\r\n";
chartdata += "&values_2=4,3,2,3,2,3,3,2,3&" + "\r\n";
chartdata += "&values_3=8,20,8,8,8,8,8,8,8&" + "\r\n";
chartdata += "&values_4=6,16,6,6,6,6,6,6,6&" + "\r\n";
chartdata += "&x_labels=一月,二月,三月,四月,五月,六月,七月,八月,九月&" + "\r\n";
chartdata += "&x_legend=本年度月份,12,#736AFF&" + "\r\n";
chartdata += "&y_min=0&" + "\r\n";
chartdata += "&y_max=25&" + "\r\n";
try {
ReadWriteFile.creatTxtFile();
ReadWriteFile.writeUTFFile(chartdata);
} catch (IOException e) {
e.printStackTrace();
}
this.chartdata = ReadWriteFile.getWebPath();
System.out.println(System.getProperty("user.dir"));
}
public String nameItJohn() {
this.setName("kenter");
return null;
}
public String nameItMark() {
this.setName("kkkk");
return null;
}
public void setJob(String job) {
this.job = job;
}
public String getJob() {
return job;
}
public void setChartdata(String chartdata) {
this.chartdata = chartdata;
}
public String getChartdata() {
return chartdata;
}
}
显示页面xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<head>
<title>测试图表哇</title>
<meta http-equiv="keywords" content="enter,your,keywords,here" />
<meta http-equiv="description"
content="A short description of this page." />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
<ui:insert name="body">
<script type="text/javascript" src="js/swfobject.js"></script>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
width="500" height="250">
<param name="movie"
value="open-flash-chart.swf?data=#{userBean.chartdata}" />
<param name="quality" value="high" />
<embed src="open-flash-chart.swf?data=#{userBean.chartdata}"
quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="500" height="250"></embed>
</object>
</ui:insert>
</p>
</body>
</html>