java-io

来源:互联网 发布:淘宝网皮衣 编辑:程序博客网 时间:2024/05/01 21:05

java的IO类操作主要包括如下几类

 1、File类的使用。

 2、字节操作流:OutputStream、InputStream

 3、字符操作流:Reader、Writer

 4、对象序列化:serializable

(1)File类

Java代码
[java] view plaincopyprint?
  1. public class File extends Object implements Serizliable Comparable<File>  

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

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)缓存读取
0 0
原创粉丝点击