流的新认知
【1】File类操作
list():所有的子文件和子目录名称
listFiles():所有的子文件和子目录File对象
getParent():父目录的名称
getParentFile():父路径的文件对象
【2】FileInput\FileOutput操作——字节流
抛异常:【字符流同理:OutputStreamWriter || InputStreamReader】
--- FileNotFouondException ——创建FIle();或者 new FileOutputStream()时候
---IOException: FileOutputStream调用 write()方法时候。
---try…catch() 也可以catch 自己抛的异常
获取当前流中可读字节数:说明了流的本质就是数组,获取数组的长度,只不过采用不同类型数组:byte[]\char[]
byte[] bs = new byte[in.available()];//获取当前流当中,该文件可读字节数
【3】字符流
区别于:字节流的读写[write.flush()]
FileOutputStream || FileInputStream
---单字节写入write(byte) &&& 字节数组写入 write(byte[])
单字符写入write(char) &&& 字符数组写入或字符串写入 write(String) write(char[])
OutPutStreamWriter || InputStreamReader
---单字节读取read() &&& 字节数组读取 read(byte[]) // 长度: 从inputStream流中获取 avaiable()
单字符读取 read() &&& 字符数组读取 read(char[]) //长度上: 从WinNTFileSystem类获取文件长度 getLength()
BufferedOutPutStream && BufferedInputStream ——作用增加缓冲
---单字节写入write(byte) &&& 字节数组写入 write(byte[])
---单字节读取read() &&& 字节数组读取 read(byte[]) // 长度: 从inputStream流中获取 avaiable()
BufferedWriter || BufferedReader
---write(String) && write(char[],off,len)
---reade(char[]) && read()-返回int && readLine() --返回字符串
【note】关系梳理:
BufferedReader(new InputStreamReader(new FileInputStream(File)))
InputStreamReader(new FileInputStream(File)) && BufferedOutputStream(new FileOutputStream())
new FileInputStream(File)
【4】ObjectOutputStream \ ObjectInputStream 对象输入输出流 &&& 深浅复制的序列化 【字节序列化ByteArrayOutputStream + ObjectOutputStream】 &&& DataOutputStream \ DataInputStream
---ObjectOutputStream(new FileOutputStream(f))
writeObject(对象)
---DataOutputStream(new FileOutputStream(f)); 【注意:读写的顺序必须保障一致】
writeInt\Short\UTF(字符串)
Java文件类中好多都是不可见:
FileInputStream—available()
WinNTFileSystem—getLength()
DataInputStream\DataOutputStream在网络编程中的应用:
客户端:
Socket socket = new Socket("127.0.0.1", 8080);
//发送请求
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
String request = "客户端请求信息:hello server http ip";
dataOutputStream.writeUTF(request);
//接受请求
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
String response = dataInputStream.readUTF();
System.out.println("客户端获得响应信息:"+response);
服务端:
serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
String receive = dataInputStream.readUTF();
System.out.println("服务端接收到消息:"+receive);
//反馈
String response = "hello client:protocal TCP\\IP";
OutputStream outputStream = socket.getOutputStream();
//outputStream.write(response.getBytes());
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF(response);
System.out.println("服务端给客户端作出响应:"+response);
字节序列化问题
Person person = new Person();
person.setAge(18);
person.setName("macro");
ByteArrayOutputStream bOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(bOutputStream);
outputStream.writeObject(person);
ByteArrayInputStream bInputStream = new ByteArrayInputStream(bOutputStream.toByteArray());
ObjectInputStream oInputStream = new ObjectInputStream(bInputStream);
Object obj = oInputStream.readObject();
if (obj instanceof Person) {
Person person2 = (Person)obj;
System.out.println(person2);
}
数组引用地址:流读取到数组,更改引用地址即可!不需要返回值!