Let's teach you how to use java to read and parse txt text data and manage simple data

In the actual development, it is often encountered to use programming language to read the contents of text files, which can be various
Let me write an example of reading a text file that I made myself,
What is stored in the file is my personal website www.yzcopen.com navigation column
Because I was too lazy to use the database, I made a simple database with text instead

The following image is my stored format:

Then the java code reads the text content and stores it in the cache when the program is loaded

The detailed code is as follows:

Create a cache class first:

package com.yzcopen.cache;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class initCache {

 private static Map<String,Object> cacheMap = new ConcurrentHashMap<String,Object>(); 

    //Single instance construction method   
 private initCache() {
 }
 
 /**
  * Add cache
  * @param key
  * @param value
  */
 public static void setValue(String key,Object value){
    cacheMap.put(key, value);
 }
 

    //Clear the specified cache   
 public synchronized static void clearOnly(String key) {   
       cacheMap.remove(key);   
 }   
    
    
    //Clear all caches   
 public synchronized static void clearAll() {   
      cacheMap.clear();   
 }
 
    //Modify cached data
 public synchronized static void editeCache(String key,Object value) {
     clearOnly(key);
     cacheMap.put(key, value);
 }
     
/**
 * The following are all queries
 * @param key
 * @return
 */
@SuppressWarnings("unchecked")
public static Map<String,String> getMapValue(String key){
     Object obj =  cacheMap.get(key);
     if(obj!=null){
         return (Map<String, String>) obj;
     }
     return null;
 }

public static Object getObjValue(String key){
     Object obj =  cacheMap.get(key);
     return obj;
    
 }

 
public static Integer getIntValue(String key){
     Object obj =  cacheMap.get(key);
     if(obj!=null){
         return (Integer)obj;
     }
     return 0;
 }

public static String getStringValue(String key){
     Object obj =  cacheMap.get(key);
     if(obj!=null){
         return (String)obj;
     }
     return "";
 }
    


}

Then read the code function:

public static void initCache(){
    InputStream in =  YzcPattern.class.getResourceAsStream("/resources/menu.txt");  //The text directory YzcPattern can be changed into any class
       InputStreamReader reader = null;
        BufferedReader br = null;
        try {
            reader = new InputStreamReader(  
                    in,"gbk");
              br = new BufferedReader(reader); // Create an object that converts the contents of a file into a language that can be read by a computer  
                String line = "";  
                line = br.readLine();
                List<Map<String,Object>> menup = new ArrayList<Map<String,Object>>();
                List<Map<String,Object>> menuc = new ArrayList<Map<String,Object>>();
                while (line != null) {
                     String[] lines = line.split("\\|");
                     if(lines[0].equals("p")){
                         Map<String,Object> map = new HashMap<String,Object>();
                         map.put("bh", lines[1]);
                         map.put("name", lines[2]);
                         map.put("img", lines[3]);
                         map.put("listmap", new ArrayList<Map<String,Object>>());
                         menup.add(map);
                     }else if(lines[0].equals("c")){
                         Map<String,Object> map = new HashMap<String,Object>();
                         map.put("pbh", lines[1]);
                         map.put("bh", lines[2]);
                         map.put("name", lines[3]);
                         map.put("dis", lines[4]);
                         map.put("url", lines[5]);
                         map.put("img", lines[6]);
                         menuc.add(map);
                     }
                     line = br.readLine();
                }
                for(int i=0;i<menup.size();i++){
                    Map<String,Object> ob = menup.get(i);
                    String bm = ob.get("bh")+"";
                    for(int b = 0;b<menuc.size();b++){
                        Map<String,Object> obc = menuc.get(b);
                        String pbm = obc.get("pbh")+"";
                        if(bm.equals(pbm)){
                            ((ArrayList)ob.get("listmap")).add(obc);
                        }
                    }
                }
                initCache.setValue("p", menup);
                initCache.setValue("c", menuc);
                
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
}

menu.txt The storage directory is as follows:

All right, the core code is posted,

I manage my navigation bar through such a text. Of course, it's better to use database management. It's convenient for text to store some simple data

Tags: Java Database Programming

Posted on Mon, 02 Dec 2019 03:07:34 -0800 by nagalenoj