一、OutputStream write
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class StreamTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//File file=new File("/Users/hanzhao/Desktop/zhaohanhan.txt");
//System.out.println(file.exists());
//題外話
String s="aa我";
System.out.println(s.length());//返回3個字符char
byte[]bytes1=s.getBytes();
System.out.println(bytes1.length);//返回5個字節,一個字母一個字節,一個漢字兩個字節
//FileOutputStream fos = null;//局部變量要賦值
OutputStream fos = null;//用FileOutputStream 的父類來調用,OutputStream是abstract抽象類,不能new
try {
// fos = new FileOutputStream("/Users/hanzhao/Desktop/zhaohanhan.txt");
fos=new FileOutputStream("/Users/hanzhao/Desktop/daleilei.txt");
byte[] bytes="aaa找找找adfsdfsdfads的房間都是法律框架上的六塊腹肌都是".getBytes();//String類裏面的方法,返回byte【】
fos.write(bytes);
//fos.close();//如果前面的代碼出現問題,那麼fos.close();就不會被執行,所以,把這個方法寫在finally,保障一定被執行
//不執行的話,會佔用資源,執行一次佔用一次
}catch(FileNotFoundException e) {
System.out.println("找不到文件");
}catch(IOException ex) {
System.out.println("找不到文件流");
}finally {
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
//裏面可以不寫,耍流氓啊啊啊啊啊啊
}
}
} //結果就是daleilei.txt被寫入內容
}
}
二、InputStream read
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class StreamTest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream fis = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/xiaoshuo.txt");
//xiaoshuo.txt裏面已經放好了內容,把這些內容read到console控制檯裏面
byte[] bytes = new byte[50];
/*int b=fis.read(bytes);//返回值int,代表每次讀的字節數
System.out.println(b);//每次讀50個字節
System.out.println(bytes);//[B@79698539
System.out.println(new String(bytes));
//要把read的bytes字節轉換爲String數據,但是這樣只能read到50個字節的內容,所有我們要用while循環以下
*/
while(fis.read(bytes)>0) {//如果寫成fis.read()>0,顯示爲一片空白
String s=new String(bytes);
System.out.print(s);
System.out.println(s);
}//read成功,但是有亂碼,這是因爲讀取字節流,按每次50個字節讀,有可能會read到半個漢字的原因。
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件地址不對");
// e.printStackTrace();
} catch (IOException e) {
System.out.println("read❌");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
}
}
}
三、字節流,read and write
// 沒有調用IOUtils中的copy方法,老老實實自己寫
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamTest3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long startMS = System.currentTimeMillis();
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
fos = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
byte[] bytes = new byte[1204];
int len;
while ((len = fis.read(bytes)) > 0) {
fos.write(bytes, 0, len);
}
long endMS = System.currentTimeMillis();
System.out.println("copytime is" + (endMS - startMS));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
}
四、調用了IOUtils中的方法了
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamTest5 {
public static void main(String[] args) {
//調用了IOUtils中的方法了
long startMS = System.currentTimeMillis();
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
fos = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
/*byte[] bytes = new byte[1204];
int len;
while ((len = fis.read(bytes)) > 0) {
fos.write(bytes, 0, len);
}*/
IOUtils.copy(fis, fos);
long endMS = System.currentTimeMillis();
System.out.println("copytime is" + (endMS - startMS));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
//自己寫的copy方法,好搞笑寫的,太×××爛啦,怎麼想的,int bufferSize明明指的是1024等等數值,我怎麼把它想成返回值了
//還有,公式不是萬能的啊,明明只能代替下面這些,我怎麼全寫上了,服了,服了。。。。。。
// byte[] bytes = new byte[1204];
// int buffSize1;
// while ((buffSize1 = inStream1.read(bytes)) > 0) {
// outStream1.write(bytes, 0, buffSize1);
// }
//copy方法放在IUOtils類裏面了,方便一些存放和調用
public static void copy(InputStream inStream, OutputStream outStream, int bufferSize) {
InputStream inStream1 = null;
OutputStream outStream1 = null;
try {
inStream1 = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
outStream1 = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
byte[] bytes = new byte[1204];
int buffSize1;
while ((buffSize1 = inStream1.read(bytes)) > 0) {
outStream1.write(bytes, 0, buffSize1);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(inStream1);
IOUtils.closeQuietly(outStream1);
}
}
}
五、IOUtils類,Utils意思爲工具類,裏面存放的全部是static靜態方法
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class IOUtils {
// IOUtils utils代表工具類,裏面是static類
// InputStream OutputStream Reader Writer都implements Closeable
// 所以都可以調用close();方法,體現了不同對象的多態
public static void closeQuietly(InputStream inStream) {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(OutputStream outStream) {
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Reader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Writer writer) {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy(InputStream inStream, OutputStream outStream, int bufferSize) throws IOException {
// 程序要嚴謹,要判斷inStream,outStream如果==null,bufferSize如果小於等於0,都要拋出異常
if (inStream == null) {
throw new IllegalArgumentException("inStream不能爲空");
// IllegalArgumentException extends RuntimeException 把異常拋出去 由調用着處理
}
if (outStream == null) {
throw new IllegalArgumentException("outStream不能爲空");
}
if (bufferSize <= 0) {
throw new IllegalArgumentException("bufferSize不能爲負數");
}
byte[] buffer = new byte[bufferSize];
int len;
while ((len = inStream.read(buffer)) > 0) {// 把異常拋出去 由調用着處理
outStream.write(buffer, 0, len);
}
}
public static void copy(InputStream inStream, OutputStream outStream) throws IOException{
copy(inStream, outStream, 1024*1024);//把異常拋出去,由調用着處理
}
}
六、自己寫着實驗用的IOUtils
import java.io.Closeable;
import java.io.IOException;
public class IOUtils2 implements Closeable {
public IOUtils2() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
}
}
七、自己寫着實驗用的IOUtils
import java.io.Closeable;
import java.io.IOException;
public interface IOUtils3 extends Closeable{
@Override
default void close() throws IOException {
// TODO Auto-generated method stub
}
}
總結;要敢於犯錯,在錯誤中總結經驗。