java中IO类的各类操做

java的IO类操做主要包括以下几类java

 一、File类的使用。数组

 二、字节操做流:OutputStream、InputStream缓存

 三、字符操做流:Reader、Writer函数

 四、对象序列化:serializablespa

(1)File类操作系统

Java代码.net

[java] view plain copy print?orm

  1. public class File extends Object implements Serizliable Comparable<File>  对象

        从定义看,File类是Object的直接子类,同时它继承了Comparable接口能够进行数组的排序。blog

File类的操做包括文件的建立、删除、重命名、获得路径、建立时间等,如下是文件操做经常使用的函数。

 File类的操做:

(1)建立文件,注意File.separator能够解决跨操做系统的问题。

        下面的例子是一建立一个文件,若是该文件存在则删除,不然建立该文件。

Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         File file = new File("D:" + File.separator + "test.txt");   

  4.         if (file.exists()) {   

  5.             file.delete();   

  6.         } else {   

  7.             try {   

  8.                 file.createNewFile();   

  9.             } catch (IOException e) {   

  10.                 // TODO Auto-generated catch block   

  11.                 e.printStackTrace();   

  12.             }   

  13.         }   

  14.     }   

  15. }  

 (2)文件的类型函数

       file.isFile(); //判断是否是文件

       file.isDirectory();//判断是否是目录

(3)列出目录的内容

        pulbic String[] list();//列出全部文件名和目录名

        public File[] listFiles();//列出全部文件和目录

(2)字节操做流(btyle)

 (1)字节输出流OutputStream



 

Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         File file = new File("D:" + File.separator + "test.txt");//指定要操做的文件   

  4.         OutputStream out=null;//定义字节流输出对象   

  5.         try {   

  6.             //out= new FileOutputStream(file,true);//是否字节追加函数   

  7.             out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖   

  8.         } catch (FileNotFoundException e) {   

  9.             e.printStackTrace();   

  10.         }   

  11.         String  info="hello";//要输入的内容   

  12.         byte[] b=info.getBytes();//将字符转化为字节数组   

  13.         try {   

  14.             out.write(b);   

  15.         } catch (IOException e) {   

  16.             e.printStackTrace();   

  17.         }   

  18.         try {   

  19.             out.close();   

  20.         } catch (IOException e) {   

  21.             e.printStackTrace();   

  22.         }   

  23.     }   

  24. }  

 (2)字节输入流InputStream


Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         File file = new File("D:" + File.separator + "test.txt");//指定要操做的文件   

  4.         InputStream In=null;//定义字节流输入对象   

  5.         try {   

  6.             //out= new FileOutputStream(file,true);//是否字节追加函数   

  7.             In= new FileInputStream(file);//获取实际的字节流输入对象   

  8.         } catch (FileNotFoundException e) {   

  9.             e.printStackTrace();   

  10.         }   

  11.         int len=0;//输入数组长度   

  12.         byte[] b=new byte[1024];//开辟空间,读取内容   

  13.         //byte[] b=new byte[(int)file.length()];//根据文件大小开辟空间   

  14.         try {   

  15.             len=In.read(b);//读取   

  16.         } catch (IOException e1) {   

  17.             e1.printStackTrace();   

  18.         }   

  19.         try {   

  20.             In.close();   

  21.         } catch (IOException e) {   

  22.             e.printStackTrace();   

  23.         }   

  24.         System.out.println(new String(b,0,len));   

  25.     }   

  26. }

 (3)字符输出流Write

 

Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         File file = new File("D:" + File.separator + "test.txt");// 指定要操做的文件   

  4.         Writer write = null;// 定义字符输出流   

  5.         try {   

  6.             write = new FileWriter(file);   

  7.         } catch (IOException e) {   

  8.             e.printStackTrace();   

  9.         }   

  10.         String infor = "hello,heiehiehieh";   

  11.         try {   

  12.             write.write(infor);   

  13.         } catch (IOException e) {   

  14.             e.printStackTrace();   

  15.         }   

  16.         try {   

  17.             write.close();   

  18.         } catch (IOException e) {   

  19.             e.printStackTrace();   

  20.         }   

  21.     }   

  22. }  

 (4)字符输入流Reader



 

Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         File file = new File("D:" + File.separator + "test.txt");// 指定要操做的文件   

  4.         Reader read = null;// 定义字符输入流   

  5.         try {   

  6.             read = new FileReader(file);   

  7.         } catch (IOException e) {   

  8.             e.printStackTrace();   

  9.         }   

  10.         String infor = "hello,heiehiehieh";   

  11.         char[] b=new char[1024];//设置字符的长度   

  12.         try {   

  13.             int len=read.read(b);   

  14.         } catch (IOException e) {   

  15.             e.printStackTrace();   

  16.         }   

  17.         try {   

  18.             read.close();   

  19.         } catch (IOException e) {   

  20.             e.printStackTrace();   

  21.         }   

  22.     }   

  23. }  

 (5)字节流和字符流的区别(重点)

      字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。所以在输出时,字节流不调用colse()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字符流在未关闭时输出信息,则须要手动调用flush()方法。

(6)转换流:在io中还存在一类是转换流,将字节流转换为字符流,同时能够将字符流转化为字节流。

OutputStreamWriter(OutStream out):j将字节流以字符流输出。

InputStreamReader(InputStream in):将字节流以字符流输入。

(7)打印流 PrintStream

      在操做中要求输出信息时,能够采用PrintStream进行输出,它包括PrintWrite和PrintReader

 (3)对象序列化

       对象序列化是指将一个对象能够转化为二进制的byte流,能够以文件的方式进行保存。

       将对象保存在文件的操做叫作对象的序列化操做。

       将对象从文件中恢复的操做叫作反序列化操做。

一个对象若是要能序列化,它必须继承Serizliable。在实现序列化是则须要ObjectOurputStream完成,而须要反序列化时则采用ObjectInputStream。

 

 transient关键字:变量声明为Transient后,该变量不可序列化。

(4)内存流

        在项目的开发过程当中,有时但愿只产生临时文件,将信息输出的内存中,此时会用到内存流,内存流基本方法以下:

 

Java代码

  1. public class FileDemo1 {   

  2.     public static void main(String[] args) {   

  3.         String infor = "hello";   

  4.         // 全部的内容向内存中输入   

  5.         InputStream input = new ByteArrayInputStream(infor.getBytes());   

  6.         // 全部内存的内容由outputStream输出   

  7.         OutputStream out = new ByteArrayOutputStream();   

  8.         int temp = 0;   

  9.         try {   

  10.             while ((temp = input.read()) != -1) {   

  11.                 char c = Character.toUpperCase((char) temp);   

  12.                 out.write(c);//从内存中输出,全部的内容都保存在ByteArrayOutputStream中   

  13.             }   

  14.         } catch (IOException e) {   

  15.             e.printStackTrace();   

  16.         }   

  17.         try {   

  18.             input.close();   

  19.         } catch (IOException e) {   

  20.             e.printStackTrace();   

  21.         }   

  22.         try {   

  23.             out.close();   

  24.         } catch (IOException e) {   

  25.             e.printStackTrace();   

  26.         }   

  27.         System.out.println(out.toString());   

  28.     }   

  29. }  

(5)System类对IO的支持



 (6)缓存读取

相关文章
相关标签/搜索