如何用Java封装一个自定义的方法?

想试着用Java封装一个readChars()方法
功能和readUTF()功能类似
输出数据流中用writeChars()方法写入的字符串

基本问题。写一个案例:

public static String readChars(DataInputStream dis) throws IOException {
    int len = dis.readInt();
    char[] chars = new char[len];
    for (int i = 0; i < len; i++) {
        chars[i] = dis.readChar();
    }
    return new String(chars);
}
String str = "test!";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(str.length());
dos.writeChars(str);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream dis = new DataInputStream(bais);
String result = readChars(dis);
System.out.println(result);  




   public static String readChars(DataInputStream dataInputStream) throws IOException {
        int length = dataInputStream.readShort();  
        char[] chars = new char[length];

        for (int i = 0; i < length; i++) {
            chars[i] = dataInputStream.readChar();  
        }

        return new String(chars);
    }

答案来自gpt
你可以使用Java的IO流来实现一个自定义的readChars()方法。下面是一个简单的示例代码:

public static char[] readChars(DataInputStream input, int length) throws IOException {
    char[] chars = new char[length];
    for (int i = 0; i < length; i++) {
        chars[i] = input.readChar();
    }
    return chars;
}

这个方法接受一个DataInputStream对象和一个整数长度作为参数,从输入流中读取指定长度的字符,并将它们存储在一个char数组中返回。

你可以使用这个自定义的方法来读取使用DataOutputStream的writeChars()方法写入的字符串。例如:

DataInputStream input = new DataInputStream(new FileInputStream("data.bin"));
char[] chars = readChars(input, 5);
String str = new String(chars);
System.out.println(str);

这个示例代码将从名为"data.bin"的文件中读取5个字符,并将它们转换为一个字符串并打印出来。

这里提供一个示例代码来封装一个自定义的方法:

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class CustomReader {

    /**
     * 读取数据流中用 writeChars() 方法写入的字符串
     *
     * @param input 数据流
     * @return 字符串
     * @ IOException
     */
    public static StringChars(DataInputStream input) throws IOException {
 int length = input.readInt();
        byte[] bytes = new byte[length * 2        input.readFully(bytes);
        ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
        DataInputStream dataInput = new DataInputStream(byteInput);
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = dataInput.readChar();
        }
        return new String(chars);
    }
}

上面的示例代码中,我们封装了一个 readChars() 的方法,该方法接收一个 DataInputStream 对象作为参数,用于读取已经用 writeChars() 方法写入的字符串。方法的实现如下:

  1. 先读取一个整数来表示字符串的长度。
  2. 在数据流中读取一定长度的字节数据,由于 writeChars() 方法写入的字符串是由 char 类型组成的,因此每个字符占用 2 个字节,因此这里需要乘以 2。
  3. 将读取到的字节数据转换为 ByteArrayInputStream 对象,再将其包装为 DataInputStream 对象。
  4. 遍历 DataInputStream 中的字符数据并拼接成字符串,最后返回字符串。

使用这个自定义方法时,只需要传入对应的 DataInputStream 对象即可:

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        String str = "Hello World";
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        DataOutputStream dataOutput = new DataOutputStream(byteOutput);
        dataOutput.writeInt(str.length());
        dataOutput.writeChars(str);
        byte[] bytes = byteOutput.toByteArray();
        System.out.println(CustomReader.readChars(new DataInputStream(new ByteArrayInputStream(bytes))));
    }
}

上面的代码中,我们使用 ByteArrayOutputStream 来模拟写入数据流,然后将其输出给 CustomReader.readChars() 方法来读取对应的字符串。注意,在写入数据到数据流时,必须先写入一个整数来表示字符串的长度。如果写入和读取的字符串长度不匹配,会出现读取错误的情况,因此请注意数据的一致性。

  • 这篇博客: Java中的&0xFF操作中的 writeChar源码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 所以每一个char就是一个0-65535之间的数字
    • DataOutputStream.writeChar源码:
    public final void writeChar(String s) throws IOException {
    	int len = s.length();
    
    	for (int i = 0; i < len; i++) {
    		int v = s.charAt(i);
    		out.write((v >>> 8) & 0xFF);
    		out.write((v >>> 0) & 0xFF);
    	}
    	incCount(len * 2);
    }
    
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MyDataInputStream extends FilterInputStream {
    public MyDataInputStream(InputStream in) {
        super(in);
    }

    public char[] readChars(int length) throws IOException {
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = readChar();
        }
        return chars;
    }
}