在Java中,可以使用字节流或字符流将数据写入数组。
- 使用字节流写入数组:
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) throws IOException { InputStream input = ...; // 获取输入流 ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) != -1) { output.write(buffer, 0, length); } byte[] data = https://www.yisu.com/ask/output.toByteArray();>
- 使用字符流写入数组:
复制import java.io.CharArrayWriter; import java.io.IOException; import java.io.Reader; public class Main { public static void main(String[] args) throws IOException { Reader reader = ...; // 获取Reader对象 CharArrayWriter writer = new CharArrayWriter(); char[] buffer = new char[1024]; int length; while ((length = reader.read(buffer)) != -1) { writer.write(buffer, 0, length); } char[] data = https://www.yisu.com/ask/writer.toCharArray();>注意,以上示例中的
...
表示你需要根据具体的情况来获取输入流或Reader对象。另外,要记得在操作完成后关闭输入流或Reader对象以及输出流或Writer对象。