java 將數(shù)據(jù)加載到內(nèi)存中的操作
將數(shù)據(jù)加載到內(nèi)存中
1、建立InitListener.java
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ù)時(shí)用到其他項(xiàng)目包中的接口,可以用如下方法 * WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); * ResourceService resourceService = (ResourceService) wac.getBean('resourceService');// 跑批接口的實(shí)現(xiàn)類 * 在springMVC.XML 中加入 * <bean /> */ String JsonStr = 獲取加載出來的數(shù)據(jù)(類型視情況而定) //將數(shù)據(jù)放到定義好的contextMap中 contextMap.put('JsonStr', JsonStr); } catch (Exception e) { e.printStackTrace(); } logger.info(contextMap); }}
2.配置web.xml
<listener> <listener-class>app.util.InitListener</listener-class></listener>
3.獲取內(nèi)存中的數(shù)據(jù)
InitListener.contextMap.get('JsonStr');
補(bǔ)充知識(shí):java 字節(jié)流——將硬盤中的文件讀到內(nèi)存中,將內(nèi)存中的數(shù)據(jù)寫入硬盤中
我就廢話不多說了,大家還是直接看代碼吧~
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í)的空間 存放我文件的數(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)存到硬盤 //文件不存在 輸出流會(huì)自動(dòng)創(chuàng)建這樣一個(gè)文件 OutputStream out=new FileOutputStream('D:haha.txt'); String s1='再見'; //輸入還是輸出流 操作的都是內(nèi)存空間 字節(jié)數(shù)組 out.write(s1.getBytes()); out.close(); }}
以上這篇java 將數(shù)據(jù)加載到內(nèi)存中的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )2. asp文件用什么軟件編輯3. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對(duì)象4. ASP新手必備的基礎(chǔ)知識(shí)5. Docker 啟動(dòng)Redis 并設(shè)置密碼的操作6. vue+element開發(fā)一個(gè)谷歌插件的全過程7. Vue axios獲取token臨時(shí)令牌封裝案例8. JS中6個(gè)對(duì)象數(shù)組去重的方法9. 利用CSS制作3D動(dòng)畫10. Spring如何替換掉默認(rèn)common-logging.jar

網(wǎng)公網(wǎng)安備