`
huanglz19871030
  • 浏览: 241451 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

文件、流总结

 
阅读更多

1、File类:File类位于java.io包中,我们可以通过File对象的方法创建,删除,修改和查找文件。 
                例:创建文件:java.io.File file = new java.io.File(path); 
                      删除文件:file.delete(); 
1)文件系统规则: 
   1、文件的名字包括文件的路径名和扩展名。 
   2、同一目录下不会存在同名的目录和文件 
   3、相对路径是不以路径分隔符开头的文件路径,绝对路径是以根目录开头的路径。 
   例:文件绝对路径:String path = "D:\\a\\b\\c\\d";     
         c文件相对d的路径:String path2 = "b\\c.txt"; 
         d文件相对c的路径:String path3 = "..\\d.txt"; 
   4、不同的系统中,分割符是不同的。在win系统中,目录分割符和路径分割符分别是"\"和";",而liunx系统中,目录  分割符和路径分割符分别是"/"和":"。 
2)File类的属性:public static Stirng pathSeparator:表示当前操作系统的路径分隔符。 
                     public static String separator:表示当前操作系统的名字分隔符 
3)File类的构造器:File(File parent,String child):表示在一个父文件下创建一个child文件。 
                         File(String pathName):创建一个指向pathname的文件对象。 
                         File(String parent,String child):创建一个以parent字符串为父目录名的指向child的文件对象。 
                         后两个构造器只是实例化了文件对象,但是并没有创建文件。 
4)File类的方法: boolean exists():判断文件是否存在 
                       例:if(temFile.exists()) 
                     String getName():得到文件名字 
                      例:String Name = temFile.getName(); 
                     boolean isDirectory():判断文件是否是目录 
                      例:if(temFile.isDirectory()) 
                     boolean isFile():判断文件是否是真实文件 
                      例:if(temFile.isFile()) 
                    long length():得到文件长度 
                      例:tem.length() 
                    File[] listFiles():将文件下一级的目录和真实文件作为一个数组返回 
                     例:File[] subFile = dirFile.listFiles(); 
                   static File[] listRoots():将每个逻辑驱动器作为返回文件的一个元素 
                      例:File fe = File.listRoots(); 
                   boolean mkdir():根据文件名字创建一个目录 
                   boolean mkdirs():若文件名字有多级目录,可一次创建 
                   boolean delete():删除文件 
                   boolean createNewFile():创建一个新文件 

2、递归:指一种计算方法或方法调用的规则,简单地说就是自己调用自己,所用的方法是相同的。递归算法常用于处理一些无法穷举的计算,但这些计算要有一定的规律。 
   递归的缺点:容易出现死循环,导致内容溢出。 
   例: public static void main(String[] args) { 
             String input=JOptionPane.showInputDialog("please input the number of the disks you want me move."); 
             int num=Integer.parseInt(input); 
             move(num,from,mid,to); 
      } 
     private static void move(int num, String from2, String mid2, String to2) { 
            if(num==1){ 
            System.out.println("move disk 1 from "+from2+" to "+to2); 
            }else { 
            move(num-1,from2,to2,mid2); 
            System.out.println("move disk "+num+" from "+from2+" to "+to2); 
            move(num-1,mid2,from2,to2); 
           } 
   } 


3、异常:java中的异常为java.lang包下的Exception类。 
   关键字:1、try:catch关键字常用在try catch或在try catch finally中定义异常处理块,每个try块都必须有一个catch或finally语句 
例:try{ 
           可能引发异常的块 
    } 
    catch{ 
          处理异常的代码 
    } 
    finally{ 
           有无异常都处理的语句 
    } 
   2、catch:catch关键字常用在try catch或在try catch finally中定义异常处理块。 
   3、finally:用来定义在try catch finally中定义的块。 
   4、throw: 用于引发异常。throw 语句将 java.lang.Throwable 作为参数,Throwable 在调用栈中向上传播,直到被适当的 catch 块捕获。 
例:if (error){ 
          throw new IOException("error reading file"); 
      } 
   5、throws:应用于方法,以便指出方法引发了特定类型的异常。throws 关键字将逗号分隔的java.lang.Throwables 列表作为参数。如果要在 try-catch 块中包含带 throws 子句的方法的调 用,必须提供该方法的调用者。 
例:public class MyClass{ 
           public method readFile(String filename) throws IOException{ 
           } 
     } 

4、流:输入和输出的抽象。 
   1)流的分类:按方向分为:输入流和输出流 
                  按性质分为:字节流:按最小单位读取的流,直接连接到输入流的源。 
                                    缓冲流:包装基础流,提高缓冲功能。 
                                    对象流:对象流的读取就是java对象序列化(只要是对象,就要进行序列化)。 
                                    基与具体数据类型的流:从流中读取指定数据类型的数据。 
                                    除字节流外,其它流均不可直接连接到输入流的源。 
   2)InputStream定义的方法:int avialavle():可读取的有效字节长度。 
                                            例:byte[] array = new byte[ins.avialavle()]; 
                                            void close():关闭流对象。 
                                            例:ins.close(); 
                                            int read():从流中读取数据,若流已到结尾,则返回-1。 
                                            例:int read(byte[] b); 
3)OutputStream定义的方法:void close():关闭流。 
                                            例:out.close(); 
                                           void flush():将有可能还保存在内存中的数据强制输出到目标中。 
                                            例:out.flush(); 
                                           void write(byte[] b):将数组中内容输出到流中 
                                           例:out.write(i); 
                                           void write(byte[] b,int off,int len):将数组中一部分写入流中 
                                           void write(int b):向流中写入一个字节 
4)FileInputStream的构造器:FileInputStream(File file):通过文件对象做参数构造输入流对象 
                                           FileInputStream(String name):传入的字符串连接到指定文件的输入流 
                                           例:InputStream ins = new FileInputStream(fileName); 
5)FileOutputStream的构造器:FileOutputStream(File file):构造输出到指定文件的输出流 
                                              FileOutputStream(File file,boolean append):append表示输出到文件的数据是否在已有数据后 
                                              FileOutputStream(String name) 例:OutputStream out = new FileOutputStream(txtName); 
                                              FileOutputStream(String name,boolean append) 
6)BufferedInputStream的构造器: BufferedInputStream(InputStream in):创建一个默认缓冲区大小的缓冲对象 
                                                     例:BufferedInputStream bis = new BufferedInputStream(ins); 
                                                     BufferedInputStream(InputStream in,int size):指定缓冲区大小,构造缓冲输入函数 
7)BufferedOutputStream的构造器:BufferedOutputStream(OutputStream out):使用默认缓冲区构造缓冲输入流 
                                                      例:BufferedOutputStream bos = new BufferedOutputStream(out); 
                                                      BufferedOutputStream(OutputStream out,int size):指定缓冲大小

 

转载自:http://wjingwen.iteye.com/blog/1133535

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics