赞
踩
io就是input和output,即输入和输出,是数据的无结构化传递
当我们做java开发时,需要在内存,磁盘,网络中传输数据时,可能要一次性要传输的数据很大,而我们的内存空间有限,无法完成大文件的批量传输,这时候我们就可以使用io流,io流传输数据就是像流水一样缓缓的流动传输。
从io操作对象的角度分析
读取一个字节
public class FileInputStreamDemo {
public static void main(String[] args) {
try {
FileInputStream is = new FileInputStream("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\txt\\test.txt");
int i = 0;
i = is.read();
System.out.println((char)i);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取文件所有内容
public class FileInputStreamDemo { public static void main(String[] args) { try { FileInputStream is = new FileInputStream("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\txt\\test.txt"); int len = 0; byte bur[] = new byte[1024]; while ((len = is.read(bur)) != -1) { System.out.println(new String(bur, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.FileInputStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream is = new FileInputStream("E:/test.txt");
int len = 0;
byte bur[] = new byte[1024];
while((len = is.read(bur)) != -1) {
System.out.println(new String(bur, 0, len));
}
}
}
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
String str = "Java IO 流";
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
int len = 0;
byte bur[] = new byte[1024];
while((len = is.read(bur)) != -1) {
System.out.println(new String(bur, 0, len));
}
}
}
public class FileInputStreamDemo
public static void main(String[] args) throws IOException {
InputStream is = System.in;
int len = 0;
byte bur[] = new byte[1024];
while((len = is.read(bur)) != -1) {
System.out.println(new String(bur, 0, len));
}
}
}
public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("[启动服务器......]"); Socket s = ss.accept(); System.out.println("[客服端:]" + s.getInetAddress().getHostAddress() + " 已连接到服务器"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); // 读取客户端发送来的消息 String msg = br.readLine(); System.out.println("[客户端:]" + msg); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); bw.write(msg + "\n"); bw.flush(); } catch (IOException e) { e.printStackTrace(); } } }
客户端
public class Client { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 8888); // 构建IO InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); // 向服务器端发送一条消息 bw.write("测试客户端和服务器端通信,服务器收到消息返回到客户端\n"); bw.flush(); // 读取服务器返回的消息 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String msg = br.readLine(); System.out.println("[服务器:]" + msg); } catch (IOException e) { e.printStackTrace(); } } }
服务器执行结果
客户端执行结果
file类是java为了操作磁盘文件创建,删除,移动而创建的类。
// 通过给定的父抽象路径名和子路径名字符串创建一个新的File实例。
File(File parent, String child);
// 通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
File(String pathname);
// 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
File(String parent, String child);
// 通过将给定的 file: URI 转换成一个抽象路径名来创建一个新的 File 实例。
File(URI uri);
递归打印文件名
public class FileUtil { public static void main(String[] args) { showDir(1, new File("D:\\Java")); } static void showDir(int indent, File file) { for (int i = 0; i < indent; i++) { System.out.print('-'); } System.out.println(file.getName()); if(file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { showDir(indent + 4, files[i]); } } } }
public class InputStreamDemo {
public static void main(String[] args) throws Exception {
File file = new File("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\img\\清纯美女.webp");
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\img\\清纯美女copy.webp");
int len = 0;
byte[] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
}
}
public class MemoryDemo { static String str = "hello world!"; public static void main(String[] args) { // 从内存中读取数据 ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes()); // 写入到内存中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len = 0; while ((len = inputStream.read()) != -1) { char c = (char) len; outputStream.write(Character.toUpperCase(c)); } System.out.println(outputStream.toString()); } }
public class BufferedDemo { public static void main(String[] args) { try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\img\\清纯美女.webp")); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\code\\test\\gupao\\socket-io\\src\\main\\java\\img\\清纯美女bufferCopy.webp")); int len = 0; byte[] bytes = new byte[1024]; while((len = bufferedInputStream.read(bytes)) != -1) { System.out.println(new String(bytes, 0, len)); bufferedOutputStream.write(bytes, 0, len); bufferedOutputStream.flush(); } } catch (Exception e) { e.printStackTrace(); } } }
flush这个方法在很早之前的网络传输阶段就有了,为了使数据传输不是直接从发送端一段段的到接收端,
这样很影响效率,于是出现了缓冲区这一个内存区域,发送数据先往缓冲区填,缓冲区填满了在往接收区填,但是有时候当我们的数据不足以填充缓冲区时,就会造成内存占用以及数据传输不全的问题,为了解决这个问题,我们需要手动调用flush方法来强制将缓冲区的数据刷到接收端。而实际上调用close从根本上也是调用flush方法。
public void close() throws IOException {
try (OutputStream ostream = out) {
flush();
}
}
参与序列化与反序列化的java类
public class Student implements Serializable {
private String name;
private int age;
// 以下省略有参构造、无参构造、set、get、toString
}
参加序列化的类必须要实现Serializable接口
序列化操作
public static void main(String[] args) throws Exception {
// 创建 Java 对象
Student student = new Student("张三",22);
// 对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student"));
// 使用 writeObject 序列化对象
oos.writeObject(student);
// 刷新
oos.flush();
// 关闭流
oos.close();
}
反序列化操作
public static void main(String[] args) throws Exception {
// 对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student"));
// 使用 readObject() 反序列化
Object obj = ois.readObject();
// 使用对象
System.out.println(obj);
// 关闭流
ois.close();
}
使用transient修饰的字段,在序列化时,不会加入到字节文件中。
public class Student implements Serializable {
private static final Long serialVersionUID = 1L;
private String name;
private transient int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。