首页主机资讯java getresource怎样处理资源缓存

java getresource怎样处理资源缓存

时间2025-09-27 13:26:03发布访客分类主机资讯浏览1372
导读:在Java中,getResource方法用于从类路径(classpath)中加载资源文件。当你多次调用getResource方法加载相同的资源时,它通常会返回相同的InputStream实例,这意味着资源文件会被缓存。 如果你想要处理资源缓...

在Java中,getResource方法用于从类路径(classpath)中加载资源文件。当你多次调用getResource方法加载相同的资源时,它通常会返回相同的InputStream实例,这意味着资源文件会被缓存。

如果你想要处理资源缓存,可以考虑以下几种方法:

  1. 使用java.util.concurrent.ConcurrentHashMap来缓存资源文件的内容。这样,你可以在第一次加载资源时将其内容存储在ConcurrentHashMap中,然后在后续的调用中直接从缓存中获取内容,而不是每次都重新加载资源。
import java.io.BufferedReader;
    
import java.io.IOException;
    
import java.io.InputStream;
    
import java.io.InputStreamReader;
    
import java.util.concurrent.ConcurrentHashMap;


public class ResourceCache {
    
    private static final ConcurrentHashMap<
    String, String>
     resourceCache = new ConcurrentHashMap<
    >
    ();


    public static String getResourceContent(String resourceName) throws IOException {
    
        return resourceCache.computeIfAbsent(resourceName, ResourceCache::loadResource);

    }


    private static String loadResource(String resourceName) throws IOException {
    
        InputStream inputStream = ResourceCache.class.getClassLoader().getResourceAsStream(resourceName);

        if (inputStream == null) {
    
            throw new IOException("Resource not found: " + resourceName);

        }


        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
    
            StringBuilder content = new StringBuilder();
    
            String line;

            while ((line = reader.readLine()) != null) {
    
                content.append(line).append('\n');

            }
    
            return content.toString();

        }
 finally {
    
            inputStream.close();

        }

    }

}
    
  1. 使用java.lang.ref.SoftReferencejava.lang.ref.WeakReference来缓存资源文件的内容。这样,当系统内存不足时,垃圾回收器可以自动回收这些软引用或弱引用的对象,从而避免内存泄漏。
import java.io.BufferedReader;
    
import java.io.IOException;
    
import java.io.InputStream;
    
import java.io.InputStreamReader;
    
import java.lang.ref.SoftReference;
    
import java.util.Map;
    
import java.util.concurrent.ConcurrentHashMap;


public class ResourceCache {
    
    private static final Map<
    String, SoftReference<
    String>
    >
     resourceCache = new ConcurrentHashMap<
    >
    ();


    public static String getResourceContent(String resourceName) throws IOException {
    
        SoftReference<
    String>
     softReference = resourceCache.get(resourceName);

        if (softReference != null) {
    
            return softReference.get();

        }
    

        InputStream inputStream = ResourceCache.class.getClassLoader().getResourceAsStream(resourceName);

        if (inputStream == null) {
    
            throw new IOException("Resource not found: " + resourceName);

        }


        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
    
            StringBuilder content = new StringBuilder();
    
            String line;

            while ((line = reader.readLine()) != null) {
    
                content.append(line).append('\n');

            }
    
            String contentString = content.toString();
    
            resourceCache.put(resourceName, new SoftReference<
    >
    (contentString));
    
            return contentString;

        }
 finally {
    
            inputStream.close();

        }

    }

}
    

请注意,这些方法可能会增加内存使用量,因为它们会缓存资源文件的内容。在使用这些方法时,请确保你了解它们的性能影响和内存使用情况。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: java getresource怎样处理资源缓存
本文地址: https://pptw.com/jishu/710012.html
java treenode怎样处理节点移动 java getresource如何处理资源加载顺序

游客 回复需填写必要信息