关于输入输出流的注意事项:

关于输入输出流的注意事项:

掌握以下几点基本语法:

  1. 逐个字节的读取

a)     只读取一个字节

      InputStream in = new InputStream(“xxx”); 

Int i = In.read();    //读取一个字节,返回的是哈希值

b)    读取所有字节

InputStream in = new InputStream(“xxx”);  

Int s;

While((s=in.read)!=-1){ //如果in.read()结果为-1,表示已经读取到最后一个字节

      Sysotem.out.print(s);    //等式返回的是左边的值

}

      2.通过缓冲字节流读取数据

扫描二维码关注公众号,回复: 6820749 查看本文章

c)      byte[ ] b = new byte[ length];

int len;

  while((len=in.read(b))!=-1) {   //in.read(b):读取指定长度的字节数据到数组中

     System.out.write(b, 0, len);  //输出数组,并规定输出数组的长度范围

  }

输出的几种形式:

1.System.out.print(xxx);  输出的是字节流

2.System.out.write(xxx,….);   输出的是字符流

3.out.write(xxx);        通过输出流输出到文件夹中

文件的删除操作:

            对于单个的文件,可以用xxx.delete()方法删除,对于非空的文件夹,需要进行非空判断,只能删除空文件夹,具体参考如下方法:

 1  public static boolean del(File f) {
 2 
 3       // 对于文件夹,需要获取文件夹中所有文件
 4 
 5       if ( f.isDirectory() ) {
 6 
 7         File[] fs = f.listFiles();
 8 
 9         // 空文件夹
10 
11         if ( fs == null || fs.length == 0 ) {
12 
13            return f.delete();
14 
15         } else {
16 
17            for (File file : fs) {
18 
19               del(file); // 递归调用
20 
21            }
22 
23         }
24 
25       }
26 
27       // 对于非文件夹,可以直接删除
28 
29       return f.delete();
30 
31    }

时间有限,先写这么多

猜你喜欢

转载自www.cnblogs.com/aqiu-jiang/p/11210086.html