File类

构造方法摘要:

  • File(File parent,String child)
  • File(String pathname)
  • File(String parent,String child)
 1 import java.io.*;
 2 class FileDemo 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         File f1=new File("a.txt");
 7         File f2=new File("c:"+File.separator+"aa"+File.separator+"dd","b.txt");
 8         File f3=new File("c:"+File.separator+"aa"+File.separator+"dd");
 9         File f4=new File(f3,"b.txt");
10         System.out.println(f1);
11         System.out.println(f2);
12         System.out.println(f4);
13     }
14 }
15 /*
16 a.txt
17 c:\aa\dd\b.txt
18 c:\aa\dd\b.txt
19 */

创建方法摘要:

  • boolean createNewFile()
  • boolean mkdir()
  • boolean mkdirs() 创建多级目录

删除方法

  • boolean delete()
  • void deleteOnExit()

判断方法摘要:

  • boolean equals(Object obj)测试路径是否相等
  • boolean canExecute() 判断文件是否可执行
  • boolean canRead()
  • boolean canWrite()
  • boolean isHidden
  • boolean isFile()
  • boolean isDirectory()
  • boolean isAbsolute()
  • int compareTo(File pathname)
  • boolean exists()
  •  

获取方法摘要:

  • String getName()
  • String getPath() 获取相对路径
  • String getAbsoluteFile()获取绝对路径
  • String getParent() 返回父目录
  • String[] list()
  • String[] list(FilenameFilter filter)
 1 import java.io.*;
 2 class FileDemo3 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         File f=new File("F:\\音樂\\华语");
 7         String[]  fileNames=f.list(new FilenameFilter()
 8         {
 9             public boolean accept(File f,String name)
10             {
11                 return name.endsWith(".ape");
12             }
13         });
14         for (String fileName:fileNames)
15         {
16             System.out.println(fileName);
17         }
18         System.out.println(fileNames.length);
19     }
20 }

利用递归获取目录中的所有文件

 1 import java.io.*;
 2 class FileDemo4 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         File dir=new File("G:\\java");
 7         showDir(dir);
 8     }
 9     public static void showDir(File dir)
10     {
11         System.out.println(dir);
12         File[] files =dir.listFiles();
13         for (int x=0;x<files.length ;x++ )
14         {
15             if (files[x].isDirectory())
16                 showDir(files[x]);
17             else
18                 System.out.println(files[x]);
19         }
20     }
21 }

 

FileWriter

方法摘要

  • void write(String str,int off,int len)
  • void write(int c)
  • void write(char[] cbuf,in off,int len)
  • void close()
  • void flush()
 1 import java.io.*;
 2 class FileWriterDemo2 
 3 {
 4     public static void main(String[] args) 
 5     {   
 6         FileWriter fw=null;
 7         try
 8         {
 9             fw=new FileWriter("3.txt");
10             fw.write("adhf");
11         }
12         catch (IOException e)
13         {
14             System.out.println(e.toString());
15         }
16         finally
17         {
18             if(fw!=null)
19             fw.close();
20         }
21     }
22 }

传递一个true参数,代表不覆盖已有文件,并在已有文件末尾进行文件的续写。

 

1  fw=new FileWriter("2.txt",true);

FileReader

  • int read() 读取单个字符如果达到流的末尾返回-1
 1 import java.io.*;
 2 class FileReaderDemo3 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         FileReader fr =null;
 7         try
 8         {
 9             fr =new FileReader("ma.txt");
10             int ch = 0;
11             while ((ch=fr.read())!=-1)
12             {
13                 System.out.print((char)ch);
14             }
15         }
16         catch (IOException e)
17         {
18             e.printStackTrace();
19         }
20         finally
21         {
22             try
23             {
24                 if(fr!=null)
25                     fr.close();
26             }
27             catch (IOException e)
28             {
29                 e.printStackTrace();
30             }
31         }
32     }
33 }
  •  int read(char[] cbuf )
 1 import java.io.*;
 2 class  FileReaderDemo5
 3 {
 4     public static void main(String[] args) throws IOException
 5     {
 6         FileReader fr=new FileReader("ma.txt");//ma内容 daskfjklashahd
 7         //定义一个字符数组,用于存储读到字符
 8         char[] buf=new char[3];
 9         //read(char[])返回的是读到字符个数
10         int num=fr.read(buf);
11         System.out.println(num);
12         System.out.println(new String(buf,0,num));
13         num=fr.read(buf);
14         System.out.println(num);
15         System.out.println(new String(buf,0,num));
16         num=fr.read(buf);
17         System.out.println(num);
18         System.out.println(new String(buf,0,num));
19         num=fr.read(buf);
20         System.out.println(num);
21         System.out.println(new String(buf,0,num));
22         num=fr.read(buf);
23         System.out.println(num);
24         System.out.println(new String(buf,0,num));
25         num=fr.read(buf);
26         System.out.println(num);
27         System.out.println(new String(buf,0,num));
28         fr.close();
29     }
30 }
31 /*
32 3
33 das
34 3
35 kfj
36 3
37 kla
38 3
39 sha
40 2
41 hd
42 -1
43 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
44 ex out of range: -1
45         at java.lang.String.<init>(String.java:226)
46         at FileReaderDemo5.main(FileReaderDemo5.java:27)
47 */
 1 import java.io.*;
 2 class FileReaderDemo6
 3 {
 4     public static void main(String args[])
 5     {
 6         FileReader fr =null;
 7         try
 8         {
 9             fr=new FileReader("ma.txt");
10             char[] cbuf =new char[1024];
11             int num;
12             while ((num=fr.read(cbuf))!=-1)
13             {
14                 System.out.println(new String(cbuf,0,num));
15             }
16         }
17         catch (IOException e)
18         {
19             e.printStackTrace();
20         }
21         finally
22         {
23             try
24             {
25                 if(fr!=null)
26                     fr.close();
27             }
28             catch (IOException e)
29             {
30                 e.printStackTrace();
31             }
32         }
33     }
34 }

复制文件

import java.io.*;
class CopyDemo
{
    public static void main(String[] args) 
    {
        copy();
    }
    public static void copy()
    {
        FileWriter fw=null;
        FileReader fr=null;
        try
        {
            fw =new FileWriter("1_copy.txt");
            fr =new FileReader("1.txt");
            char[] buf=new char[1024];
            int len=0;
            while ((len=fr.read(buf))!=-1)
            {
                fw.write(buf,0,len);
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException("读写失败");
        }
        finally
        {
            
                try
                {
                    if(fr!=null)
                        fr.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            
                try
                {
                    if (fw!=null)
                        fw.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
    }
}

 

BufferedWriter

 1 import java.io.*;
 2 class BufferedWriterDemo2 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         FileWriter fw =null;
 7         BufferedWriter bw=null;
 8         try
 9         {
10             fw =new FileWriter("ma.txt");
11             bw =new BufferedWriter(fw);
12             for (int x=0;x<5 ;x++ )
13             {
14                 bw.write("abc"+x);
15                 bw.newLine();
16                 bw.flush();
17             }
18         }
19         catch (IOException e)
20         {
21             e.printStackTrace();
22         }
23         finally
24         {
25             try
26             {
27                 if (bw!=null)
28                     bw.close();
29                 
30             }
31             catch (IOException e)
32             {
33                 e.printStackTrace();
34             }
35         }
36     }

BufferedReader

import java.io.*;
class BufferedReaderDemo2 
{
    public static void main(String[] args) 
    {
        BufferedReader br=null;
        try
        {
            br =new BufferedReader(new FileReader("ma.txt"));
            String str=null;
            while ((str=br.readLine())!=null)
            {
                System.out.println(str);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(br!=null)
                    br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

复制文件

 1 /*
 2 readLine方法返回的时候只返回回车符之前的数据内容,并不返回回车符。
 3 */
 4 import java.io.*;
 5 class CopyByBufDemo
 6 {
 7     public static void main(String[] args) 
 8     {
 9         copy();
10     }
11     public static void copy()
12     {
13         BufferedWriter bufw=null;
14         BufferedReader bufr=null;
15         try
16         {
17             bufw =new BufferedWriter(new FileWriter("2_copy.txt"));
18             bufr =new BufferedReader(new FileReader("2.txt"));
19             char[] buf=new char[1024];
20             String str=null;
21             while ((str=bufr.readLine())!=null)
22             {
23                 bufw.write(str);
24                 bufw.newLine();
25                 bufw.flush();
26             }
27         }
28         catch (IOException e)
29         {
30             throw new RuntimeException("读写失败");
31         }
32         finally
33         {
34             
35                 try
36                 {
37                     if(bufr!=null)
38                         bufr.close();
39                 }
40                 catch (IOException e)
41                 {
42                     e.printStackTrace();
43                 }
44             
45                 try
46                 {
47                     if (bufw!=null)
48                         bufw.close();
49                 }
50                 catch (IOException e)
51                 {
52                     e.printStackTrace();
53                 }
54         }
55     }
56 }

 

/*
字符流
FileReader 
FileWriter
字节流
OutputStream InputStream

*/
import java.io.*;
class  FileStream
{
    public static void main(String[] args) throws IOException
    {
        writeFile();
        readFile_1();
        readFile_2();
    }
    public static void writeFile() throws IOException
    {
        FileOutputStream fos=new FileOutputStream("3.txt");
        fos.write("djfk吗".getBytes());
        fos.close();
    }
    public static void readFile_1() throws IOException
    {
        FileInputStream fis=new FileInputStream("3.txt");
        int ch=0;
        while ((ch=fis.read())!=-1)
        {
            System.out.println((char)ch);
        }
    }
    public static void readFile_2() throws IOException
    {
        FileInputStream fis=new FileInputStream("3.txt");
        byte[] buf=new byte[1024];
        int num=0;
        //read(byte[])返回读到字节的个数
        while ((num=fis.read(buf))!=-1)
        {
            System.out.println(new String(buf,0,num));
        }
    }
}

 

posted on 2012-06-09 08:25  Lincon Ma  阅读(188)  评论(0编辑  收藏  举报