21.01数据输入输出流的概述和使用
A:数据输入输出流的概述
数据输入流:DataInputStream
数据输出流:DataOutputStream
特点:可以写基本数据类型,可以读取基本数据类型
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
int num = in.readInt();
System.out.println(num);
boolean b = in.readBoolean();
System.out.println(b);
String s = in.readUTF();
System.out.println(s);
double v = in.readDouble();
System.out.println(v);
}
private static void writeData() throws IOException {
DataOutputStream ds = new DataOutputStream(new FileOutputStream("a.txt"));
ds.writeInt(500);
ds.writeBoolean(true);
ds.writeUTF("hahaha");
ds.writeDouble(3.14);
ds.close();
}
21.02_内存操作流的概述和使用
A:内存操作流的概述
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
public static void main(String[] args) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("aaa".getBytes());
bos.write("bbb".getBytes());
bos.write("ccc".getBytes());
byte[] bytes = bos.toByteArray();
String s = new String(bytes);
System.out.println(s);
String s2 = bos.toString();
System.out.println(s2);
}
public static void main(String[] args) throws IOException {
byte[] bytes = "西部开源科技教育有限公司".getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len = in.read(bytes1);
String s = new String(bytes1, 0, len);
System.out.println(s);
}
public static void main(String[] args) throws IOException {
FileInputStream in1 = new FileInputStream("C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3");
FileInputStream in2 = new FileInputStream("C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3");
FileOutputStream allOut = new FileOutputStream("C:\\Users\\57642\\Desktop\\The Weeknd.mp3");
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(in1);
list.add(in2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024 * 8];
int len = 0;
for (FileInputStream in : list) {
while((len = in.read(bytes)) != -1){
bos.write(bytes,0,len);
}
in.close();
}
byte[] allBytes = bos.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(allBytes);
byte[] bytes2 = new byte[1024 * 8];
int len2 = 0;