内存中写数据的程序

2020-06-30 08:54

/**

* 初始缓存实例 */

private static CacheService cacheManager = CacheManager.getInstance(\

//其中 括号里边的 “busiNo”为申请的内存的名称。即唯一的标识符。类似于表名 String cacheKey = cacheManager.generateCacheKey(\

// cacheKey 如同Map里边的 key ,括号里边的存放一个标识字段,类型为Object类型 Object object = cacheManager.getCache(cacheKey); //此句为通过上文中的cacheKey取内存中的对象。

if(object != null){ return object.toString(); }//存在就返回相应的字符串 cacheManager.putCache(cacheKey, Object); return Object;

读取资源文件的办法

Java读取Properties文件的六种方法 1。使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法

示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 3。使用java.util.PropertyResourceBundle类的构造函数

示例: InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in); 4。使用class变量的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例: InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); 补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in);

JProperties.java文件 public class JProperties {

public final static int BY_PROPERTIES = 1;

public final static int BY_RESOURCEBUNDLE = 2;

public final static int BY_PROPERTYRESOURCEBUNDLE = 3; public final static int BY_CLASS = 4;

public final static int BY_CLASSLOADER = 5;

public final static int BY_SYSTEM_CLASSLOADER = 6;

public final static Properties loadProperties(final String name, final int type) throws IOException {

Properties p = new Properties(); InputStream in = null;

if (type == BY_PROPERTIES) {

in = new BufferedInputStream(new FileInputStream(name)); assert (in != null); p.load(in);

} else if (type == BY_RESOURCEBUNDLE) {

ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); assert (rb != null);

p = new ResourceBundleAdapter(rb);

} else if (type == BY_PROPERTYRESOURCEBUNDLE) { in = new BufferedInputStream(new FileInputStream(name)); assert (in != null);

ResourceBundle rb = new PropertyResourceBundle(in); p = new ResourceBundleAdapter(rb); } else if (type == BY_CLASS) {

assert (JProperties.class.equals(new JProperties().getClass())); in = JProperties.class.getResourceAsStream(name); assert (in != null); p.load(in);

// return new JProperties().getClass().getResourceAsStream(name);

}否则如果(类型== BY_CLASSLOADER){ 断言(JProperties.class.getClassLoader()。等于(新JProperties()。的getClass()。getClassLoader ()));

。在= JProperties.class.getClassLoader()getResourceAsStream(姓名); 断言(在=空!); p.load(中);

/ /返回新JProperties()的getClass()getClassLoader()getResourceAsStream(名)。。。; }否则如果(类型== BY_SYSTEM_CLASSLOADER){ 在= ClassLoader.getSystemResourceAsStream(姓名); 断言(在=空!); p.load(中); }

如果(在!=空){ in.close(); }

返回磷; }

/ / ---------------------------------------------- Servlet的使用 / / ----------------------------------------------支持类 公共静态类ResourceBundleAdapter扩展性能{

公共ResourceBundleAdapter(ResourceBundle的包){

断言(经常预算的instanceof java.util.PropertyResourceBundle); this.rb =经常预算;

java.util.Enumeration é = rb.getKeys(); 而(e.hasMoreElements()){ 对象o = e.nextElement(); this.put(海外,rb.getObject((字符串)o)条); } }

私人的ResourceBundle包= 0;

公众的ResourceBundle的getBundle(弦乐的basename){ 返回ResourceBundle.getBundle(基名); }

公众的ResourceBundle的getBundle(弦乐的basename,语系语系){ 返回ResourceBundle.getBundle(基名,语言环境); }

公众的ResourceBundle的getBundle(弦乐的basename,现场的语言环境,类加载器加载器){

返回ResourceBundle.getBundle(基名,语言环境,装载机); }

公共枚举getKeys(){ 返回rb.getKeys(); }

公共区域设置getLocale(){ 返回rb.getLocale(); }

公共对象的GetObject(String键){ 返回rb.getObject(关键); }

公共字符串的getString(String键){ 返回rb.getString(关键); }

公众的String [] getStringArray(String键){ 返回rb.getStringArray(关键); }

保护对象handleGetObject(String键){ 返回((PropertyResourceBundle)包)handleGetObject(键)。

} } }

JPropertiesTest.java文件扩展TestCase的公共类JPropertiesTest { JProperties jProperties;

String键=“helloworld.title”; 字符串值=“!世界您好”;

公共无效testLoadProperties()抛出异常{ 字符串名称= 0;

属性p值=()的新特性;

名称=的“C:\\ \\ IDEAP \\ \\ Properties4Methods \\ \\型钢\\ \\的COM \\ \\ kindani \\ \\测试\\ \\ LocalStrings.properties”;

P值JProperties.loadProperties(姓名,JProperties.BY_PROPERTIES); 的assertEquals(值,p.getProperty(键));

名称=“com.kindani.test.LocalStrings”;

P值JProperties.loadProperties(姓名,JProperties.BY_RESOURCEBUNDLE); 的assertEquals(值,p.getProperty(键)); 的assertEquals(值,((JProperties.ResourceBundleAdapter)芘)的getString(键)。); 名称=的“C:\\ \\ IDEAP \\ \\ Properties4Methods \\ \\型钢\\ \\的COM \\ \\ kindani \\ \\测试\\ \\ LocalStrings.properties”;

P值JProperties.loadProperties(姓名,JProperties.BY_PROPERTYRESOURCEBUNDLE); 的assertEquals(值,p.getProperty(键)); 的assertEquals(值,((JProperties.ResourceBundleAdapter)芘)的getString(键)。); 名称=“\\ \\的COM \\ \\ kindani \\ \\测试\\ \\ LocalStrings.properties”;

P值JProperties.loadProperties(姓名,JProperties.BY_SYSTEM_CLASSLOADER); 的assertEquals(值,p.getProperty(键));

名称=“\\ \\的COM \\ \\ kindani \\ \\测试\\ \\ LocalStrings.properties”;

P值JProperties.loadProperties(姓名,JProperties.BY_CLASSLOADER); 的assertEquals(值,p.getProperty(键)); 名称为“test \\ \\ LocalStrings.properties”;

P值JProperties.loadProperties(姓名,JProperties.BY_CLASS); 的assertEquals(值,p.getProperty(键)); } }

属性文件与JPropertiesTest.java文件相同的目录下 LocalStrings.properties文件

#$编号:LocalStrings.properties,1.1版2000/08/17○时57分52秒horwat进出口$ #默认例如servlet的本地化资源 #此语言环境是en_US helloworld.title =世界您好!

requestinfo.title =请求信息的一个例子 requestinfo.label.method =方法:

requestinfo.label.requesturi =请求URI:

requestinfo.label.protocol =协议: requestinfo.label.pathinfo =路径信息: requestinfo.label.remoteaddr =远程地址: requestheader.title =请求头示例

requestparams.title =请求参数的例子

requestparams.params在- REQ的=参数在此要求: requestparams.no -参数=没有参数,请输入一些 requestparams.firstname =名字: requestparams.lastname =姓: cookies.title =饼干为例

cookies.cookies =您的浏览器发送以下的Cookie: cookies.no -饼干=您的浏览器不发送任何cookies cookies.make -饼干=创建一个cookie发送到浏览器 cookies.name =名称: cookies.value =价值:

cookies.set =你刚送来的下列cookie到您的浏览器: sessions.title =会话范例 sessions.id =会话ID:

sessions.created =创建时间: sessions.lastaccessed =上次访问:

sessions.data =下列数据是在您的会话: sessions.adddata =添加到您的会话数据 sessions.dataname =会话属性名称: sessions.datavalue =价值会话属性: Java对properties配置文件的操作 public class PropertiesUnit { private String filename; private Properties p;

private FileInputStream in; private FileOutputStream out;

public PropertiesUnit(String filename) { this.filename = filename;

File file = new File(filename); try {

in = new FileInputStream(file); p = new Properties(); p.load(in); in.close();

} catch (FileNotFoundException e) { // TODO Auto-generated catch block

System.err.println(\配置文件config.properties找不到!\ e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block


内存中写数据的程序.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:大学运动会秩序册

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: