複製:其實就是先讀後寫
public classHelloWorld {
public static voidmain(String[] args) throws IOException{
FileReader fr = newFileReader("C:\\QcOSD.txt");
FileWriter fw = newFileWriter("D:\\demo.txt");
// char[] buf = new char[1024];
int len = 0;
while ((len=fr.read())!=-1){
fw.write(len);
}
fw.close();
fr.close();
}
}
上面是一個一個讀的,下面按數組讀:
public classHelloWorld {
public static voidmain(String[] args){
FileReader fr = null;
FileWriter fw = null;
try {
fr = newFileReader("C:\\QcOSD.txt");
//寫的文件有就覆蓋,沒有就創建
fw = newFileWriter("D:\\demo.txt");
//創建一個緩存容器,用來緩存讀取到的字符
char[] buf = new char[1024];
//定義一個變量記錄讀取到的字符數,其實就是數組裏的字符個數
int len = 0;
while((len=fr.read(buf))!=-1){
fw.write(buf, 0, len);
}
} catch(Exception e) {
throw newRuntimeException("錯了");
} finally{
if(fw!=null)
try {
fw.close();
} catch(IOException e) {
e.printStackTrace();
}
if(fr!=null)
try {
fr.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
複製原理圖:
字符流緩衝區:
public classHelloWorld {
private static final String LINE_SEPARATOR = System.lineSeparator();
public static voidmain(String[] args) throws IOException{
FileWriter fw = newFileWriter("d:\\demo.txt");
//關聯緩衝區
BufferedWriter bufw = newBufferedWriter(fw);
bufw.write("asdf"+LINE_SEPARATOR+"ghjkl");
bufw.write("xixixixi");
//緩衝區自帶的換行方法newline()
bufw.newLine();
bufw.write("hahahahh");
//緩衝區關的時候,fw也自動關掉了,緩衝區只是提高效率,底層關的是fw
bufw.close();
}
}
上面是寫,下面是讀:
public classHelloWorld {
private static final String LINE_SEPARATOR = System.lineSeparator();
public static voidmain(String[] args) throws IOException{
FileReader fr = newFileReader("c:\\QcOSD.txt");
//他有一個特殊的方法,一次讀取一行
BufferedReader bufw = newBufferedReader(fr);
String line = null;
while((line=bufw.readLine())!=null){
System.out.println(line);
}
}
}
裝飾設計模式:類似裝修,就是將一個事物的外觀改變,實質沒變
Buffer就是用裝飾設計模式設計出來的
下3圖爲裝飾事例:
----
package db_01;
//這個類是爲了增強person而出現的
public class NewPerson{
//增強他,所以肯定有Person
private Person p;
NewPerson(Person p){
this.p = p;
}
public void chifan(){
System.out.println("開胃酒");
p.chifan();
System.out.println("甜點");
}
}
----
package db_01;
public class Person {
Person(){}
void chifan(){
System.out.println("吃飯");
}
}
----
運行結果:
----
裝飾和緩衝區的區別:
這裏的text和media僅僅是例子,不真實存在
繼承:
裝飾:
Buffered的一個裝飾子類:LineNumberReader
字節流:
字節流的數組用的是byte[] 字節流用的是char[]
FileInputStream有一個特殊的方法available(),這個方法可以返回數據的長度
此方法只適應於小文件,不適應於大文件
字節流操作媒體文件練習:
方式一:
public classHelloWorld {
public static voidmain(String[] args) throws IOException{
String str = "F:\\photo\\0.png";
String str1 = "F:\\photo\\00.png";
FileInputStream fis = newFileInputStream(str);
FileOutputStream fos = newFileOutputStream(str1);
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
}
}
方式二:
public classHelloWorld {
public static voidmain(String[] args) throws IOException{
String str = "F:\\photo\\0.png";
String str1 = "F:\\photo\\00.png";
FileInputStream fis = newFileInputStream(str);
BufferedInputStream bufis = newBufferedInputStream(fis);
FileOutputStream fos = newFileOutputStream(str1);
BufferedOutputStream bufos =newBufferedOutputStream(fos);
byte[] buf = new byte[1024];
int len = 0;
while ((len=bufis.read(buf))!=-1) {
bufos.write(buf, 0, len);
//緩衝區的刷新是複寫過的,所以有用
bufos.flush();
}
bufos.close();
bufis.close();
}
}
這些文本,音樂,圖片都可以說是文件,所以都是filexxx類