io流使用
个人理解
1.输入 – 缓冲区(1.代码中的缓冲(非缓冲流);2.框架缓冲区的缓冲(缓冲流,装饰者模式)) - 输出
2.用字符流 (Writer) 的时候要将通道中的流刷新(flush)到硬盘上,其中close()方法会调用flush
注意:使用后关闭io,节省系统资源(一般在finally中关闭,看个人习惯)
代码
1.字节流
/**
1.字节流,基于字节,任何格式文件都可使用
*/
private static void tesByteCharacter() throws Exception{
InputStream in = new FileInputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/1.zip");
OutputStream out = new FileOutputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/2.zip");
byte[] buffer = new byte[10];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}
2.字节缓冲流
/**
2.字节缓冲流
*/
private static void testBufferByte() throws Exception{
BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/1.zip"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/3.zip"));
//可以拼接后 在write
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer))!=-1){
out.write(buffer,0,len);
}
out.close();
in.close();
}
3.字符流
/**
3.字符流
*/
private static void testCharacter() throws Exception{
Reader reader = new FileReader("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/1.txt");
Writer writer = new FileWriter("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/3.txt");
char[] buffer = new char[1024];
int len;
while ((len = reader.read(buffer))!=-1){
writer.write(buffer,0,len);
}
writer.close();
reader.close();
}
4.字符缓冲流
/**
4.字符缓冲流
*/
private static void testBufferCharacter() throws Exception{
Reader reader = new FileReader("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/1.txt");
Writer writer = new FileWriter("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/3.txt");
BufferedReader in = new BufferedReader(reader);
BufferedWriter out = new BufferedWriter(writer);
//可以拼接后 在write
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine())!=null){
builder.append(line).append("\n");
// out.write(line);
// out.newLine();
}
out.write(builder.toString());
out.close();
in.close();
writer.close();
reader.close();
}
5.二进制缓冲流
/**
5.二进制缓冲流
*/
private static void testBufferData() throws Exception{
BufferedInputStream in = new BufferedInputStream(new DataInputStream(new FileInputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/1.zip")));
BufferedOutputStream out = new BufferedOutputStream(new DataOutputStream(new FileOutputStream("E:\\SVN new\\code\\002 特殊项目开发\\20210910 产品管理运维平台\\004 源码\\Frontend\\jhws_operatation\\target/3.zip")));
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer))!=-1){
out.write(buffer,0,len);
}
//简写,每个io流应该都要关闭参考4,此处直接new DataInputStream了
out.close();
in.close();
}