字节输出流OutputStream:
OutputStream此抽象类,是表示输出字节流的所有类的超类!操作的数据都是字节,定义了输出字节流的基本共性功能方法!
//输出流中定义都是写write方法,如下图:
FileOutputStream文件字节输出流:
OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件!
FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流!
//Ctrl+T查看类之间的层级继承关系!
//可以传入一个File对象,也可以传入一个表示文件路径的字符串!
给文件中续写和换行:
//布尔类型的append值,默认是false,即执行一下代码,覆盖源文件,如果设置为true,则是续写!
IO流的异常处理:
package com.oracle.output; import java.io.FileOutputStream; import java.io.IOException; public class IODemo { public static void main(String[] args){ //只要是释放资源的代码,无论前面发不发生异常都需要执行,所以要全部放到finally里面去! //创建FileOutputStream文件字节输出流对象,并赋值为NULL: FileOutputStream fos=null; try { //将fos赋值为F盘Demo.txt,设置为续写: fos=new FileOutputStream("F:\IOTest\Demo.txt",true); fos.write(100); }catch(IOException ex) { //运行时异常,一旦发生异常立马终止程序! throw new RuntimeException("文件写入失败!"); }finally { try { //下面只有一句代码可以这么写,避免发生空指针异常! if(fos!=null) fos.close(); } catch (IOException e) { throw new RuntimeException("文件写入失败!"); } } } }
字节输入流InputStream:
InputStream此抽象类,是表示字节输入流的所有类的超类;定义了字节输入流的基本共性功能方法!
//注意:只要调用read()方法,就是读取下一个字节,类似ResultSet中的next()方法!
int read():读取一个字节并返回,没有字节返回-1!
int read(byte[]):读取一定量的字节数,并存储到字节数组中,返回读取到的字节数!
FileInputStream文件字节输入流:
InputStream有很多子类,其中子类FileInputStream可用来读取文件内容;
FileInputStream 从文件系统中的某个文件中获得输入字节!
//构造方法:
FileInputStream类读取数据的Read方法:
//注意:调用read()方法就是读取下一个字节!
示例代码:
package com.oracle.output; import java.io.FileInputStream; import java.io.IOException; public class InputStreamDemo { public static void main(String[] args) throws IOException { //创建文件字节输入流对象,并读取TestInput.txt文件: FileInputStream fis=new FileInputStream("F:\IOTest\TestInput.txt"); //根据read()方法的特点,读不到数据即返回-1,所以可以用while循环依次读取文档中内容: int len=0; //用自定义int变量len来代替read()方法,原因是read()方法调用一次即读取下一个! while((len=fis.read())!=-1) { System.out.println((char)len); } //关闭流: fis.close(); } }
OutputStream示例:
package com.oracle.output; import java.io.FileOutputStream; import java.io.IOException; public class OutputStreamDemo { public static void main(String[] args) throws IOException { //FileOutputStream的构造方法,如果文件存在,则覆盖,如果不存在则创建该文件! //true是续写,false是覆盖,默认是false! FileOutputStream fos=new FileOutputStream("F:\IOTest\Bian.txt",true); //这个是ASCII码: fos.write(100); fos.write(49); fos.write(48); fos.write(48); //用write方法传入一个字节数组,负数是汉字(一个汉字占两个字节): byte[] bytes= {-65,-66,-67,-68}; fos.write(bytes,1,2); fos.write("FUCK YOU BRO".getBytes()); //换行: fos.write("卢本伟 牛逼!".getBytes()); fos.close(); } }
字符编码表:
//ANSI不是一种编码方式,这里代指系统默认编码方式!
1;ASCII:一个字节中的7位就可以表示,对应的字节都是正数!
2;ISO-8859-1:拉丁码表Latin!
3;GB2312:简体中文码表,包含6000-7000中文和符号!用两个字节表示,两个字节第一个字节是负数,第二个字节可能是正数!
4;GBK:目前最常用的中文码表,2万的中文和符号!用两个字节表示,其中的一部分文字,第一个字节开头是1,第二字节开头是0!
5;GB18030:最新的中文码表,目前还没有正式使用!
6;Unicode:国际标准码表,无论是什么文字,都用两个字节存储(Java中的Char类型用的就是这个码表,一个Char变量占两个字节)!
//Java中的字符串是按照系统默认码表来解析的,简体中文版字符串默认的码表是GBK!
7;UTF-8:基于Unicode,一个字节就可以存储数据,不要用两个字节存储,而且这个码表更加的标准化,在每一个字节头加入了编码信息(后期到API中查找)!
//能识别中文的码表:GBK,UTF-8!正因为识别中文码表不唯一,涉及到了编码解码问题!
//对于我们开发而言,常见的编码 GBK +UTF-8+ISO-8859-1
文字-->数字:编码:“ABC”.getBytes()返回byte[]数组!
数字-->文字:解码:byte[] bytes={97,98,99},new String(bytes)返回String字符串!
FileReader读取包含中文的文件:
字符输入流Writer:
//FileWriter的构造方法:
flush()和close()的区别:
用flush()方法刷新缓冲之后还可以继续写入,close虽然会刷新缓冲,但是之后不能继续写入!
作业代码:
package com.oracle.homework; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class HW1 { public static void main(String[] args) throws IOException { //明确数据源: FileReader fr=new FileReader("F:\IOTest\TestPic.jpg"); //找到目的地: FileWriter fw=new FileWriter("F:\IOTest\TestPic\TestPicZF.jpg"); int len=0; char[] testC=new char[1024]; while((len=fr.read(testC))!=-1){ fw.write(new String(testC,0,len)); } //关流: fr.close(); fw.close(); //字符流复制图片会使图片损坏!!! } } package com.oracle.homework; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class HW2 { public static void main(String[] args) throws IOException { //明确数据源: FileInputStream fis=new FileInputStream("F:\IOTest\TestPic.jpg"); //找到目标: FileOutputStream fos=new FileOutputStream("F:\IOTest\TestPic\TestPicZJ.jpg"); int len=0; byte[] bytes=new byte[1024]; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } //关流: fis.close(); fos.close(); } }
课堂示例代码:
package com.oracle.reader; import java.io.FileReader; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) throws IOException { FileReader fr=new FileReader("F:\IOTest\TestReader.txt"); int len=0; char[] ch=new char[1024*10240]; while((len=fr.read(ch))!=-1){ System.out.print(new String(ch,0,len)); } fr.close(); } public static void method1(FileReader fr) throws IOException{ int len=0; while((len=fr.read())!=-1){ System.out.print((char)len); } fr.close(); } } package com.oracle.reader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class RWCopy { public static void main(String[] args) throws IOException { //明确数据源: FileReader fr=new FileReader("F:\IOTest\TestReader.txt"); //明确目的地: FileWriter fw=new FileWriter("F:\IOTest\TestReaderCopy.txt"); int len=0; char[] ch=new char[1024*10240]; while((len=fr.read(ch))!=-1){ fw.write(new String(ch,0,len)); } fr.close(); fw.close(); } } package com.oracle.reader; import java.io.FileWriter; import java.io.IOException; public class WriterDemo { public static void main(String[] args) throws IOException { //写入中文字符: FileWriter fw=new FileWriter("F:\IOTest\TestReader.txt"); //写在缓冲区内: fw.write("火云邪神,天下第一"); //刷入或者close关闭写入文件: fw.flush(); //fw.close(); } }
文件复制代码:
package com.oracle.copyfile; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyDemo { public static void main(String[] args) throws IOException { long time1=System.currentTimeMillis(); //明确数据源(要复制哪个文件): @SuppressWarnings("resource") FileInputStream fis=new FileInputStream("E:\文件下载\CentOS-6.0-i386-LiveCD.iso"); //明确目的地(要复制到哪儿去): @SuppressWarnings("resource") FileOutputStream fos=new FileOutputStream("E:\文件下载\FUCK\CentOS-6.0-i386-LiveCD.iso",true); byte[] bytes=new byte[5]; int len=0; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } long time2=System.currentTimeMillis(); System.out.println(time2-time1); fis.close(); fos.close(); } } package com.oracle.copyfile; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyDemo02 { public static void main(String[] args) throws IOException { //字节流用于不带中文字符的赋值复制! long time1=System.currentTimeMillis(); //明确数据源: FileInputStream fis=new FileInputStream("E:\文件下载\CentOS-6.0-i386-LiveCD.iso"); //明确目的地: FileOutputStream fos=new FileOutputStream("E:\文件下载\FUCK\CentOS-6.0-i386-LiveCD.iso"); int len=0;//获取读取字节的有效长度 byte[] bytes=new byte[1024*10240]; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } long time2=System.currentTimeMillis(); System.out.println(time2-time1); fis.close(); fos.close(); } }