学习Java IO,不得不提到的就是JavaIO流。html
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各类类,方便更直观的进行数据操做。java
根据处理数据类型的不一样分为:字符流和字节流linux
根据数据流向不一样分为:输入流和输出流windows
字符流的由来: 由于数据编码的不一样,而有了对字符进行高效操做的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:数组
(1)读写单位不一样:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。多线程
(2)处理对象不一样:字节流能处理全部类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。dom
(3)字节流在操做的时候自己是不会用到缓冲区的,是文件自己的直接操做的;而字符流在操做的时候下后是会用到缓冲区的,是经过缓冲区来操做文件,咱们将在下面验证这一点。ide
结论:优先选用字节流。首先由于硬盘上的全部文件都是以字节的形式进行传输或者保存的,包括图片等内容。可是字符只是在内存中才会造成的,因此在开发中,字节流使用普遍。函数
对输入流只能进行读操做,对输出流只能进行写操做,程序中须要根据待传输数据的不一样特性而使用不一样的流。工具
从输入字节流的继承图能够看出:
InputStream 是全部的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream 是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。
ObjectInputStream 和全部FilterInputStream的子类都是装饰流(装饰器模式的主角)。意思是FileInputStream类能够经过一个String路径名建立一个对象,FileInputStream(String name)。而DataInputStream必须装饰一个类才能返回一个对象,DataInputStream(InputStream in)。以下图示:
【案例 】读取文件内容
/** * 字节流 * 读文件内容 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; in.read(b); in.close(); System.out.println(new String(b)); } }
注意:该示例中因为b字节数组长度为1024,若是文件较小,则会有大量填充空格。咱们能够利用in.read(b);的返回值来设计程序,以下案例:
【案例】读取文件内容
/** * 字节流 * 读文件内容 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int len=in.read(b); in.close(); System.out.println("读入长度为:"+len); System.out.println(new String(b,0,len)); } }
注意:观察上面的例子能够看出,咱们预先申请了一个指定大小的空间,可是有时候这个空间可能过小,有时候可能太大,咱们须要准确的大小,这样节省空间,那么咱们能够这样作:
【案例】读取文件内容
/** * 字节流 * 读文件内容,节省空间 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[(int)f.length()]; in.read(b); System.out.println("文件长度为:"+f.length()); in.close(); System.out.println(new String(b)); } }
【案例】逐字节读
/** * 字节流 * 读文件内容,节省空间 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[(int)f.length()]; for (int i = 0; i < b.length; i++) { b[i]=(byte)in.read(); } in.close(); System.out.println(new String(b)); } }
注意:上面的几个例子都是在知道文件的内容多大,而后才展开的,有时候咱们不知道文件有多大,这种状况下,咱们须要判断是否独到文件的末尾。
【案例】字节流读取文件
/** * 字节流 *读文件 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int count =0; int temp=0; while((temp=in.read())!=(-1)){ b[count++]=(byte)temp; } in.close(); System.out.println(new String(b)); } }
注意:当读到文件末尾的时候会返回-1.正常状况下是不会返回-1的。
【案例】DataInputStream类
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class DataOutputStreamDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator +"hello.txt"); DataInputStream input = new DataInputStream(new FileInputStream(file)); char[] ch = new char[10]; int count = 0; char temp; while((temp = input.readChar()) != 'C'){ ch[count++] = temp; } System.out.println(ch); } }
【案例】PushBackInputStream回退流操做
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputStream; /** * 回退流操做 * */ public class PushBackInputStreamDemo{ public static void main(String[] args) throwsIOException{ String str = "hello,rollenholt"; PushbackInputStream push = null; ByteArrayInputStream bat = null; bat = new ByteArrayInputStream(str.getBytes()); push = new PushbackInputStream(bat); int temp = 0; while((temp = push.read()) != -1){ if(temp == ','){ push.unread(temp); temp = push.read(); System.out.print("(回退" +(char) temp + ") "); }else{ System.out.print((char) temp); } } } }
IO 中输出字节流的继承图可见上图,能够看出:
OutputStream 是全部的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,
ObjectOutputStream 和全部FilterOutputStream的子类都是装饰流。具体例子跟InputStream是对应的。
【案例】向文件中写入字符串
/** * 字节流 * 向文件中写入字符串 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f); String str="Hello World"; byte[] b=str.getBytes(); out.write(b); out.close(); } }
你也能够一个字节一个字节的写入文件:
【案例】逐字节写入文件
/** * 字节流 * 向文件中一个字节一个字节的写入字符串 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f); String str="Hello World!!"; byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } }
【案例】向文件中追加新内容
/** * 字节流 * 向文件中追加新内容: * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f,true);//true表示追加模式,不然为覆盖 String str="Rollen"; //String str="\r\nRollen"; 能够换行 byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } }
【案例】复制文件
/** * 文件的复制 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { if(args.length!=2){ System.out.println("命令行参数输入有误,请检查"); System.exit(1); } File file1=new File(args[0]); File file2=new File(args[1]); if(!file1.exists()){ System.out.println("被复制的文件不存在"); System.exit(1); } InputStream input=new FileInputStream(file1); OutputStream output=new FileOutputStream(file2); if((input!=null)&&(output!=null)){ int temp=0; while((temp=input.read())!=(-1)){ output.write(temp); } } input.close(); output.close(); } }
【案例】使用内存操做流将一个大写字母转化为小写字母
/** * 使用内存操做流将一个大写字母转化为小写字母 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String str="ROLLENHOLT"; ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream output=new ByteArrayOutputStream(); int temp=0; while((temp=input.read())!=-1){ char ch=(char)temp; output.write(Character.toLowerCase(ch)); } String outStr=output.toString(); input.close(); output.close(); System.out.println(outStr); } }
【案例】验证管道流:进程间通讯
/** * 验证管道流 * */ import java.io.*; /** * 消息发送类 * */ class Send implements Runnable{ private PipedOutputStream out=null; public Send() { out=new PipedOutputStream(); } public PipedOutputStream getOut(){ return this.out; } public void run(){ String message="hello , Rollen"; try{ out.write(message.getBytes()); }catch (Exception e) { e.printStackTrace(); }try{ out.close(); }catch (Exception e) { e.printStackTrace(); } } } /** * 接受消息类 * */ class Recive implements Runnable{ private PipedInputStream input=null; public Recive(){ this.input=new PipedInputStream(); } public PipedInputStream getInput(){ return this.input; } public void run(){ byte[] b=new byte[1000]; int len=0; try{ len=this.input.read(b); }catch (Exception e) { e.printStackTrace(); }try{ input.close(); }catch (Exception e) { e.printStackTrace(); } System.out.println("接受的内容为 "+(new String(b,0,len))); } } /** * 测试类 * */ class hello{ public static void main(String[] args) throws IOException { Send send=new Send(); Recive recive=new Recive(); try{ //管道链接 send.getOut().connect(recive.getInput()); }catch (Exception e) { e.printStackTrace(); } new Thread(send).start(); new Thread(recive).start(); } }
【案例】DataOutputStream类示例
import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator +"hello.txt"); char[] ch = { 'A', 'B', 'C' }; DataOutputStream out = null; out = new DataOutputStream(new FileOutputStream(file)); for(char temp : ch){ out.writeChar(temp); } out.close(); } }
【案例】ZipOutputStream类
先看一下ZipOutputStream类的继承关系
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.util.zip.DeflaterOutputStream
java.util.zip.ZipOutputStream
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipOutputStreamDemo1{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator +"hello.txt"); File zipFile = new File("d:" + File.separator +"hello.zip"); InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( zipFile)); zipOut.putNextEntry(new ZipEntry(file.getName())); // 设置注释 zipOut.setComment("hello"); int temp = 0; while((temp = input.read()) != -1){ zipOut.write(temp); } input.close(); zipOut.close(); } }
【案例】ZipOutputStream类压缩多个文件
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 一次性压缩多个文件 * */ public class ZipOutputStreamDemo2{ public static void main(String[] args) throws IOException{ // 要被压缩的文件夹 File file = new File("d:" + File.separator +"temp"); File zipFile = new File("d:" + File.separator + "zipFile.zip"); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( zipFile)); zipOut.setComment("hello"); if(file.isDirectory()){ File[] files = file.listFiles(); for(int i = 0; i < files.length; ++i){ input = newFileInputStream(files[i]); zipOut.putNextEntry(newZipEntry(file.getName() + File.separator +files[i].getName())); int temp = 0; while((temp = input.read()) !=-1){ zipOut.write(temp); } input.close(); } } zipOut.close(); } }
【案例】ZipFile类展现
import java.io.File; import java.io.IOException; import java.util.zip.ZipFile; /** *ZipFile演示 * */ public class ZipFileDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator +"hello.zip"); ZipFile zipFile = new ZipFile(file); System.out.println("压缩文件的名称为:" + zipFile.getName()); } }
【案例】解压缩文件(压缩文件中只有一个文件的状况)
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * 解压缩文件(压缩文件中只有一个文件的状况) * */ public class ZipFileDemo2{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator +"hello.zip"); File outFile = new File("d:" + File.separator +"unZipFile.txt"); ZipFile zipFile = new ZipFile(file); ZipEntry entry =zipFile.getEntry("hello.txt"); InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } }
【案例】ZipInputStream类解压缩一个压缩文件中包含多个文件的状况
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; /** * 解压缩一个压缩文件中包含多个文件的状况 * */ public class ZipFileDemo3{ public static void main(String[] args) throws IOException{ File file = new File("d:" +File.separator + "zipFile.zip"); File outFile = null; ZipFile zipFile = new ZipFile(file); ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file)); ZipEntry entry = null; InputStream input = null; OutputStream output = null; while((entry = zipInput.getNextEntry()) != null){ System.out.println("解压缩" + entry.getName() + "文件"); outFile = new File("d:" + File.separator + entry.getName()); if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdir(); } if(!outFile.exists()){ outFile.createNewFile(); } input = zipFile.getInputStream(entry); output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } } }
图中蓝色的为主要的对应部分,红色的部分就是不对应部分。紫色的虚线部分表明这些流通常要搭配使用。从上面的图中能够看出Java IO 中的字节流是极其对称的。哲学上讲“存在及合理”,如今咱们看看这些字节流中不太对称的几个类吧!
主要完成从流中读取数据时,会获得相应的行号,至于何时分行、在哪里分行是由改类主动肯定的,并非在原始中有这样一个行号。在输出部分没有对应的部分,咱们彻底能够本身创建一个LineNumberOutputStream,在最初写入时会有一个基准的行号,之后每次遇到换行时会在下一行添加一个行号,看起来也是能够的。好像更不入流了。
其功能是查看最后一个字节,不满意就放入缓冲区。主要用在编译器的语法、词法分析部分。输出部分的BufferedOutputStream 几乎实现相近的功能。
StringBufferInputStream
已经被Deprecated,自己就不该该出如今InputStream部分,主要由于String 应该属于字符流的范围。已经被废弃了,固然输出部分也没有必要须要它了!还容许它存在只是为了保持版本的向下兼容而已。
能够认为是一个工具类,将两个或者多个输入流当成一个输入流依次读取。彻底能够从IO 包中去除,还彻底不影响IO 包的结构,却让其更“纯洁”――纯洁的Decorator 模式。
【案例】将两个文本文件合并为另一个文本文件
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; /** * 将两个文本文件合并为另一个文本文件 * */ public class SequenceInputStreamDemo{ public static voidmain(String[] args) throws IOException{ File file1 = newFile("d:" + File.separator + "hello1.txt"); File file2 = newFile("d:" + File.separator + "hello2.txt"); File file3 = newFile("d:" + File.separator + "hello.txt"); InputStream input1 =new FileInputStream(file1); InputStream input2 =new FileInputStream(file2); OutputStream output =new FileOutputStream(file3); // 合并流 SequenceInputStreamsis = new SequenceInputStream(input1, input2); int temp = 0; while((temp =sis.read()) != -1){ output.write(temp); } input1.close(); input2.close(); output.close(); sis.close(); } }
也能够认为是一个辅助工具。主要能够向其余输出流,或者FileInputStream 写入数据,自己内部实现仍是带缓冲的。本质上是对其它流的综合运用的一个工具而已。同样能够踢出IO 包!System.err和System.out 就是PrintStream 的实例!
【案例】使用PrintStream进行输出
/** * 使用PrintStream进行输出 * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { PrintStream print = new PrintStream(new FileOutputStream(newFile("d:" + File.separator +"hello.txt"))); print.println(true); print.println("Rollen"); print.close(); } }
【案例】使用PrintStream进行格式化输出
/** * 使用PrintStream进行输出 * 并进行格式化 * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { PrintStream print = new PrintStream(new FileOutputStream(newFile("d:" + File.separator +"hello.txt"))); String name="Rollen"; int age=20; print.printf("姓名:%s. 年龄:%d.",name,age); print.close(); } }
【案例】使用OutputStream向屏幕上输出内容
/** * 使用OutputStream向屏幕上输出内容 * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { OutputStream out=System.out; try{ out.write("hello".getBytes()); }catch (Exception e) { e.printStackTrace(); } try{ out.close(); }catch (Exception e) { e.printStackTrace(); } } }
【案例】输入输出重定向
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /** * 为System.out.println()重定向输出 * */ public class systemDemo{ public static void main(String[] args){ // 此刻直接输出到屏幕 System.out.println("hello"); File file = new File("d:" + File.separator +"hello.txt"); try{ System.setOut(new PrintStream(new FileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.out.println("这些内容在文件中才能看到哦!"); } }
【案例】使用System.err重定向
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /** *System.err重定向这个例子也提示咱们可使用这种方法保存错误信息 * */ public class systemErr{ public static void main(String[] args){ File file = new File("d:" + File.separator +"hello.txt"); System.err.println("这些在控制台输出"); try{ System.setErr(new PrintStream(new FileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.err.println("这些在文件中才能看到哦!"); } }
【案例】System.in重定向
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** *System.in重定向 * */ public class systemIn{ public static void main(String[] args){ File file = new File("d:" + File.separator +"hello.txt"); if(!file.exists()){ return; }else{ try{ System.setIn(newFileInputStream(file)); }catch(FileNotFoundException e){ e.printStackTrace(); } byte[] bytes = new byte[1024]; int len = 0; try{ len = System.in.read(bytes); }catch(IOException e){ e.printStackTrace(); } System.out.println("读入的内容为:" + new String(bytes, 0, len)); } } }
在上面的继承关系图中能够看出:
Reader 是全部的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。
BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。
FilterReader 是全部自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增长一个行号。
InputStreamReader 是一个链接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader能够说是一个达到此功能、经常使用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。咱们能够从这个类中获得必定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。后面会有Reader 与InputStream 的对应关系。
【案例】从文件中读取内容
/** * 字符流 * 从文件中读出内容 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); char[] ch=new char[100]; Reader read=new FileReader(f); int count=read.read(ch); read.close(); System.out.println("读入的长度为:"+count); System.out.println("内容为"+new String(ch,0,count)); } }
注意:固然最好采用循环读取的方式,由于咱们有时候不知道文件到底有多大。
【案例】以循环方式从文件中读取内容
/** * 字符流 * 从文件中读出内容 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); char[] ch=new char[100]; Reader read=new FileReader(f); int temp=0; int count=0; while((temp=read.read())!=(-1)){ ch[count++]=(char)temp; } read.close(); System.out.println("内容为"+new String(ch,0,count)); } }
【案例】BufferedReader的小例子
注意:BufferedReader只能接受字符流的缓冲区,由于每个中文须要占据两个字节,因此须要将System.in这个字节输入流变为字符输入流,采用:
BufferedReader buf = new BufferedReader(newInputStreamReader(System.in));
下面是一个实例:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 使用缓冲区从键盘上读入内容 * */ public class BufferedReaderDemo{ public static void main(String[] args){ BufferedReader buf = new BufferedReader( newInputStreamReader(System.in)); String str = null; System.out.println("请输入内容"); try{ str = buf.readLine(); }catch(IOException e){ e.printStackTrace(); } System.out.println("你输入的内容是:" + str); } }
【案例】Scanner类实例
import java.util.Scanner; /** *Scanner的小例子,从键盘读数据 * */ public class ScannerDemo{ publicstatic void main(String[] args){ Scanner sca = new Scanner(System.in); // 读一个整数 int temp = sca.nextInt(); System.out.println(temp); //读取浮点数 float flo=sca.nextFloat(); System.out.println(flo); //读取字符 //...等等的,都是一些太基础的,就不师范了。 } }
【案例】Scanner类从文件中读出内容
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** *Scanner的小例子,从文件中读内容 * */ public class ScannerDemo{ public static void main(String[] args){ File file = new File("d:" + File.separator +"hello.txt"); Scanner sca = null; try{ sca = new Scanner(file); }catch(FileNotFoundException e){ e.printStackTrace(); } String str = sca.next(); System.out.println("从文件中读取的内容是:" + str); } }
在上面的关系图中能够看出:
Writer 是全部的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
PipedWriter 是向与其它线程共用的管道中写入数据,
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。
PrintWriter 和PrintStream 极其相似,功能和使用也很是类似。
OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类(具体能够研究一SourceCode)。功能和使用和OutputStream 极其相似,后面会有它们的对应图。
【案例】向文件中写入数据
/** * 字符流 * 写入数据 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); Writer out =new FileWriter(f); String str="hello"; out.write(str); out.close(); } }
注意:这个例子上以前的例子没什么区别,只是你能够直接输入字符串,而不须要你将字符串转化为字节数组。当你若是想问文件中追加内容的时候,可使用将上面的声明out的哪一行换为:
Writer out =new FileWriter(f,true);
这样,当你运行程序的时候,会发现文件内容变为:hellohello若是想在文件中换行的话,须要使用“\r\n”好比将str变为String str="\r\nhello";这样文件追加的str的内容就会换行了。
(1)其是字符流和字节流之间的桥梁
(2)可对读取到的字节数据通过指定编码转换成字符
(3)可对读取到的字符数据通过指定编码转换成字节
当字节和字符之间有转换动做时;
流操做的数据须要编码或解码时。
InputStreamReader:字节到字符的桥梁
OutputStreamWriter:字符到字节的桥梁
这两个流对象是字符体系中的成员,它们有转换做用,自己又是字符流,因此在构造的时候须要传入字节流对象进来。
【案例】将字节输出流转化为字符输出流
/** * 将字节输出流转化为字符输出流 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Writer out=new OutputStreamWriter(new FileOutputStream(file)); out.write("hello"); out.close(); } }
【案例】将字节输入流转换为字符输入流
/** * 将字节输入流变为字符输入流 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Reader read=new InputStreamReader(new FileInputStream(file)); char[] b=new char[100]; int len=read.read(b); System.out.println(new String(b,0,len)); read.close(); } }
File类是对文件系统中文件以及文件夹进行封装的对象,能够经过对象的思想来操做文件和文件夹。 File类保存文件或目录的各类元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、得到当前目录中的文件列表,建立、删除文件和目录等方法。
【案例 】建立一个文件
import java.io.*; class hello{ public static void main(String[] args) { File f=new File("D:\\hello.txt"); try{ f.createNewFile(); }catch (Exception e) { e.printStackTrace(); } } }
【案例2】File类的两个常量
import java.io.*; class hello{ public static void main(String[] args) { System.out.println(File.separator); System.out.println(File.pathSeparator); } }
此处多说几句:有些同窗可能认为,我直接在windows下使用\进行分割不行吗?固然是能够的。可是在linux下就不是\了。因此,要想使得咱们的代码跨平台,更加健壮,因此,你们都采用这两个常量吧,其实也多写不了几行。
【案例3】File类中的常量改写案例1的代码:
import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); try{ f.createNewFile(); }catch (Exception e) { e.printStackTrace(); } } }
【案例4】删除一个文件(或者文件夹)
import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); if(f.exists()){ f.delete(); }else{ System.out.println("文件不存在"); } } }
【案例5】建立一个文件夹
/** * 建立一个文件夹 * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello"; File f=new File(fileName); f.mkdir(); } }
【案例6】列出目录下的全部文件
/** * 使用list列出指定目录的所有文件 * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); String[] str=f.list(); for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } }
注意使用list返回的是String数组,。并且列出的不是完整路径,若是想列出完整路径的话,须要使用listFiles.它返回的是File的数组。
【案例7】列出指定目录的所有文件(包括隐藏文件):
/** * 使用listFiles列出指定目录的所有文件 * listFiles输出的是完整路径 * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); File[] str=f.listFiles(); for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } }
【案例8】判断一个指定的路径是否为目录
/** * 使用isDirectory判断一个指定的路径是否为目录 * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); if(f.isDirectory()){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
【案例9】递归搜索指定目录的所有内容,包括文件和文件夹
* 列出指定目录的所有内容 * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); print(f); } public static void print(File f){ if(f!=null){ if(f.isDirectory()){ File[] fileArray=f.listFiles(); if(fileArray!=null){ for (int i = 0; i
该对象并非流体系中的一员,其封装了字节流,同时还封装了一个缓冲区(字符数组),经过内部的指针来操做字符数组中的数据。该对象特色:
该对象只能操做文件,因此构造函数接收两种类型的参数:a.字符串文件路径;b.File对象。
该对象既能够对文件进行读操做,也能进行写操做,在进行对象实例化时可指定操做模式(r,rw)
注意:该对象在实例化时,若是要操做的文件不存在,会自动建立;若是文件存在,写数据未指定位置,会从头开始写,即覆盖原有的内容。能够用于多线程下载或多个线程同时写数据到文件。
【案例】使用RandomAccessFile写入文件
/** * 使用RandomAccessFile写入文件 * */ import java.io.*; class hello{ public static void main(String[]args) throws IOException { StringfileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); RandomAccessFile demo=newRandomAccessFile(f,"rw"); demo.writeBytes("asdsad"); demo.writeInt(12); demo.writeBoolean(true); demo.writeChar('A'); demo.writeFloat(1.21f); demo.writeDouble(12.123); demo.close(); } }
【案例 】取得本地的默认编码
/** * 取得本地的默认编码 * */ publicclass CharSetDemo{ public static void main(String[] args){ System.out.println("系统默认编码为:"+ System.getProperty("file.encoding")); } }
【案例 】乱码的产生
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * 乱码的产生 * */ public class CharSetDemo2{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); OutputStream out = new FileOutputStream(file); byte[] bytes = "你好".getBytes("ISO8859-1"); out.write(bytes); out.close(); }//输出结果为乱码,系统默认编码为GBK,而此处编码为ISO8859-1 }
对象序列化就是把一个对象变为二进制数据流的一种方法。
一个类要想被序列化,就行必须实现java.io.Serializable接口。虽然这个接口中没有任何方法,就如同以前的cloneable接口同样。实现了这个接口以后,就表示这个类具备被序列化的能力。先让咱们实现一个具备序列化能力的类吧:
【案例 】实现具备序列化能力的类
import java.io.*; /** * 实现具备序列化能力的类 * */ public class SerializableDemo implements Serializable{ public SerializableDemo(){ } publicSerializableDemo(String name, int age){ this.name=name; this.age=age; } @Override public String toString(){ return "姓名:"+name+" 年龄:"+age; } private String name; private int age; }
【案例 】序列化一个对象 – ObjectOutputStream
import java.io.Serializable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; /** * 实现具备序列化能力的类 * */ public class Person implements Serializable{ public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public String toString(){ return "姓名:" +name + " 年龄:" +age; } private String name; private int age; } /** * 示范ObjectOutputStream * */ public class ObjectOutputStreamDemo{ public static voidmain(String[] args) throws IOException{ File file = newFile("d:" + File.separator + "hello.txt"); ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream( file)); oos.writeObject(newPerson("rollen", 20)); oos.close(); } }
【案例 】反序列化—ObjectInputStream
import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; /** * ObjectInputStream示范 * */ public class ObjectInputStreamDemo{ public static voidmain(String[] args) throws Exception{ File file = new File("d:" +File.separator + "hello.txt"); ObjectInputStreaminput = new ObjectInputStream(new FileInputStream( file)); Object obj =input.readObject(); input.close(); System.out.println(obj); } }
注意:被Serializable接口声明的类的对象的属性都将被序列化,可是若是想自定义序列化的内容的时候,就须要实现Externalizable接口。
当一个类要使用Externalizable这个接口的时候,这个类中必需要有一个无参的构造函数,若是没有的话,在构造的时候会产生异常,这是由于在反序列话的时候会默认调用无参的构造函数。
如今咱们来演示一下序列化和反序列话:
【案例 】使用Externalizable来定制序列化和反序列化操做
package IO; import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; /** * 序列化和反序列化的操做 * */ public class ExternalizableDemo{ public static voidmain(String[] args) throws Exception{ ser(); // 序列化 dser(); // 反序列话 } public static void ser()throws Exception{ File file = newFile("d:" + File.separator + "hello.txt"); ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(newPerson("rollen", 20)); out.close(); } public static void dser()throws Exception{ File file = newFile("d:" + File.separator + "hello.txt"); ObjectInputStreaminput = new ObjectInputStream(new FileInputStream( file)); Object obj =input.readObject(); input.close(); System.out.println(obj); } } class Person implements Externalizable{ public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } @Override public String toString(){ return "姓名:" +name + " 年龄:" +age; } // 复写这个方法,根据须要能够保存的属性或者具体内容,在序列化的时候使用 @Override public voidwriteExternal(ObjectOutput out) throws IOException{ out.writeObject(this.name); out.writeInt(age); } // 复写这个方法,根据须要读取内容 反序列话的时候须要 @Override public voidreadExternal(ObjectInput in) throws IOException, ClassNotFoundException{ this.name = (String)in.readObject(); this.age =in.readInt(); } private String name; private int age; }
注意:Serializable接口实现的操做实际上是吧一个对象中的所有属性进行序列化,固然也可使用咱们上使用是Externalizable接口以实现部分属性的序列化,可是这样的操做比较麻烦,
当咱们使用Serializable接口实现序列化操做的时候,若是一个对象的某一个属性不想被序列化保存下来,那么咱们可使用transient关键字进行说明:
【案例 】使用transient关键字定制序列化和反序列化操做
package IO; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * 序列化和反序列化的操做 * */ public class serDemo{ public static voidmain(String[] args) throws Exception{ ser(); // 序列化 dser(); // 反序列话 } public static void ser()throws Exception{ File file = newFile("d:" + File.separator + "hello.txt"); ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(newPerson1("rollen", 20)); out.close(); } public static void dser()throws Exception{ File file = newFile("d:" + File.separator + "hello.txt"); ObjectInputStreaminput = new ObjectInputStream(new FileInputStream( file)); Object obj =input.readObject(); input.close(); System.out.println(obj); } } class Person1 implements Serializable{ public Person1(){ } public Person1(Stringname, int age){ this.name = name; this.age = age; } @Override public String toString(){ return "姓名:" +name + " 年龄:" +age; } // 注意这里 private transient Stringname; private int age; }
【运行结果】:
姓名:null 年龄:20
【案例 】序列化一组对象
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * 序列化一组对象 * */ public class SerDemo1{ public static voidmain(String[] args) throws Exception{ Student[] stu = { newStudent("hello", 20), new Student("world", 30), newStudent("rollen", 40) }; ser(stu); Object[] obj = dser(); for(int i = 0; i
一、http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
二、http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html
三、转载自红黑联盟