黑马程序员之java学习笔记14

1.递归(recursion):递归就是无限调用自己但必须有一个出口,以免出现死循环。

2.递归的两个例子:阶乘和斐波那契数列。

  如:阶乘的实现:

 public class JieCheng{

      //循环方式

      Public int compute(int number){

        Int result = 1;

        For(int I =number; i >0;i--){

           Result = result*i;

}

//递归方式

      Public int compute(int number){

         If(number==1) return number;

         Else return number*compute(number-1);

  }

      Public static void main(String[] args){

         JieCheng jc = new JieCheng();

         System.out.println(Jc.compute(10));

}   

  

 

斐波那契数列实现:

Public class Febonacci{

   Public int compute(int number){

     If(number==1||number==2){

       Return number;}

      Else return compute(number-1)+compute(number-2);

   Public static void main(String[] args){

    Febonacci fb= New Febonacci();

    Fb.compute(10);

3.使用递归删除目录中的所有东西:

Public class DeleteFile{

Public void deleteAll(File file){

If(file.isFile()||file.list().length==0)  file.delete;

Else{

File[] files = file.listFiles();

For(File f: files){

deleteAll(f);

f.delete();

}

 Public static void main(String[] args){

 deleteAll(new File(“c:/abc/dce”));

} }

 

 

 

3. 输入输出流的区分的标准:我们自己的程序或内存。数据从内存-à磁盘,就是输出流;数据从磁盘à内存或程序,就是输入流。

4. 输入输出流:从流结构上分为字节流( Byte Stream以字节为单位处理,可以处理二进制数据)和字符流(Character Stream以字符串为单位处理,可处理文本文件)。字节流是基础,字符流实际上是通过字节流实现的,字符流对字节流进行了封装。

5.字节流对应的是抽象类InputStreamOutputStream,字符流对应的是抽象类ReaderWriter.

6. 字节流读写8位的二进制字节,主要用于操作图片或声音的二进制数据。

  字符流读写16位的二进制字符,主要用于操作文本文件。

7. java I/O 3种分类:输入流输出流;字节流字符流;节点流过滤流。

8. 读数据的步骤:

  开流à定义相关information-àread information-à关流

9. inputStream类中的三种read方法:read(),read(byte[] b),read(byte[] b, int off, int length);其中read()方法是抽象的。Read方法返回是实际读取的字节个数。

10.从文件中读取关键代码:

   FileInputStream fps = new FileInputStream(“c:/abc/test.txt”);

   byte[] b = new byte[300];

   Int length = 0;

   While(-1!=length=fps.read(b,0,300){//length是返回的实际长度

    String str = new String(b,0.length);//将字节数组b,从位置0开始的length个字节转化为字符串。

    System.out.println(str);

    }

11. InputSteam抽象类的主要子类-àFileInputStream, ByteArrayInputStream,FilterInputStream.

  FilterInputStream的主要子类-àBufferInputStream,DataInputStream. 相应的OutputStream抽象类的情况和InputStream一样,都有相应的那些子类。DataInputStream过滤类能直接输入java基本数据类型,如intbyte,String,而不仅仅是二进制数据。

12. 过滤类本质是对各种节点流的一种包装,赋予流一些新的功能。过滤类不直接操作文件。过滤类也可封装过滤类。如,DataInputStream过滤类能封装BufferInputStream过滤类。DataInputStream dis = new DataInputStream(new BufferInputStream(new FileInputStream(“c:/test.txt”);这样就上dis输入流就拥有缓存,直接输入java基本数据类型的功能。

13. 当使用BufferInputStream向程序或内存中输入东西时,要想获得在缓存中的东西,有两种方式:1)使用flush()方法,会强制清空流;2)使用close()方法,因为close方法在关闭流的时候,会首先清空流中的东西。在过滤流中调用了close()方法,也会关闭被包装流。

14.使用字节输出流,文件作为目的的关键代码:

   FileOutputStream fos = new FileOutputStream(“c:/text.txt”);

   String str = “hi”;//字符串

   Byte[] b = str.getBytes();//将字符串转化为字节数组。

   fos.write(b);//将字节数组写入流中,就会写入到对应的文件中。

   使用上述方式向文件中写入东西,会覆盖之前文件中的内容,但如果这样写:

   FileOutputStream fos = new FileOutputStream(“c:/text.txt”, true);就会将下次要写入的内容追加到上次写入内容的后面。

16. ByteArrayInputStream是一个输入过滤类,将ByteArray数组作为源。

   ByteArrayInputStream的两种构造方法:

1)ByteArrayInputStream(Byte[] array);

2)ByteArrayInputStream(Byte[] array, int start, int length);

使用ByteArrayInputStream关键代码:

String str = “abcde”;

Byte[] b = str.getBytes[];

ByteArrayInputStream bais = new ByteArrayInputStream(b);

ByteArrayInputStream bis = new ByteArrayInputStream(b,0,4);

17. 三种基本的读方法:

   1) abstract int read() 读取一个字节数据,并返回一个读取的这个数据。返回-1,表示读到了输入流的末尾。

   2int read(byte[] b),将一个数据读入到字节数组中,并返回实际读取的字节数。返回-1,表示读到了输入流的末尾。

   3) int read(byte[] b, int start, int length),将从位置start开始,最多读取长度length的字节,读入byte数组中,并返回实际读取的字节数。返回-1,表示读到了输入流的末尾。

 18. 一个输出流调用toByteArray() 的作用是将输出流里的内容转化为字节数组。

 19. 如何将字节数组输出流(ByteArrayInputStream)里的内容写到文件中:

    ByteArrayInputStream bis = new ByteArrayInputStream();

    String str = “helloworld”;

    Byte[] b = str.getBytes();

    bis.wirte(b);

    FileOutputStream fos = new FileOutputStream(“c:/text.txt”);

    bis.writeTo(fos);//将字节数组输出流中的内容写进文件。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值