recursion&file&Buffered&I/O流

递归

描述:

递归做为一种算法在程序设计语言中广泛应用。 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解。递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。递归的能力在于用有限的语句来定义对象的无限集合。一般来说,递归需要有边界条件、递归前进段和递归返回段。当边界条件不满足时,递归前进;当边界条件满足时,递归返回。

递归算法解题相对常用的算法如普通循环等,运行效率较低。因此,应该尽量避免使用递归,除非没有更好的算法或者某种特定情况,递归更为适合的时候。在递归调用的过程当中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等。

Java中使用递归注意:构造方法不能递归使用。

  • 代码实例:
    • 斐波那契
      /*for example-Fibonacci*/
      /*思考如何用递归实现斐波那契数列的存储和显示。*/
      public static int fib(int n) {
      		if (n == 1 || n == 2) {
      			return 1;
      		} else {
      			return fib(n - 1) + fib(n - 2);
      		}
      	}
      
      /*明显的优势是将有用的数据存储在数组里面。*/
      arr[0] = 1;
      arr[1] = 1;
      for (int x = 2; x < arr.length; x++) {
      	arr[x] = arr[x - 2] + arr[x - 1];			
      }
      
      /*不断替换a,b对应的数据,数据不保留。*/
      int a = 1;
      int b = 1;
      for (int x = 0; x < 18; x++) {
      	// 每运行一次将a,b对应的数据刷新一遍。
      	int temp = a;
      	a = b;
      	b = temp + b;
      	System.out.println(a);
      }
      

IO流

描述:

按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流…

  • 分类:

    • 流向
      可以这样理解,流的输入还是输出性是相对于内存来描述的。相对于内存是进入内存,则为输入流–读取数据;相对于内存是流出内存,则是输出流–写出数据–写入到外部存储设备。
    • 数据类型
      字节流:字节输入流,字节输出流。
      字符流:字符输入流,字符输出流。

    建议使用字节流进行文件的输入和输出。

  • FileOutputStream写出数据

    使用中,主函数必须public static void main(String[] args) throws IOException/try…catch…

    /*for example*/
    		FileOutputStream fos = new FileOutputStream("fos.txt");			
    		fos.write("hello,world.".getBytes());			
    		fos.close();
    
  • FileInputStream读取数据

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

    使用中,主函数必须public static void main(String[] args) throws IOException/try…catch…

    /*for example*/
    		FileInputStream fis = new FileInputStream("fos.txt");
    		//way_01:	
    		int by = 0;
    		while((by=fis.read())!=-1) {
    			System.out.print((char)by);
    		}
    
    		//way_02
    		char by = 0;		
    		while ((by = (char) fis.read()) != -1) {
    			System.out.print(by);
    		}
    	
    		//way_03
    			byte[] bys = new byte[1024];
    			int len = 0;
    			while((len=fis.read(bys))!=-1) {
    				System.out.print(new String(bys,0,len));
    			}		
    		fos.close();
    
  • 字节缓冲区流

    • BufferedOutputStream.
    • BufferedInputStream.

    	//字符缓冲区的使用方法.
    	public static void Way_One(String srcString, String destString)
    			throws IOException {
    		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    				srcString));
    		BufferedOutputStream bos = new BufferedOutputStream(
    				new FileOutputStream(destString));
    
    		byte[] bys = new byte[1024];
    		int len = 0;
    		while ((len = bis.read(bys)) != -1) {
    			bos.write(bys, 0, len);
    		}
    
    		bos.close();
    		bis.close();
    	}
    
    	
    	public static void Way_Two(String srcString, String destString)
    			throws IOException {
    		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    				srcString));
    		BufferedOutputStream bos = new BufferedOutputStream(
    				new FileOutputStream(destString));
    
    		int by = 0;
    		while ((by = bis.read()) != -1) {
    			bos.write(by);
    
    		}
    
    	fos.close();
    	fis.close();
    
    
  • 字符流

    描述:字节流对于数据的复制存储操作上并没有什么区别,但是有操作中文数据的可视化上不是特别的方便,所以就出现了转换流,转换流的作用就是把字节流转换字符流来使用。

    • 转换流就是字符
      字符流 = 字节流 + 编码表(注明编码格式.)
    • 编码表
      • 就是由字符和对应的数值组成的表。

    编码表

  • 字符串中的编码

    • IO流中的编码

      /*for example*/
      /*字符流与字节流的桥梁*/
      OutputStreamWriter
      
      	是字符的桥梁流以字节流:向其写入的字符编码成使用指定的字节charset 。 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
      
      	OutputStreamWriter(OutputStream os):默认编码,GBK
      	OutputStreamWriter(OutputStream os,String charsetName):指定编码。
      
      InputStreamReader
      
      	是从字节流到字符流的桥:它读取字节,并使用指定的charset将其解码为字符 。 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
       
      	InputStreamReader(InputStream is):默认编码,GBK.
      	InputStreamReader(InputStream is,String charsetName):指定编码.
      
      编码问题
      	存储的本质问题都是字节流,解决办法直接指定编码格式与之对应即可,或者使用写入文件的编码格式即可。
      
      public static void main(String[] args) throws IOException {
      		
      		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
      		// "osw.txt")); // 默认GBK
      		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
      		// "osw.txt"), "GBK"); // 指定GBK
      		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
      				"osw.txt"), "UTF-8"); // 指定UTF-8
      		
      	
      		osw.write("中国");		
      		osw.close();
      }
      
      public static void main(String[] args) throws IOException {
      		
      		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
      		// "osw.txt"));
      		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
      		// "osw.txt"), "GBK");
      		InputStreamReader isr = new InputStreamReader(new FileInputStream(
      				"osw.txt"), "UTF-8");
      		
      		int ch = 0;
      		while ((ch = isr.read()) != -1) {
      			System.out.print((char) ch);
      		}
      
      		/*
      		char[] chs = new char[1024];
      		int len = 0;
      		while ((len = isr.read(chs)) != -1) {
      			System.out.print(new String(chs, 0, len));
      		}
      		*/
      		
      		isr.close();
      }
      
      public static void main(String[] args) throws IOException {
      
      	InputStreamReader isr = new InputStreamReader(new FileInputStream(
      			"a.txt"));	
      	OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
      			"b.txt"));
      	
      	/*方式1
      	int ch = 0;
      	while ((ch = isr.read()) != -1) {
      	osw.write(ch);
      	}
      	*/
      
      	/* 方式2*/
      	char[] chs = new char[1024];
      	int len = 0;
      	while ((len = isr.read(chs)) != -1) {
      		osw.write(chs, 0, len);
      		// osw.flush();
      	}
      
      	// 释放资源
      	osw.close();
      	isr.close();
      }
      
    • 字符流

      Reader
      	|--InputStreamReader
      		|--FileReader
      	|--BufferedReader
      Writer
      	|--OutputStreamWriter
      		|--FileWriter
      	|--BufferedWriter
      

IO流操作结构图解

  • IO图解

  • I/O代码实例

/*for example*/
/*字符缓冲流
 BufferedWriter:
 		public void newLine():根据系统来决定换行符
 BufferedReader:
 		public String readLine():一次读取一行数据
 		包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回null.
 */

猜你喜欢

转载自blog.csdn.net/CS_GaoMing/article/details/83793163