分类 Java 下的文章

public class NioFileDemo {
    public static void main(String[] args) {
        NioFileDemo demo = new NioFileDemo();
        demo.writeFile();
        demo.readFile();
    }

    // 使用 NIO 写入文件
    public void writeFile() {
        Path path = Paths.get("logs/itwanger/paicoding.txt");
        try {
            FileChannel fileChannel = FileChannel.open(path, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));

            ByteBuffer buffer = StandardCharsets.UTF_8.encode("学编程就上技术派");
            fileChannel.write(buffer);

            System.out.println("写入完成");
            fileChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 使用 NIO 读取文件
    public void readFile() {
        Path path = Paths.get("logs/itwanger/paicoding.txt");
        try {
            FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ);
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            int bytesRead = fileChannel.read(buffer);
            while (bytesRead != -1) {
                buffer.flip();
                System.out.println("读取的内容: " + StandardCharsets.UTF_8.decode(buffer));
                buffer.clear();
                bytesRead = fileChannel.read(buffer);
            }

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

Java RMI(Remote Method Invocation)是一种强大的技术,它允许对象调用运行在另一个 Java 虚拟机(JVM)中的对象的方法。RMI 为创建分布式应用程序提供了一个简单直接的模型,使开发人员能够构建和部署可轻松跨网络(包括互联网)运行的系统。它是 Java SE(标准版)平台的一部分。

使用 SecureRandom

import java.security.SecureRandom;

public class Main {
    public static void main(String[] args) {
        SecureRandom secureRandom = new SecureRandom();
        int randomInteger = secureRandom.nextInt(10);
        System.out.println("Secure random Integer: " + randomInteger);
    }
}

SpotBugs is an open-source static code analysis tool used in software development for bug detection. It scans bytecode (i.e., .class files) in your Java applications for bug patterns and ranks potential issues in terms of severity. SpotBugs can identify a variety of common coding mistakes, including null pointer dereferences, infinite recursive loops, bad uses of the Java libraries, and more.

@Test
void booleanArrayToIntArray() {
    boolean[] inputs = {true, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
    int[] intArray = new int[inputs.length];
    IntStream.range(0, inputs.length).forEach(i -> intArray[i] = Boolean.compare(inputs[i], false));
    System.out.println(Arrays.toString(intArray));
}

类的静态方法

在类中static 修饰的方法是静态方法也叫类方法,可以直接调用,不需要创建实例

Netty 中内存分配有一个最顶层的抽象就是ByteBufAllocator,负责分配所有ByteBuf 类型的内存。功能其实不是很多,主要有以下几个重要的API:

什么叫匿名类

Java 的匿名类(也叫匿名内部类,因为肯定在某个类的内部)是没有名称的类。因为没有名称就无法在别的地方调用,只能在定义的时候调用了。