@TestpublicvoidInputAndOutStream(){//字节输入输出流,可以设置FileOutputStream中可以设置true为把内容覆盖//下面方式采用try-with-catch方式,在括号内的资源会在执行结束后自动释放try(InputStream in =newFileInputStream("E:\\mubiao\\onre.png");OutputStream out =newFileOutputStream("E:\\mubiao\\b.png",true);){byte[] b =newbyte[1024];int len;while((len = in.read(b))!=-1){
out.write(b,0,len);
out.flush();}}catch(IOException e){
e.printStackTrace();}}
2、BufferedInputStream 与 BufferedOutputStream
@TestpublicvoidBufferInputAndBufferOutStream(){//字节输入输出流,可以设置FileOutputStream中可以设置true为把内容覆盖//下面方式采用try-with-catch方式,在括号内的资源会在执行结束后自动释放//BufferedInputStream包裹InputStream提高性能try(InputStream in =newFileInputStream("E:\\mubiao\\onre.jpg");OutputStream out =newFileOutputStream("E:\\mubiao\\b.jpg");BufferedInputStream bis =newBufferedInputStream(in);BufferedOutputStream bos =newBufferedOutputStream(out);){int len;byte[] by =newbyte[1024];while((len = bis.read(by))!=-1){
bos.write(by,0,len);}}catch(IOException e){
e.printStackTrace();}}
3、ObjectInputStream 与 ObjectOutputStream
@TestpublicvoidObjectInputAndObjectOutStream(){Person person01 =newPerson("哈哈");Person person02 =newPerson("喜喜");try(OutputStream os =newFileOutputStream("E:\\学习文件\\看源码程序\\src\\io流\\Object.txt",true);ObjectOutputStream oos =newObjectOutputStream(os);InputStream is =newFileInputStream("E:\\学习文件\\看源码程序\\src\\io流\\Object.txt");ObjectInputStream ois =newObjectInputStream(is);){
oos.writeObject(person01);
oos.writeObject(person02);Object o ;while((o = ois.readObject())!=null){System.out.println(o.toString());}}catch(Exception e){
e.printStackTrace();}}//会出现java.io.StreamCorruptedException: invalid type code: AC,此处未解决,重写ObjectOutputStream的一些方法能解决
字符流
字符输入流
字符输出流
Reader(抽象类)
Writer(抽象类)
FileReader
FileWriter
BufferedReader
BufferedWriter
1、FileReader和FileWriter
@TestpublicvoidWrideAndReader(){//字符输入流//BufferedReader包裹FileReader可以提高性能try(FileReader re =newFileReader("E:\\mubiao\\a.txt");FileWriter fw =newFileWriter("E:\\mubiao\\b.txt");){char[] by =newchar[1024];int len;while((len = re.read(by))!=-1){
fw.write(by,0,len);}}catch(Exception e){
e.printStackTrace();}}
2、BufferedReader和BufferedWriter
@TestpublicvoidBufferedWriterAndBufferReader(){//BufferedReader包裹FileReader可以提高性能try(BufferedReader re =newBufferedReader(newFileReader("E:\\mubiao\\a.txt"));BufferedWriter fw =newBufferedWriter(newFileWriter("E:\\mubiao\\b.txt"));){char[] by =newchar[1024];while(re.read(by)!=-1){
fw.write(by);}}catch(Exception e){
e.printStackTrace();}}
3、转换流
转换流可以更改字符集读取
@TestpublicvoidInputStreamReaderOutputStreamWriter(){//字符转换流//可以自定义编码try(Reader re =newInputStreamReader(newFileInputStream("E:\\mubiao\\a.txt"),"utf-8");Writer fw =newOutputStreamWriter(newFileOutputStream("E:\\mubiao\\b.txt"),"utf-8")){char[] by =newchar[1024];int len;while((len = re.read(by))!=-1){
fw.write(by,0,len);}}catch(Exception e){
e.printStackTrace();}}
4、打印流
@TestpublicvoidPrintStream(){//1.打印流PrintStreamtry(PrintStream ps01 =newPrintStream(newFileOutputStream("E:\\学习文件\\看源码程序\\src\\io流\\Print.txt",true));//直接通向文件//PrintStream ps02 = new PrintStream("E:\\学习文件\\看源码程序\\src\\io流\\Print.txt");){//ps02.print("嘻嘻");
ps01.println("哈哈");}catch(FileNotFoundException e){
e.printStackTrace();}}