javaIo流小记

JavaIO流总结

一、File常用方法

创建文件目录

1、创建file对象,File file=new File(“目录”);

2、通过file对象创建目录file.mkdir();返回boolean是否创建成功

创建文件

3、创建file对象,File file=new File(“文件名”);

4、通过file对象创建文件file.creatNewFile();返回boolean是否创建成功

判断文件是否存在并删除

5、创建file对象,File file=new File(“文件名”);

6、通过file对象判断file是否存在file.exists(),返回boolean是否存在

7、存在的话,通过file对象删除文件file.delete();返回boolean是否删除成功

判断目录是否存在并删除

8、创建file对象,File file=new File(“目录”);

9、通过file对象判断file是否存在file.exists(),返回boolean是否存在

10、存在的话,通过file对象删除目录file.delete();返回boolean是否删除成功

遍历目录

11、创建file对象,File file=new File(“目录”);

12、通过file获取所有的目录对象file.listFiles();返回File数组

13、遍历File数组

14、判断当前文件对象是不是文件:file.isDirectory(),返回boolean

(fille对象直接打印调用toString返回文件目录)

15、eg:递归打印所有的目录

public static void main(String[]args) throws IOException {

                   Filefile=new File("E:\\java1234.com\\二");

                   getFileList(file);

         }

        

         publicstatic void getFileList(File file){

                   if(file!=null){

                            if(file.isDirectory()){

                                     System.out.println(file);

                                     File[]files=file.listFiles();

                                     if(files.length>0){

                                               for(inti=0;i<files.length;i++){

                                                        getFileList(files[i]);

                                               }

                                     }

                            }else{

                                     System.out.println(file);

                            }

                   }

         }

二、inputStream常用方法

注意注意:FileInputStream默认的编码是文件的编码,所以在存文件时得注意要存utf-8,不然会乱码

把文件流读入内存

1、  创建file对象Filefile=new File(“文件名”);

2、  创建inputStream对象 InputStreaminputStream=new FileInputStream(file);

一次一批读取

(1)读取1024字节,输出读到的字节

1、创建存储字节数组byte b[]=newbyte[1024];

2、把文件流读取到数组中intlength=inputStream.read(b)返回读取到的字节数

3、关闭输入流inputStream.close();

4、打印读取的内容:System.out.println(“读取的内容是:”+newString(b,0,length));把读取到的字节数转换为String对象

(2)读取file有效字节的长度并输出

1、获取file对象的字节数的长度intfileLength=(int)file.length();并向下转型成int

2、创建存储字节数组byte b[]=newbyte[fileLength];

3、把文件流读取到数组中intlength=inputStream.read(b)返回读取到的字节数

4、关闭输入流inputStream.close();

5、打印读取的内容:System.out.println(“读取的内容是:”+newString(b));把读取到的字节数转换为String对象

一个字节一个字节读取

1、获取file对象的字节数的长度intfileLength=(int)file.length();并向下转型成int

2、创建存储字节数组byte b[]=newbyte[fileLength];

3、定义读取到的字节索引intindex=0;定义每个被读取到的字节变量int temp=0;

4、一个字节一个字节读取while((temp=inputStream.read())!=-1){

                                                                 b[index++]=(byte)temp;

                                                        }

4、关闭输入流inputStream.close();

5、打印读取的内容:System.out.println(“读取的内容是:”+newString(b));把读取到的字节数转换为String对象

三、outputStream常用方法

把内存对象读入硬盘

1、  创建file对象File file=new File(“文件名”);

2、  创建outputStream对象OutputStream outputStream=new FileOutputStream(file);、

new FileOutputStream(file,true);同一个文件写入时会追加内容

1、创建-需要写入的字符串对象Stringstr=”你好,我好,大家好”;

2、把字符串转换为字节数组byte[]b=str.getBytes();

3、读取字节数组到文件中outPutStream.write(b);

4、关闭输出流outputStream.close();

四、bufferedOutputStream和bufferedInputStream

建立了缓冲区,读取速度加快

1、创建输入流BufferedInputStreambufferInputStream=new BufferedInputStream(new FileInputStream(“文件名”));

1、创建输入流BufferedOutputStreambufferOutputStream=new BufferedOutputStream(new FileOutputStream(“文件名”));

2、一个字节一个字节读取同时写入intb=0;

while((b= bufferInputStream.read())!=-1){

                   bufferOutputStream.write(b);

}

bufferInputStream.close();

bufferOutputStream.close();

 

五、Reader常用方法

把文件流读入内存

1、  创建file对象Filefile=new File(“文件名”);

2、  创建Reader对象 Reader reader=newFileReader (file);

一次一批读取操作的是字符(和inputstream字节读取有所不同)

1、创建存储字节数组char c[]=newchar[1024];

2、把文件流读取到数组中intlength= reader.read(c)返回读取到的字符数

3、关闭输入流reader.close();

4、打印读取的内容:System.out.println(“读取的内容是:”+newString(c,0,length));把读取到的字节数转换为String对象

一个字节一个字节读取

1、  创建file对象File file=new File(“文件名”);

2、  创建Reader对象 Reader reader=new FileReader (file);

3、创建存储字节数组char c[]=newchar[1024];

4、int index=0;inttemp=0; while((temp=reader.read())!=-1){

                                               c[index++]=(char)temp;

}

5、关闭输入流reader.close();

6、打印读取的内容:System.out.println(“读取的内容是:”+newString(c,0, index));把读取到的字节数转换为String对象

六、Writer常用方法

把文件流读入内存

1、创建file对象Filefile=new File(“文件名”);

2、创建Reader对象 Writer out=newFileWriter (file); Writer out=new FileWriter (file,true);后面添加

3、写入out.write(“爱我中华”);

4、out.close();

猜你喜欢

转载自blog.csdn.net/fly_grass_fish/article/details/80951926