Java 的一些知识手册

本文深入探讨了Java开发中九项关键技术,包括资源加载、日期处理、Maven配置、类加载器自定义、接口实现检查及数据流转换等,为开发者提供实用的代码示例和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. getResource

	File file = new File(
		getClass().getClassLoader().getResource("database.properties").getFile()
	);

.class.getClassLoader().getResource(“test-1.0-SNAPSHOT.jar”).getFile()

2. getResourceAsStream


	InputStream inputStream = getClass()
			.getClassLoader().getResourceAsStream("database.properties");
    private File getFileFromResources(String fileName) {

        ClassLoader classLoader = getClass().getClassLoader();

        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException("file is not found!");
        } else {
            return new File(resource.getFile());
        }

    }
    void loadXMLTest() {

        ClassLoader classLoader = getClass().getClassLoader();

        try (InputStream inputStream = classLoader.getResourceAsStream("xml/data.xml")) {

            String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            System.out.println(result);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3. Java get Date

Date d=new Date();   
   SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");   
   System.out.println("今天的日期:"+df.format(d));   
   System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - (long)2 * 24 * 60 * 60 * 1000)));  
   System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + (long)3 * 24 * 60 * 60 * 1000)));

4. Maven 解决Error:java: release version 5 not supported问题

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

5. Maven添加系统包

        <dependency>
            <groupId>com.importsys</groupId>
            <artifactId>hvmtest</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>/Users/src/main/resources/test-1.0-SNAPSHOT.jar</systemPath>
        </dependency>

6. java get public method of a class

import javax.sound.midi.Soundbank;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class GetMethodOfClass {
    public void foo() {
        System.out.println("I am foo");
    }
    public int bar() {
        System.out.println("I am bar");
        return 1;

    }
    private void xxx() {
        System.out.println("I am xxx");
    }
    public static void main(String[] args) {
        try {
            Class c = GetMethodOfClass.class;
            Method[] allMethods = c.getDeclaredMethods();
            for  (Method method : allMethods) {
                if (Modifier.isPublic(method.getModifiers())) {
                    System.out.println(method);

                }
            }
        } catch (Throwable e) {
            System.err.println(e);
        }
    }
}

7. JAVA 自定义classloader(findClass)

    public Class findClass(String name) throws ClassNotFoundException{
        try {
            System.out.println("[find class] " + name);

               return this.loadClass(name);

            return this.getParent().loadClass(name);
        } catch (ClassNotFoundException e) {
            try {
                return this.getClass().getClassLoader().loadClass(name);
            } catch (ClassNotFoundException e1) {
                byte[] clazz = this.classMap.get(name);
                if (clazz == null || clazz.length == 0) {
                    throw new RuntimeException("no such class: " + name);
                }

                return defineClass(name, clazz, 0, clazz.length);
            }
        }
    }

8. JAVA 判断一个class是否实现了某个接口

    private static boolean checkClazzIsSpecific(Class CClazz, Class specific) {
        if (CClazz == specific) return true;
        Class[] interfaces = CClazz.getInterfaces();
        boolean result = false;
        for (Class inter : interfaces) {
            if (result) break;
            if (inter == specific) {
                result = true;
                break;
            }
            result = checkClazzIsSpecific(inter, specific);
        }
        return result;
    }

9. JAVA convert byte[] to stream

```java
public void givenUsingPlainJava_whenConvertingByteArrayToInputStream_thenCorrect() 
  throws IOException {
    byte[] initialArray = { 0, 1, 2 };
    InputStream targetStream = new ByteArrayInputStream(initialArray);
}
    byte[] initialArray = { 0, 1, 2 };
    InputStream is = ByteSource.wrap(initialArray).openStream();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值