1.转换流
1.1 将字节流转换为字符流
InputStreamReader:将字节输出流变为字符输出流
定义
public class OutputStreamWriter extends Writer //继承自字符输出流
构造方法
public OutputStreamWriter(OutputStream out)//接收字节输出流
代码示例
package JAVASE.IOtext;
import java.io.*;
/**
* @Name:将字节流转换为字符流
* @Author:ZYJ
* @Date:2019-05-15-19:46
* @Description:
*/
public class Test11 {
public static void main(String[] args) throws IOException {
//1. 获取终端流
File file = new File("C:\\Users\\ZYJ\\Desktop\\javaIO\\ioTest\\hello.txt");
//定义字节输出流
OutputStream output = new FileOutputStream(file);
//将字节输出流转为字符输出流
Writer out = new OutputStreamWriter(output);
String msg = "hello java";
// 将字符串写入至程序中
out.write(msg);
out.close();
}
}
1.2 将字符流转为字节流
1.定义
public class InputStreamReader extends Reader//继承自字符输入流
2.构造方法
public InputStreamReader(InputStream in)//接收字节输入流
3.代码示例
package JAVASE.IOtext;
import java.io.*;
/**
* @Name:将字符流转换为字节流
* @Author:ZYJ
* @Date:2019-05-15-20:10
* @Description:
*/
public class Test12 {
public static void main(String[] args) throws IOException {
String str ="hello";
//字符串传入字符输入流中
InputStream in = new ByteArrayInputStream(str.getBytes());
//实例化字符输出流
OutputStream out = new ByteArrayOutputStream();
int len=0;
//读取字符串
while((len=in.read())!=-1){
out.write(Character.toUpperCase(len));
}
System.out.println(out);
in.close();
out.close();
}
}
所有字符流的子类都是给通过OutputStreamWiter(转换流)转换来的
所有的字符流的版本
2.文件拷贝
package JAVASE.IOtext;
import java.io.*;
/**
* @Name:
* @Author:ZYJ
* @Date:2019-05-16-17:31
* @Description:
*/
public class Test13 {
public static void main(String[] args) throws IOException {
if(args.length!=2){
System.out.println("非法操作!Java fileCopy 源文件路径 目标文件路径");
return;
}
//定义源文件
File sourceFile = new File(args[0]);
//定义目标文件
File destFile = new File(args[1]);
//输入流
InputStream in = new FileInputStream(sourceFile);
//输出流
OutputStream out = new FileOutputStream(destFile);
//调用拷贝方法
fileCopy(in,out);
}
private static void fileCopy(InputStream in, OutputStream out) throws IOException {
System.out.println("----start-----");
long start = System.currentTimeMillis();
//定义字节数组
byte[] dates = new byte[1024];
int len=0;
//字节输入流调用read()方法将输入流中数据写入字节数组中
while((len=in.read(dates))!=-1){
//字节输出流调用write() 方法将 字节数组中数据 写入文件
out.write(dates,0,len);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:"+(end-start)+"ms");
System.out.println("----end----");
}
}
3.内存流
3.1 定义
IO的操作除了发生在文件中也发生在内存中,这种流称为内存操作流。文件流的操作里一定会产生一个文件数据,如果不希望产生这个文件,这种情况下使用内存作为操作终端。
内存流分为:
3.2 字节、字符内存流
字节内存流:ByteArrayInputStream
,ByteArrayOutputStream
构造方法:public ByteArrayInputStream(byte buf[])
字符操作流:CharArrayReader
,CharArrayWriter
构造方法:public ByteArrayOutputStream()
3.3 应用
package JAVASE.IOtext;
import java.io.*;
/**
* @Name:内存流实现字符串转大写
* @Author:ZYJ
* @Date:2019-05-19-18:02
* @Description:
*/
public class ToupCase {
public static void main(String[] args) throws IOException {
String msg = "hello java";
InputStream in = new ByteArrayInputStream(msg.getBytes());
OutputStream out = new ByteArrayOutputStream();
int len=0;
while((len=in.read())!=-1){
out.write(Character.toUpperCase(len));
}
System.out.println(out);
in.close();
out.close();
}
}
4.打印流
打印流解决的就是OutputStream
的设计缺陷,属于OutputStream
功能的加强版。如果操作的不是二进制数据,只是想通过程序向终端目标输出信息的话,OutputStream
不是很方便,其缺点有两个
1.所有数据必须转为字节数组
2.如果要输出的是int、double等类型就不方便
4.1 系统提供的打印流
打印流分为:字节打印流 :PrintStream
字符打印流: PrintWriter
(使用几率较高)
4.1.1 设计模式
打印流的设计模式是装饰设计模式:核心依然是某个类的功能,但是为了得到更好的操作效果,让其支持的功能更多。
代码示例
package JAVASE.IOtext;
import java.io.*;
/**
* @Name:打印流练习
* @Author:ZYJ
* @Date:2019-05-19-18:53
* @Description:
*/
public class PrintWriterTest {
public static void main(String[] args) throws FileNotFoundException {
File file =new File("C:\\Users\\ZYJ\\Desktop\\javaIO\\ioTest\\java.txt");
OutputStream out = new FileOutputStream(file);
PrintWriter printWriter = new PrintWriter(out);
printWriter.print("Name:");
printWriter.println("Amanda");
printWriter.print("Age:");
printWriter.println(21);
printWriter.close();
}
}