博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
黑马程序员 Java输入\输出
阅读量:4511 次
发布时间:2019-06-08

本文共 10804 字,大约阅读时间需要 36 分钟。

---------------------- android培训、java培训、期待与您交流! ----------------------

 

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.txt17 c:\aa\dd\b.txt18 c:\aa\dd\b.txt19 */

创建方法摘要:

  • 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

 

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         finally17         {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         finally21         {22             try23             {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 333 das34 335 kfj36 337 kla38 339 sha40 241 hd42 -143 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind44 ex out of range: -145         at java.lang.String.
(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         finally22         {23             try24             {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         finally24         {25             try26             {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         try16         {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         finally33         {34             35                 try36                 {37                     if(bufr!=null)38                         bufr.close();39                 }40                 catch (IOException e)41                 {42                     e.printStackTrace();43                 }44             45                 try46                 {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));        }    }}

 

---------------------- android培训、java培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima

转载于:https://www.cnblogs.com/malinkang1989/archive/2012/06/09/2542807.html

你可能感兴趣的文章
Day3:Spring-JDBC、事务管理
查看>>
模块的四种形式
查看>>
教你如何培养幽默感
查看>>
asp.net的一个简单简历缓存方法
查看>>
loj 1185(bfs)
查看>>
全排列-按从大到小-time limited
查看>>
减肥中,做个 体重三围 测量软件
查看>>
windows下命令行修改系统时间;修改系统时间的软件
查看>>
[LeetCode] 384. Shuffle an Array 数组洗牌
查看>>
最大公约数
查看>>
序列化和反序列化
查看>>
Mac上Chrome浏览器跨域解决方案
查看>>
Sublime Text 3 全程详细图文原创教程(持续更新中。。。)
查看>>
java输出重定向
查看>>
load data with matlab
查看>>
ctypes调用dll的参数问题
查看>>
微信支付接口的调用(转)
查看>>
XSS攻击
查看>>
浅谈Sql各种join的用法
查看>>
Durid数据库连接池配置(不使用框架)
查看>>