1、建立InitListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package app.util; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.jboss.logging.Logger; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import com.test.ResourceService; /** * 加載數(shù)據(jù)到內(nèi)存案例 * @author 淺陌 * */ public class InitListener extends HttpServlet implements ServletContextListener { /** * */ private static final long serialVersionUID = 1L; public static Map<String, Object> contextMap= new HashMap<String,Object>(); private Logger logger = Logger.getLogger(InitListenerMobileResourceTree. class ); public void init() throws ServletException{ // logger.info("====初始化方法運(yùn)行初完畢===="); } @Override public void contextDestroyed(ServletContextEvent arg0) { logger.info( "this is last destroyeed " ); } @Override public void contextInitialized(ServletContextEvent sce) { //獲取要加載的數(shù)據(jù)方法 try { /* *如果在獲取數(shù)據(jù)時用到其他項(xiàng)目包中的接口,可以用如下方法 * WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); * ResourceService resourceService = (ResourceService) wac.getBean("resourceService");// 跑批接口的實(shí)現(xiàn)類 * 在springMVC.XML 中加入 * <bean id="resourceService" class="com.test.ResourceService" /> */ String JsonStr = 獲取加載出來的數(shù)據(jù)(類型視情況而定) //將數(shù)據(jù)放到定義好的contextMap中 contextMap.put( "JsonStr" , JsonStr); } catch (Exception e) { e.printStackTrace(); } logger.info(contextMap); } } |
2.配置web.xml
1
2
3
|
< listener > < listener-class >app.util.InitListener</ listener-class > </ listener > |
3.獲取內(nèi)存中的數(shù)據(jù)
InitListener.contextMap.get("JsonStr");
補(bǔ)充知識:java 字節(jié)流——將硬盤中的文件讀到內(nèi)存中,將內(nèi)存中的數(shù)據(jù)寫入硬盤中
我就廢話不多說了,大家還是直接看代碼吧~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.oracle.core; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class ByteStream_Study { public static void main(String[] args) throws IOException { // 輸入流 //從硬盤到內(nèi)存,文件必須存在 InputStream in= new FileInputStream( "D:\\hello.txt" ); //1.分配一塊內(nèi)存空間 臨時的空間 存放我文件的數(shù)據(jù) byte [] b= new byte [in.available()]; //2.將數(shù)據(jù)讀入到內(nèi)存空間 in.read(b); //3.將數(shù)據(jù)轉(zhuǎn)換為字符串 //如果編碼是UTF-8 可以省略 String s= new String(b, "GBK" ); System.out.println(s); in.close(); // 輸出流 //從內(nèi)存到硬盤 //文件不存在 輸出流會自動創(chuàng)建這樣一個文件 OutputStream out= new FileOutputStream( "D:\\haha.txt" ); String s1= "再見" ; //輸入還是輸出流 操作的都是內(nèi)存空間 字節(jié)數(shù)組 out.write(s1.getBytes()); out.close(); } } |
以上這篇java 將數(shù)據(jù)加載到內(nèi)存中的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/nicolewjt/article/details/88293237