流的概念和作用

学习JavaIO,不得不提到的就是JavaIO流。

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类

根据处理数据类型的不同分为:字符流和字节流

根据数据流向不同分为:输入流和输出流

字符流和字节流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:

(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

输入流和输出流

对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

Java流操作有关的类或接口:

Java流类图结构:


Java IO流对象

1. 输入字节流InputStream

定义和结构说明:

从输入字节流的继承图可以看出:

InputStream 是所有的输入字节流的父类,它是一个抽象类。

ByteArrayInputStream、StringBufferInputStream、FileInputStream
是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream
是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。

ObjectInputStream
和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。意思是FileInputStream类可以通过一个String路径名创建一个对象,FileInputStream(String
name)。而DataInputStream必须装饰一个类才能返回一个对象,DataInputStream(InputStream
in)。如下图示:

实例操作演示:

【案例 】读取文件内容

  1. /**
  2. * 字节流
  3. * 读文件内容
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. InputStream in=new FileInputStream(f);
  11. byte[] b=new byte[1024];
  12. in.read(b);
  13. in.close();
  14. System.out.println(new String(b));
  15. }
  16. }

注意:该示例中由于b字节数组长度为1024,如果文件较小,则会有大量填充空格。我们可以利用in.read(b);的返回值来设计程序,如下案例:

【案例】读取文件内容

  1. /**
  2. * 字节流
  3. * 读文件内容
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. InputStream in=new FileInputStream(f);
  11. byte[] b=new byte[1024];
  12. int len=in.read(b);
  13. in.close();
  14. System.out.println("读入长度为:"+len);
  15. System.out.println(new String(b,0,len));
  16. }
  17. }

注意:观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样做:

【案例】读取文件内容

  1. /**
  2. * 字节流
  3. * 读文件内容,节省空间
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. InputStream in=new FileInputStream(f);
  11. byte[] b=new byte[(int)f.length()];
  12. in.read(b);
  13. System.out.println("文件长度为:"+f.length());
  14. in.close();
  15. System.out.println(new String(b));
  16. }
  17. }

【案例】逐字节读

  1. /**
  2. * 字节流
  3. * 读文件内容,节省空间
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. InputStream in=new FileInputStream(f);
  11. byte[] b=new byte[(int)f.length()];
  12. for (int i = 0; i < b.length; i++) {
  13. b[i]=(byte)in.read();
  14. }
  15. in.close();
  16. System.out.println(new String(b));
  17. }
  18. }

注意:上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。

【案例】字节流读取文件

  1. /**
  2. * 字节流
  3. *读文件
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. InputStream in=new FileInputStream(f);
  11. byte[] b=new byte[1024];
  12. int count =0;
  13. int temp=0;
  14. while((temp=in.read())!=(-1)){
  15. b[count++]=(byte)temp;
  16. }
  17. in.close();
  18. System.out.println(new String(b));
  19. }
  20. }

注意:当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的。

【案例】DataInputStream类

  1. import java.io.DataInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. public class DataOutputStreamDemo{
  6. public static void main(String[] args) throws IOException{
  7. File file = new File("d:" + File.separator +"hello.txt");
  8. DataInputStream input = new DataInputStream(new FileInputStream(file));
  9. char[] ch = new char[10];
  10. int count = 0;
  11. char temp;
  12. while((temp = input.readChar()) != 'C'){
  13. ch[count++] = temp;
  14. }
  15. System.out.println(ch);
  16. }
  17. }

【案例】PushBackInputStream回退流操作

  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.io.PushbackInputStream;
  4. /**
  5. * 回退流操作
  6. * */
  7. public class PushBackInputStreamDemo{
  8. public static void main(String[] args) throwsIOException{
  9. String str = "hello,rollenholt";
  10. PushbackInputStream push = null;
  11. ByteArrayInputStream bat = null;
  12. bat = new ByteArrayInputStream(str.getBytes());
  13. push = new PushbackInputStream(bat);
  14. int temp = 0;
  15. while((temp = push.read()) != -1){
  16. if(temp == ','){
  17. push.unread(temp);
  18. temp = push.read();
  19. System.out.print("(回退" +(char) temp + ") ");
  20. }else{
  21. System.out.print((char) temp);
  22. }
  23. }
  24. }
  25. }

2. 输出字节流OutputStream

定义和结构说明:

IO 中输出字节流的继承图可见上图,可以看出:

OutputStream 是所有的输出字节流的父类,它是一个抽象类。

ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,

ObjectOutputStream 和所有FilterOutputStream的子类都是装饰流。具体例子跟InputStream是对应的。

实例操作演示:

【案例】向文件中写入字符串

  1. /**
  2. * 字节流
  3. * 向文件中写入字符串
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. OutputStream out =new FileOutputStream(f);
  11. String str="Hello World";
  12. byte[] b=str.getBytes();
  13. out.write(b);
  14. out.close();
  15. }
  16. }

你也可以一个字节一个字节的写入文件:

【案例】逐字节写入文件

  1. /**
  2. * 字节流
  3. * 向文件中一个字节一个字节的写入字符串
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. OutputStream out =new FileOutputStream(f);
  11. String str="Hello World!!";
  12. byte[] b=str.getBytes();
  13. for (int i = 0; i < b.length; i++) {
  14. out.write(b[i]);
  15. }
  16. out.close();
  17. }
  18. }

【案例】向文件中追加新内容

  1. /**
  2. * 字节流
  3. * 向文件中追加新内容:
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
  11. String str="Rollen";
  12. //String str="\r\nRollen"; 可以换行
  13. byte[] b=str.getBytes();
  14. for (int i = 0; i < b.length; i++) {
  15. out.write(b[i]);
  16. }
  17. out.close();
  18. }
  19. }

【案例】复制文件

  1. /**
  2. * 文件的复制
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) throws IOException {
  7. if(args.length!=2){
  8. System.out.println("命令行参数输入有误,请检查");
  9. System.exit(1);
  10. }
  11. File file1=new File(args[0]);
  12. File file2=new File(args[1]);
  13. if(!file1.exists()){
  14. System.out.println("被复制的文件不存在");
  15. System.exit(1);
  16. }
  17. InputStream input=new FileInputStream(file1);
  18. OutputStream output=new FileOutputStream(file2);
  19. if((input!=null)&&(output!=null)){
  20. int temp=0;
  21. while((temp=input.read())!=(-1)){
  22. output.write(temp);
  23. }
  24. }
  25. input.close();
  26. output.close();
  27. }
  28. }

【案例】使用内存操作流将一个大写字母转化为小写字母

  1. /**
  2. * 使用内存操作流将一个大写字母转化为小写字母
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) throws IOException {
  7. String str="ROLLENHOLT";
  8. ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
  9. ByteArrayOutputStream output=new ByteArrayOutputStream();
  10. int temp=0;
  11. while((temp=input.read())!=-1){
  12. char ch=(char)temp;
  13. output.write(Character.toLowerCase(ch));
  14. }
  15. String outStr=output.toString();
  16. input.close();
  17. output.close();
  18. System.out.println(outStr);
  19. }
  20. }

【案例】验证管道流:进程间通信

  1. /**
  2. * 验证管道流
  3. * */
  4. import java.io.*;
  5. /**
  6. * 消息发送类
  7. * */
  8. class Send implements Runnable{
  9. private PipedOutputStream out=null;
  10. public Send() {
  11. out=new PipedOutputStream();
  12. }
  13. public PipedOutputStream getOut(){
  14. return this.out;
  15. }
  16. public void run(){
  17. String message="hello , Rollen";
  18. try{
  19. out.write(message.getBytes());
  20. }catch (Exception e) {
  21. e.printStackTrace();
  22. }try{
  23. out.close();
  24. }catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. /**
  30. * 接受消息类
  31. * */
  32. class Recive implements Runnable{
  33. private PipedInputStream input=null;
  34. public Recive(){
  35. this.input=new PipedInputStream();
  36. }
  37. public PipedInputStream getInput(){
  38. return this.input;
  39. }
  40. public void run(){
  41. byte[] b=new byte[1000];
  42. int len=0;
  43. try{
  44. len=this.input.read(b);
  45. }catch (Exception e) {
  46. e.printStackTrace();
  47. }try{
  48. input.close();
  49. }catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. System.out.println("接受的内容为 "+(new String(b,0,len)));
  53. }
  54. }
  55. /**
  56. * 测试类
  57. * */
  58. class hello{
  59. public static void main(String[] args) throws IOException {
  60. Send send=new Send();
  61. Recive recive=new Recive();
  62. try{
  63. //管道连接
  64. send.getOut().connect(recive.getInput());
  65. }catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. new Thread(send).start();
  69. new Thread(recive).start();
  70. }
  71. }

【案例】DataOutputStream类示例

  1. import java.io.DataOutputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class DataOutputStreamDemo{
  6. public static void main(String[] args) throws IOException{
  7. File file = new File("d:" + File.separator +"hello.txt");
  8. char[] ch = { 'A', 'B', 'C' };
  9. DataOutputStream out = null;
  10. out = new DataOutputStream(new FileOutputStream(file));
  11. for(char temp : ch){
  12. out.writeChar(temp);
  13. }
  14. out.close();
  15. }
  16. }

【案例】ZipOutputStream类

先看一下ZipOutputStream类的继承关系

Java.lang.Object

java.io.OutputStream

java.io.FilterOutputStream

java.util.zip.DeflaterOutputStream

java.util.zip.ZipOutputStream

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. public class ZipOutputStreamDemo1{
  9. public static void main(String[] args) throws IOException{
  10. File file = new File("d:" + File.separator +"hello.txt");
  11. File zipFile = new File("d:" + File.separator +"hello.zip");
  12. InputStream input = new FileInputStream(file);
  13. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
  14. zipFile));
  15. zipOut.putNextEntry(new ZipEntry(file.getName()));
  16. // 设置注释
  17. zipOut.setComment("hello");
  18. int temp = 0;
  19. while((temp = input.read()) != -1){
  20. zipOut.write(temp);
  21. }
  22. input.close();
  23. zipOut.close();
  24. }
  25. }

【案例】ZipOutputStream类压缩多个文件

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. /**
  9. * 一次性压缩多个文件
  10. * */
  11. public class ZipOutputStreamDemo2{
  12. public static void main(String[] args) throws IOException{
  13. // 要被压缩的文件夹
  14. File file = new File("d:" + File.separator +"temp");
  15. File zipFile = new File("d:" + File.separator + "zipFile.zip");
  16. InputStream input = null;
  17. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
  18. zipFile));
  19. zipOut.setComment("hello");
  20. if(file.isDirectory()){
  21. File[] files = file.listFiles();
  22. for(int i = 0; i < files.length; ++i){
  23. input = newFileInputStream(files[i]);
  24. zipOut.putNextEntry(newZipEntry(file.getName()
  25. + File.separator +files[i].getName()));
  26. int temp = 0;
  27. while((temp = input.read()) !=-1){
  28. zipOut.write(temp);
  29. }
  30. input.close();
  31. }
  32. }
  33. zipOut.close();
  34. }
  35. }

【案例】ZipFile类展示

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.zip.ZipFile;
  4. /**
  5. *ZipFile演示
  6. * */
  7. public class ZipFileDemo{
  8. public static void main(String[] args) throws IOException{
  9. File file = new File("d:" + File.separator +"hello.zip");
  10. ZipFile zipFile = new ZipFile(file);
  11. System.out.println("压缩文件的名称为:" + zipFile.getName());
  12. }
  13. }

【案例】解压缩文件(压缩文件中只有一个文件的情况)

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipFile;
  8. /**
  9. * 解压缩文件(压缩文件中只有一个文件的情况)
  10. * */
  11. public class ZipFileDemo2{
  12. public static void main(String[] args) throws IOException{
  13. File file = new File("d:" + File.separator +"hello.zip");
  14. File outFile = new File("d:" + File.separator +"unZipFile.txt");
  15. ZipFile zipFile = new ZipFile(file);
  16. ZipEntry entry =zipFile.getEntry("hello.txt");
  17. InputStream input = zipFile.getInputStream(entry);
  18. OutputStream output = new FileOutputStream(outFile);
  19. int temp = 0;
  20. while((temp = input.read()) != -1){
  21. output.write(temp);
  22. }
  23. input.close();
  24. output.close();
  25. }
  26. }

【案例】ZipInputStream类解压缩一个压缩文件中包含多个文件的情况

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipFile;
  9. import java.util.zip.ZipInputStream;
  10. /**
  11. * 解压缩一个压缩文件中包含多个文件的情况
  12. * */
  13. public class ZipFileDemo3{
  14. public static void main(String[] args) throws IOException{
  15. File file = new File("d:" +File.separator + "zipFile.zip");
  16. File outFile = null;
  17. ZipFile zipFile = new ZipFile(file);
  18. ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
  19. ZipEntry entry = null;
  20. InputStream input = null;
  21. OutputStream output = null;
  22. while((entry = zipInput.getNextEntry()) != null){
  23. System.out.println("解压缩" + entry.getName() + "文件");
  24. outFile = new File("d:" + File.separator + entry.getName());
  25. if(!outFile.getParentFile().exists()){
  26. outFile.getParentFile().mkdir();
  27. }
  28. if(!outFile.exists()){
  29. outFile.createNewFile();
  30. }
  31. input = zipFile.getInputStream(entry);
  32. output = new FileOutputStream(outFile);
  33. int temp = 0;
  34. while((temp = input.read()) != -1){
  35. output.write(temp);
  36. }
  37. input.close();
  38. output.close();
  39. }
  40. }
  41. }

3.字节流的输入与输出的对应图示

图中蓝色的为主要的对应部分,红色的部分就是不对应部分。紫色的虚线部分代表这些流一般要搭配使用。从上面的图中可以看出Java IO 中的字节流是极其对称的。哲学上讲“存在及合理”,现在我们看看这些字节流中不太对称的几个类吧!

4.几个特殊的输入流类分析

LineNumberInputStream

主要完成从流中读取数据时,会得到相应的行号,至于什么时候分行、在哪里分行是由改类主动确定的,并不是在原始中有这样一个行号。在输出部分没有对应的部分,我们完全可以自己建立一个LineNumberOutputStream,在最初写入时会有一个基准的行号,以后每次遇到换行时会在下一行添加一个行号,看起来也是可以的。好像更不入流了。

PushbackInputStream

其功能是查看最后一个字节,不满意就放入缓冲区。主要用在编译器的语法、词法分析部分。输出部分的BufferedOutputStream 几乎实现相近的功能。

StringBufferInputStream

已经被Deprecated,本身就不应该出现在InputStream部分,主要因为String 应该属于字符流的范围。已经被废弃了,当然输出部分也没有必要需要它了!还允许它存在只是为了保持版本的向下兼容而已。

SequenceInputStream

可以认为是一个工具类,将两个或者多个输入流当成一个输入流依次读取。完全可以从IO 包中去除,还完全不影响IO 包的结构,却让其更“纯洁”――纯洁的Decorator 模式。

【案例】将两个文本文件合并为另外一个文本文件

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.SequenceInputStream;
  8. /**
  9. * 将两个文本文件合并为另外一个文本文件
  10. * */
  11. public class SequenceInputStreamDemo{
  12. public static voidmain(String[] args) throws IOException{
  13. File file1 = newFile("d:" + File.separator + "hello1.txt");
  14. File file2 = newFile("d:" + File.separator + "hello2.txt");
  15. File file3 = newFile("d:" + File.separator + "hello.txt");
  16. InputStream input1 =new FileInputStream(file1);
  17. InputStream input2 =new FileInputStream(file2);
  18. OutputStream output =new FileOutputStream(file3);
  19. // 合并流
  20. SequenceInputStreamsis = new SequenceInputStream(input1, input2);
  21. int temp = 0;
  22. while((temp =sis.read()) != -1){
  23. output.write(temp);
  24. }
  25. input1.close();
  26. input2.close();
  27. output.close();
  28. sis.close();
  29. }
  30. }

PrintStream

也可以认为是一个辅助工具。主要可以向其他输出流,或者FileInputStream
写入数据,本身内部实现还是带缓冲的。本质上是对其它流的综合运用的一个工具而已。一样可以踢出IO 包!System.err和System.out
就是PrintStream 的实例!

【案例】使用PrintStream进行输出

  1. /**
  2. * 使用PrintStream进行输出
  3. * */
  4. import java.io.*;
  5. class hello {
  6. public static void main(String[] args) throws IOException {
  7. PrintStream print = new PrintStream(new FileOutputStream(newFile("d:"
  8. + File.separator +"hello.txt")));
  9. print.println(true);
  10. print.println("Rollen");
  11. print.close();
  12. }
  13. }

【案例】使用PrintStream进行格式化输出

  1. /**
  2. * 使用PrintStream进行输出
  3. * 并进行格式化
  4. * */
  5. import java.io.*;
  6. class hello {
  7. public static void main(String[] args) throws IOException {
  8. PrintStream print = new PrintStream(new FileOutputStream(newFile("d:"
  9. + File.separator +"hello.txt")));
  10. String name="Rollen";
  11. int age=20;
  12. print.printf("姓名:%s. 年龄:%d.",name,age);
  13. print.close();
  14. }
  15. }

【案例】使用OutputStream向屏幕上输出内容

  1. /**
  2. * 使用OutputStream向屏幕上输出内容
  3. * */
  4. import java.io.*;
  5. class hello {
  6. public static void main(String[] args) throws IOException {
  7. OutputStream out=System.out;
  8. try{
  9. out.write("hello".getBytes());
  10. }catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. try{
  14. out.close();
  15. }catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

【案例】输入输出重定向

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;
  5. /**
  6. * 为System.out.println()重定向输出
  7. * */
  8. public class systemDemo{
  9. public static void main(String[] args){
  10. // 此刻直接输出到屏幕
  11. System.out.println("hello");
  12. File file = new File("d:" + File.separator +"hello.txt");
  13. try{
  14. System.setOut(new PrintStream(new FileOutputStream(file)));
  15. }catch(FileNotFoundException e){
  16. e.printStackTrace();
  17. }
  18. System.out.println("这些内容在文件中才能看到哦!");
  19. }
  20. }

【案例】使用System.err重定向

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;
  5. /**
  6. *System.err重定向这个例子也提示我们可以使用这种方法保存错误信息
  7. * */
  8. public class systemErr{
  9. public static void main(String[] args){
  10. File file = new File("d:" + File.separator +"hello.txt");
  11. System.err.println("这些在控制台输出");
  12. try{
  13. System.setErr(new PrintStream(new FileOutputStream(file)));
  14. }catch(FileNotFoundException e){
  15. e.printStackTrace();
  16. }
  17. System.err.println("这些在文件中才能看到哦!");
  18. }
  19. }

【案例】System.in重定向

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. /**
  6. *System.in重定向
  7. * */
  8. public class systemIn{
  9. public static void main(String[] args){
  10. File file = new File("d:" + File.separator +"hello.txt");
  11. if(!file.exists()){
  12. return;
  13. }else{
  14. try{
  15. System.setIn(newFileInputStream(file));
  16. }catch(FileNotFoundException e){
  17. e.printStackTrace();
  18. }
  19. byte[] bytes = new byte[1024];
  20. int len = 0;
  21. try{
  22. len = System.in.read(bytes);
  23. }catch(IOException e){
  24. e.printStackTrace();
  25. }
  26. System.out.println("读入的内容为:" + new String(bytes, 0, len));
  27. }
  28. }
  29. }

5.字符输入流Reader

定义和说明:

在上面的继承关系图中可以看出:

Reader 是所有的输入字符流的父类,它是一个抽象类。

CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。

BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。

FilterReader 是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增加一个行号。

InputStreamReader
是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream
转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream
中的类使用一致。后面会有Reader 与InputStream 的对应关系。

实例操作演示:

【案例】从文件中读取内容

  1. /**
  2. * 字符流
  3. * 从文件中读出内容
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. char[] ch=new char[100];
  11. Reader read=new FileReader(f);
  12. int count=read.read(ch);
  13. read.close();
  14. System.out.println("读入的长度为:"+count);
  15. System.out.println("内容为"+new String(ch,0,count));
  16. }
  17. }

注意:当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。

【案例】以循环方式从文件中读取内容

  1. /**
  2. * 字符流
  3. * 从文件中读出内容
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. char[] ch=new char[100];
  11. Reader read=new FileReader(f);
  12. int temp=0;
  13. int count=0;
  14. while((temp=read.read())!=(-1)){
  15. ch[count++]=(char)temp;
  16. }
  17. read.close();
  18. System.out.println("内容为"+new String(ch,0,count));
  19. }
  20. }

【案例】BufferedReader的小例子

注意:BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:

BufferedReader buf = new BufferedReader(newInputStreamReader(System.in));

下面是一个实例:

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. /**
  5. * 使用缓冲区从键盘上读入内容
  6. * */
  7. public class BufferedReaderDemo{
  8. public static void main(String[] args){
  9. BufferedReader buf = new BufferedReader(
  10. newInputStreamReader(System.in));
  11. String str = null;
  12. System.out.println("请输入内容");
  13. try{
  14. str = buf.readLine();
  15. }catch(IOException e){
  16. e.printStackTrace();
  17. }
  18. System.out.println("你输入的内容是:" + str);
  19. }
  20. }

【案例】Scanner类实例

  1. import java.util.Scanner;
  2. /**
  3. *Scanner的小例子,从键盘读数据
  4. * */
  5. public class ScannerDemo{
  6. publicstatic void main(String[] args){
  7. Scanner sca = new Scanner(System.in);
  8. // 读一个整数
  9. int temp = sca.nextInt();
  10. System.out.println(temp);
  11. //读取浮点数
  12. float flo=sca.nextFloat();
  13. System.out.println(flo);
  14. //读取字符
  15. //...等等的,都是一些太基础的,就不师范了。
  16. }
  17. }

【案例】Scanner类从文件中读出内容

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. /**
  5. *Scanner的小例子,从文件中读内容
  6. * */
  7. public class ScannerDemo{
  8. public static void main(String[] args){
  9. File file = new File("d:" + File.separator +"hello.txt");
  10. Scanner sca = null;
  11. try{
  12. sca = new Scanner(file);
  13. }catch(FileNotFoundException e){
  14. e.printStackTrace();
  15. }
  16. String str = sca.next();
  17. System.out.println("从文件中读取的内容是:" + str);
  18. }
  19. }

6.字符输出流Writer

定义和说明:

在上面的关系图中可以看出:

Writer 是所有的输出字符流的父类,它是一个抽象类。

CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。

PipedWriter 是向与其它线程共用的管道中写入数据,

BufferedWriter 是一个装饰器为Writer 提供缓冲功能。

PrintWriter 和PrintStream 极其类似,功能和使用也非常相似。

OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter
其实就是一个实现此功能的具体类(具体可以研究一SourceCode)。功能和使用和OutputStream 极其类似,后面会有它们的对应图。

实例操作演示:

【案例】向文件中写入数据

  1. /**
  2. * 字符流
  3. * 写入数据
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) throws IOException {
  8. String fileName="D:"+File.separator+"hello.txt";
  9. File f=new File(fileName);
  10. Writer out =new FileWriter(f);
  11. String str="hello";
  12. out.write(str);
  13. out.close();
  14. }
  15. }

注意:这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

Writer out =new FileWriter(f,true);

这样,当你运行程序的时候,会发现文件内容变为:hellohello如果想在文件中换行的话,需要使用“\r\n”比如将str变为String str="\r\nhello";这样文件追加的str的内容就会换行了。

7.字符流的输入与输出的对应

8.字符流与字节流转换

转换流的特点:

(1)其是字符流和字节流之间的桥梁

(2)可对读取到的字节数据经过指定编码转换成字符

(3)可对读取到的字符数据经过指定编码转换成字节

何时使用转换流?

当字节和字符之间有转换动作时;

流操作的数据需要编码或解码时。

具体的对象体现:

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁

这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。

字节流和字符流转换实例:

【案例】将字节输出流转化为字符输出流

  1. /**
  2. * 将字节输出流转化为字符输出流
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) throws IOException {
  7. String fileName= "d:"+File.separator+"hello.txt";
  8. File file=new File(fileName);
  9. Writer out=new OutputStreamWriter(new FileOutputStream(file));
  10. out.write("hello");
  11. out.close();
  12. }
  13. }

【案例】将字节输入流转换为字符输入流

  1. /**
  2. * 将字节输入流变为字符输入流
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) throws IOException {
  7. String fileName= "d:"+File.separator+"hello.txt";
  8. File file=new File(fileName);
  9. Reader read=new InputStreamReader(new FileInputStream(file));
  10. char[] b=new char[100];
  11. int len=read.read(b);
  12. System.out.println(new String(b,0,len));
  13. read.close();
  14. }
  15. }

9.File类

File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。
File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。

【案例 】创建一个文件

  1. import java.io.*;
  2. class hello{
  3. public static void main(String[] args) {
  4. File f=new File("D:\\hello.txt");
  5. try{
  6. f.createNewFile();
  7. }catch (Exception e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. }

【案例2】File类的两个常量

  1. import java.io.*;
  2. class hello{
  3. public static void main(String[] args) {
  4. System.out.println(File.separator);
  5. System.out.println(File.pathSeparator);
  6. }
  7. }

此处多说几句:有些同学可能认为,我直接在windows下使用\进行分割不行吗?当然是可以的。但是在Linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧,其实也多写不了几行。

【案例3】File类中的常量改写案例1的代码:

  1. import java.io.*;
  2. class hello{
  3. public static void main(String[] args) {
  4. String fileName="D:"+File.separator+"hello.txt";
  5. File f=new File(fileName);
  6. try{
  7. f.createNewFile();
  8. }catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. }

【案例4】删除一个文件(或者文件夹)

  1. import java.io.*;
  2. class hello{
  3. public static void main(String[] args) {
  4. String fileName="D:"+File.separator+"hello.txt";
  5. File f=new File(fileName);
  6. if(f.exists()){
  7. f.delete();
  8. }else{
  9. System.out.println("文件不存在");
  10. }
  11. }
  12. }

【案例5】创建一个文件夹

  1. /**
  2. * 创建一个文件夹
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) {
  7. String fileName="D:"+File.separator+"hello";
  8. File f=new File(fileName);
  9. f.mkdir();
  10. }
  11. }

【案例6】列出目录下的所有文件

  1. /**
  2. * 使用list列出指定目录的全部文件
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) {
  7. String fileName="D:"+File.separator;
  8. File f=new File(fileName);
  9. String[] str=f.list();
  10. for (int i = 0; i < str.length; i++) {
  11. System.out.println(str[i]);
  12. }
  13. }
  14. }

注意使用list返回的是String数组,。而且列出的不是完整路径,如果想列出完整路径的话,需要使用listFiles.它返回的是File的数组。

【案例7】列出指定目录的全部文件(包括隐藏文件):

  1. /**
  2. * 使用listFiles列出指定目录的全部文件
  3. * listFiles输出的是完整路径
  4. * */
  5. import java.io.*;
  6. class hello{
  7. public static void main(String[] args) {
  8. String fileName="D:"+File.separator;
  9. File f=new File(fileName);
  10. File[] str=f.listFiles();
  11. for (int i = 0; i < str.length; i++) {
  12. System.out.println(str[i]);
  13. }
  14. }
  15. }

【案例8】判断一个指定的路径是否为目录

  1. /**
  2. * 使用isDirectory判断一个指定的路径是否为目录
  3. * */
  4. import java.io.*;
  5. class hello{
  6. public static void main(String[] args) {
  7. String fileName="D:"+File.separator;
  8. File f=new File(fileName);
  9. if(f.isDirectory()){
  10. System.out.println("YES");
  11. }else{
  12. System.out.println("NO");
  13. }
  14. }
  15. }

【案例9】递归搜索指定目录的全部内容,包括文件和文件夹

  1. /* 列出指定目录的全部内容* */
  2. import java.io.File;
  3. class hello {
  4. public static void main(String[] args) {
  5. String fileName = "D:" + File.separator;
  6. File f = new File(fileName);
  7. print(f);
  8. }
  9. public static void print(File f) {
  10. if (f != null) {
  11. if (f.isDirectory()) {
  12. File[] fileArray = f.listFiles();
  13. if (fileArray != null) {
  14. for (int i = 0; i < fileArray.length; i++) {
  15. print(fileArray[i]);// 递归调用
  16. }
  17. } else {
  18. System.out.println(f);
  19. }
  20. }
  21. }
  22. }
  23. }

10.RandomAccessFile类

该对象并不是流体系中的一员,其封装了字节流,同时还封装了一个缓冲区(字符数组),通过内部的指针来操作字符数组中的数据。该对象特点:该对象只能操作文件,所以构造函数接收两种类型的参数:a.字符串文件路径;b.File对象。该对象既可以对文件进行读操作,也能进行写操作,在进行对象实例化时可指定操作模式(r,rw)

注意:该对象在实例化时,如果要操作的文件不存在,会自动创建;如果文件存在,写数据未指定位置,会从头开始写,即覆盖原有的内容。可以用于多线程下载或多个线程同时写数据到文件。

【案例】使用RandomAccessFile写入文件

  1. /*** 使用RandomAccessFile写入文件* */
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. class hello {
  6. public static void main(String[] args) throws IOException {
  7. String fileName = "D:" + File.separator + "hello.txt";
  8. File f = new File(fileName);
  9. RandomAccessFile demo = new RandomAccessFile(f, "rw");
  10. demo.writeBytes("asdsad");
  11. demo.writeInt(12);
  12. demo.writeBoolean(true);
  13. demo.writeChar('A');
  14. demo.writeFloat(1.21f);
  15. demo.writeDouble(12.123);
  16. demo.close();
  17. }
  18. }

11.Java IO流的高级概念

编码问题


【案例 】取得本地的默认编码

  1. /*** 取得本地的默认编码* */
  2. public class CharSetDemo {
  3. public static void main(String[] args) {
  4. System.out.println("系统默认编码为:" + System.getProperty("file.encoding"));
  5. }
  6. }

【案例 】乱码的产生

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. /**
  6. * 乱码的产生
  7. * */
  8. public class CharSetDemo2 {
  9. public static void main(String[] args) throws IOException {
  10. File file = new File("d:" + File.separator + "hello.txt");
  11. OutputStream out = new FileOutputStream(file);
  12. byte[] bytes = "你好".getBytes("ISO8859-1");
  13. out.write(bytes);
  14. out.close();
  15. }// 输出结果为乱码,系统默认编码为GBK,而此处编码为ISO8859-1
  16. }

12.对象的序列化

对象序列化就是把一个对象变为二进制数据流的一种方法。

一个类要想被序列化,就行必须实现java.io.Serializable接口。虽然这个接口中没有任何方法,就如同之前的cloneable接口一样。实现了这个接口之后,就表示这个类具有被序列化的能力。先让我们实现一个具有序列化能力的类吧:

【案例 】实现具有序列化能力的类

  1. import java.io.Serializable;
  2. /**
  3. * 实现具有序列化能力的类
  4. * */
  5. public class SerializableDemo implements Serializable {
  6. public SerializableDemo() {
  7. }
  8. public SerializableDemo(String name, int age) {
  9. this.name = name;
  10. this.age = age;
  11. }
  12. @Override
  13. public String toString() {
  14. return "姓名:" + name + "  年龄:" + age;
  15. }
  16. private String name;
  17. private int age;
  18. }

【案例 】序列化一个对象 – ObjectOutputStream

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectOutputStream;
  5. import java.io.Serializable;
  6. /**
  7. * 实现具有序列化能力的类
  8. * */
  9. public class Person implements Serializable {
  10. public Person() {
  11. }
  12. public Person(String name, int age) {
  13. this.name = name;
  14. this.age = age;
  15. }
  16. @Override
  17. public String toString() {
  18. return "姓名:" + name + "  年龄:" + age;
  19. }
  20. private String name;
  21. private int age;
  22. }
  23. /**
  24. * 示范ObjectOutputStream
  25. * */
  26. public class ObjectOutputStreamDemo {
  27. public static void main(String[] args) throws IOException {
  28. File file = new File("d:" + File.separator + "hello.txt");
  29. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
  30. file));
  31. oos.writeObject(new Person("rollen", 20));
  32. oos.close();
  33. }
  34. }

【案例 】反序列化—ObjectInputStream

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.ObjectInputStream;
  4. /**
  5. * ObjectInputStream示范
  6. * */
  7. public class ObjectInputStreamDemo {
  8. public static void main(String[] args) throws Exception {
  9. File file = new File("d:" + File.separator + "hello.txt");
  10. ObjectInputStream input = new ObjectInputStream(new FileInputStream(
  11. file));
  12. Object obj = input.readObject();
  13. input.close();
  14. System.out.println(obj);
  15. }
  16. }

注意:被Serializable接口声明的类的对象的属性都将被序列化,但是如果想自定义序列化的内容的时候,就需要实现Externalizable接口。

        当一个类要使用Externalizable这个接口的时候,这个类中必须要有一个无参的构造函数,如果没有的话,在构造的时候会产生异常,这是因为在反序列话的时候会默认调用无参的构造函数。

        现在我们来演示一下序列化和反序列话:

【案例 】使用Externalizable来定制序列化和反序列化操作

  1. package IO;
  2. import java.io.Externalizable;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInput;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutput;
  10. import java.io.ObjectOutputStream;
  11. /**
  12. * 序列化和反序列化的操作
  13. * */
  14. public class ExternalizableDemo {
  15. public static void main(String[] args) throws Exception {
  16. ser(); // 序列化
  17. dser(); // 反序列话
  18. }
  19. public static void ser() throws Exception {
  20. File file = new File("d:" + File.separator + "hello.txt");
  21. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
  22. file));
  23. out.writeObject(new Person("rollen", 20));
  24. out.close();
  25. }
  26. public static void dser() throws Exception {
  27. File file = new File("d:" + File.separator + "hello.txt");
  28. ObjectInputStream input = new ObjectInputStream(new FileInputStream(
  29. file));
  30. Object obj = input.readObject();
  31. input.close();
  32. System.out.println(obj);
  33. }
  34. }
  35. class Person implements Externalizable {
  36. public Person() {
  37. }
  38. public Person(String name, int age) {
  39. this.name = name;
  40. this.age = age;
  41. }
  42. @Override
  43. public String toString() {
  44. return "姓名:" + name + "  年龄:" + age;
  45. }
  46. // 复写这个方法,根据需要可以保存的属性或者具体内容,在序列化的时候使用
  47. @Override
  48. public void writeExternal(ObjectOutput out) throws IOException {
  49. out.writeObject(this.name);
  50. out.writeInt(age);
  51. }
  52. // 复写这个方法,根据需要读取内容 反序列话的时候需要
  53. @Override
  54. public void readExternal(ObjectInput in) throws IOException,
  55. ClassNotFoundException {
  56. this.name = (String) in.readObject();
  57. this.age = in.readInt();
  58. }
  59. private String name;
  60. private int age;
  61. }

注意:Serializable接口实现的操作其实是吧一个对象中的全部属性进行序列化,当然也可以使用我们上使用是Externalizable接口以实现部分属性的序列化,但是这样的操作比较麻烦,当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明:

      【案例 】使用transient关键字定制序列化和反序列化操作

  1. package IO;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. /**
  9. * 序列化和反序列化的操作
  10. * */
  11. public class serDemo {
  12. public static void main(String[] args) throws Exception {
  13. ser(); // 序列化
  14. dser(); // 反序列话
  15. }
  16. public static void ser() throws Exception {
  17. File file = new File("d:" + File.separator + "hello.txt");
  18. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
  19. file));
  20. out.writeObject(new Person1("rollen", 20));
  21. out.close();
  22. }
  23. public static void dser() throws Exception {
  24. File file = new File("d:" + File.separator + "hello.txt");
  25. ObjectInputStream input = new ObjectInputStream(new FileInputStream(
  26. file));
  27. Object obj = input.readObject();
  28. input.close();
  29. System.out.println(obj);
  30. }
  31. }
  32. class Person1 implements Serializable {
  33. public Person1() {
  34. }
  35. public Person1(String name, int age) {
  36. this.name = name;
  37. this.age = age;
  38. }
  39. @Override
  40. public String toString() {
  41. return "姓名:" + name + "  年龄:" + age;
  42. }
  43. // 注意这里
  44. private transient String name;
  45. private int age;
  46. }

【运行结果】:

       姓名:null 年龄:20

【案例 】序列化一组对象

对象输出时只提供了一个对象的输出操作(writeObject(Object obj)),并没有提供多个对象的输出,所以如果现在要同时序列化多个对象,就可以使用对象数组进行操作,因为数组属于引用数据类型,所以可以直接使用Object类型进行接收,如图12-32所示。

 (点击查看大图)图12-32 
序列化一组对象

  1. package org.lxh.demo12.serdemo;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9. public class SerDemo05 {
  10. public static void main(String[] args)
  11. throws Exception {
  12. Person per[] = { new Person("张三",
  13. 30), new Person("李四", 31),
  14. new Person("王五", 32) };
  15. // 定义对象数组
  16. ser(per);
  17. // 序列化对象数组
  18. Object o[] = dser();
  19. // 读取被序列化的对象数组
  20. for (int i = 0; i < o.length; i++) {
  21. Person p = (Person) o[i];
  22. System.out.println(p);
  23. }
  24. }
  25. public static void ser(Object obj[])
  26. throws Exception {
  27. File f = new File("D:" + File.
  28. separator + "test.txt");
  29. ObjectOutputStream oos = null;
  30. OutputStream out = new FileOutputStream(f);
  31. // 文件输出流
  32. oos = new ObjectOutputStream(out);
  33. // 为对象输出流实例化
  34. oos.writeObject(obj);
  35. // 保存对象数组到文件
  36. oos.close();
  37. // 关闭输出
  38. }
  39. public static Object[] dser() throws Exception {
  40. File f = new File("D:" + File.separator + "test.txt");
  41. ObjectInputStream ois = null;
  42. InputStream input = new FileInputStream(f);
  43. // 文件输入流
  44. ois = new ObjectInputStream(input);
  45. // 为对象输出流实例化
  46. Object obj[] = (Object[]) ois.readObject();
  47. // 读取对象数组
  48. ois.close();
  49. // 关闭输出
  50. return obj;
  51. }
  52. }

程序运行结果:

    1. 姓名:张三;年龄:30
    2. 姓名:李四;年龄:31
    3. 姓名:王五;年龄:32

IO流的更多相关文章

  1. Java基础之IO流

    很长时间都没有更新了,最近在补充JavaSE的一些细节部分 关于IO流的一些总结 首先要介绍的是File类,File类用于对文件和目录的一些操作 1.创建文件CreateNewFile() 2.对文件 ...

  2. Java中IO流,输入输出流概述与总结

    总结的很粗糙,以后时间富裕了好好修改一下. 1:Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都是抽象类InputStream(字节输入 ...

  3. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  4. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  5. IO流总结

    IO流的作用:用于设备和设备之间的数据传输. IO流的概念:流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象. IO流的分类:  按照操作数据的类型分为两种: 字节流和字符流.  按 ...

  6. Java IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  7. 揭开Java IO流中的flush()的神秘面纱

    大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...

  8. java IO流 之 其他流

    一.内存操作流(ByteArrayInputStream.ByteArrayOutputStream) (一).   public class ByteArrayInputStream extends ...

  9. java IO流 之 字符流

    字符是我们能读懂的一些文字和符号,但在计算机中存储的却是我们看不懂的byte 字节,那这就存在关于字符编码解码的问题.所以在学习Io流的字符流前我们先了解些关于编码问题. 一.字符集与字符编码 1.什 ...

随机推荐

  1. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  2. 数据库表结构设计方法及原则(li)

    数据库设计的三大范式:为了建立冗余较小.结构合理的数据库,设计数据库时必须遵循一定的规则.在关系型数据库中这种规则就称为范式.范式是符合某一种设计要求的总结.要想设计一个结构合理的关系型数据库,必须满 ...

  3. 2015年软件测试STATE报告

    STATE OF TESTING 2015 Report 测试职业的地理位置分配 大部分有5年以上工作经验 大部分是Test Leader   测试工程师角色   测试工程师怎么工作的? 测试中的软件 ...

  4. swift 如何在IOS应用图标上添加消息数

    在应用图标右上角添加消息数提醒,可以很方便的告知用户该应用中有无新消息需要处理.下面用xcode 7.3.1来简要说明一下如何用swift语言进行此功能的实现. 1.修改 AppDelegate.sw ...

  5. Webmin 安装 (centos7 rpm 方式)

    网上有很多此类的教程,大多都很老了.这里记录下自己安装Webmin的过程. # 系统准备 > yum -y install perl perl-Net-SSLeay openssl perl-I ...

  6. 我的屌丝giser成长记-研一篇(下)

    研一生活的下学期开始,课程就比较少了,加上选修课,4门课而已,总体还是比较轻松的,让我有更过充裕时间来做自己的事情以及导师的项目.开始导师的一个新的webgis开发项目,叫做三峡库区事故型水环境污染风 ...

  7. 解决mysql卸载后无法从新安装,卡在最后一步的问题

    mysql服务出现问题往往是最麻烦的,往往需要重装,而重装很多人卸不干净残留文件,更加装不上.在下就遇到这个问题.重装mysql到最后一步时卡在了最后一步的第二条上 解决办法就是卸载后删注册表+删数据 ...

  8. JAVA-堆区,栈区,方法区。

    转载:http://blog.csdn.net/wangxin1982314/article/details/50293241 堆区: 村线程操纵的数据(对象形式存放) 1 存储的全部是对象,每个对象 ...

  9. JokeClient-Swift 仿写学习

    required init?(coder aDecoder: NSCoder) 可失败构造器 在init关键字后面添加问号(init?). 可失败构造器会创建一个类型为自身类型的可选类型的对象.你通过 ...

  10. React-Native坑:Invariant Violation:Application 项目名 has not been registered.

    前言 在学习一门新技术的你也许有跟我一样的困惑,照着书上或者视频上的敲了.但是就是有各种问题没有出来自己想要的结果.我会将自己在这个过程中遇到的坑都记录下来,不一定全覆盖,但希望这些文章可以解决你的问 ...