字节流:
FileInputStream
FileInputStream fIn = new FileInputStream("1.avi");
FileOutputStream(默认覆盖)
FileOutputStream
fOut
=
new
FileOutputStream(
"2.avi"
);
BufferedInputStream
BufferedInputStream fIn = new BufferedInputStream(new FileInputStream("1.avi"));
BufferedOutputStream
BufferedOutputStream fOut = new BufferedOutputStream(new FileOutputStream("2.avi"));
字符流:
FileReader
FileReader fr = new FileReader("
1.avi
");
FileWriter
(默认覆盖)
FileWriter fw = new FileWriter("
2.avi
");
BufferedReader
BufferedReader reader = new BufferedReader(new FileReader("
1.avi
"));
BufferedReader
reader
= new BufferedReader(new InputStreamReader(new FileInputStream("
1.avi
")));
BufferedWriter
BufferedWriter writer = new BufferedWriter(new FileWriter("
2.avi
"));
BufferedWriter
writer
= new BufferedWriter(new OutputStreamWriter(new FileOutputStream("
2.avi
")));
转换流(字节流转换成字符流)(默认为GBK编码)
InputStreamReader
InputStreamReader
reader
= new InputStreamReader(new FileInputStream("
1.avi
"));
OutputStreamWriter
OutputStreamWriter
writer
= new OutputStreamWriter(new FileOutputStream("
2.avi
"));
数据流:(可读写带类型的数据)
注意:要使用该流读取数据,必须提前知道该文件中数据的存储格式和顺序。读的顺序必须和写入的顺序相同。
DataInputStream
DataInputStream in= new DataInputStream(new FileInputStream(
"
1.avi
"
));
DataOutputStream
DataOutputStream out = new DataOutputStream(new FileOutputStream(
"
2.avi
"
));
对象流:
ObjectInputStream
ObjectOutputStream
打印流:(主要用来记录日志,通过改变系统自带的流的输出方向来实现)
PrintWriter//标准的输出流(字符流)
PrintStream //标准的输出流(默认输出到控制台)
PrintStream ps = System.out;
ps.println("JAVA。。。。");
用系统打印流记录日志:
//默认是输出到控制台的.
System.out.println("HelloWorld!");
PrintStream ps = System.out;
ps.println("JAVA。。。。");
//可以改变输出方向.
System.setOut(new PrintStream(new FileOutputStream("log"))); //log日志文件
//再次输出
//System.out.print("HAHA");//此时已经改变了输出方向,"HAHA"将会记录到日志文件中!
//通常使用上面的这种方式记录日志.
//需求:记录日志,m1方法开始执行的时间和结束的时间.记录到log文件中.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println("m1方法开始执行 "+sdf.format(new Date()));
m1();
System.out.println("m1方法执行结束 "+sdf.format(new Date()));