java基础笔记(12)递归,文件输入输出流与复制

1.递归: 自己方法调用自己
形成了隐式循环
必须要存在出口, StackOverFlowError ,不然会栈内存溢出
思想: 将复杂的问题简单化, 知道简单到直接可以得到结果为止
递归比较耗费内存, 所以适当使用,在特定的场景使用
斐波那切数列

//假定一对大兔子每月能生一对小兔子,且每对新生的小兔子经过一个月可以长成一对大兔子,具备繁殖能力,如果不发生死亡,且每次均生下一雌一雄,问10个月后可以有多少兔子
public class Test {
    
    
	public static void main(String[] args) {
    
    
		int num = rabbit(10);
		System.out.println(num);
	}
	public static int rabbit(int month) {
    
    
		if(month==1 || month==2) {
    
    
			return 1;
		}
		return rabbit(month-1)+rabbit(month-2);
	}
}

在这里插入图片描述

2.File
File(String)
File(String,String)
File(File ,String)
createNewFile 创建文件
mkdirs 创建多级目录
delete() 删除文件, 文件被占用着的时候就不能删除成功
过滤器
只保留 文件, 过滤掉文件夹

import java.io.File;
import java.io.FilenameFilter;
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		File file = new File("C:\\Users\\18434\\Desktop\\资料");
		String[] list = file.list(new MyFileNameFilter());
		for (String string : list) {
    
    
			System.out.println(string);
		}
	}
}
class MyFileNameFilter implements FilenameFilter{
    
    
	@Override
	public boolean accept(File dir, String name) {
    
    
		//System.out.println("====dir===="+dir);//file 对象路径
		//System.out.println("====name===="+name);// 文件夹下面的所有文件的名字		
		File file = new File(dir, name);
		if(file.isDirectory()){
    
    
			return true;
		}		
		//保留后缀名为txt的
		 if(name.endsWith(".txt")){
    
    
			return true;
		}		
		return false;// return false 被过滤掉
					//         true 保留 不被过滤掉
	}	
}

在这里插入图片描述

3.文件字节输入流
1.创建文件字节输入流
2.创建对象
3.将数据读取到数组中
read方法的作用, 读取字节,并且指针向后移动
当读取到文件结尾以后,再次读取读取到的是-1 , -1是文件结尾的标志

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Test {
    
    
	public static void main(String[] args) {
    
    
		// 创建文件字节输入流
				File file = new File("a.txt");				
				try {
    
    
					// 创建对象
					FileInputStream fis = new FileInputStream(file);
					FileInputStream fis2 = new FileInputStream("a.txt");					
					// 跳过两个字节
					fis2.skip(2);
					// 剩余可用字节个数
					int num = fis2.available();
					System.out.println("可用字节个数"+num);					
					// read方法的作用, 读取字节,并且指针向后移动
					int i = fis2.read();
					System.out.println(i);
					int i2 = fis2.read();
					System.out.println(i2);	
					byte[] bs = new byte[4];
					// read的方法的返回值代表的是本次读取有效元素的个数
					int num2 = fis2.read(bs);// 将数据读取到数组中
					System.out.println(Arrays.toString(bs));
					System.out.println(num2);//2 
					//当读取到文件结尾以后,再次读取读取到的是-1 , -1是文件结尾的标志
					int num3 = fis2.read(bs);
					System.out.println(num3);
					System.out.println(Arrays.toString(bs));					
					String str = new String(bs, 0, num3);
					System.out.println(str);					
					byte[] bs2 = new byte[4];
					int num4 = fis2.read(bs2, 2, 2);//off偏移量 偏移的数组的下标,读取的位置不会有变化      len 长度
						//  offset+len<=数组.lenth
					System.out.println(Arrays.toString(bs2));
					System.out.println(num4);
					// 最常用的是第二个
				} catch (FileNotFoundException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	}
}

文件a.txt的内容
在这里插入图片描述
在这里插入图片描述

4.FileInputStream字节输入流的使用

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		FileInputStream fis =null;
		try {
    
    
			fis = new FileInputStream("a.txt");
			byte[] bs = new byte[10];
			int len = 0;			
			while((len = fis.read(bs))!=-1){
    
    
				String str = new String(bs,0,len);
				System.out.println(str);
			}			
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			if(fis!=null){
    
    
				try {
    
    
					fis.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

在这里插入图片描述

5.FileOutputStream字节输出流的使用

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		FileOutputStream fos = null;
		try {
    
    
			// 输出流 连接的文件不存在的情况下可以自动创建
			//fos = new FileOutputStream("b.txt");
			fos  = new FileOutputStream("b.txt", true);// 以追加方式写入内容
			// 一次只能写一个
			fos.write(99);
			fos.write(100);
			// 一次会写一个数组的数据
			byte[] bs = {
    
    97,98,99,100};
			fos.write(bs);			
			// 写入数组中指定位置的部分进入文件
			fos.write(bs, 1, 2);			
	        // 写的时候 第三个比较常用			
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			if(fos!=null){
    
    
				try {
    
    
					fos.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

在这里插入图片描述
b.txt输出,追加字符

6.字节输入流和输出流复制文件的使用
1.源文件
2.目标文件
3.创建输入输出流对象
4.读
5.写
6.关流

扫描二维码关注公众号,回复: 12299850 查看本文章
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test4 {
    
    
	public static void main(String[] args) {
    
    
		//1.源文件
				File src=new File("a.txt");
				//2.目标文件
				File dest= new File("c.txt");
				//3.创建输入输出流对象
				FileInputStream fis =null;
				FileOutputStream fos =null;
				try {
    
    
					 fis = new FileInputStream(src);
					 fos = new FileOutputStream(dest);
					//4.读		
						byte[] bs = new byte[10];
						int len = 0;
						while((len = fis.read(bs))!=-1){
    
    
							// 5.写到c.txt
							fos.write(bs, 0, len);
						}
						System.out.println("复制完成");
				} catch (FileNotFoundException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
    
    
					//6.关流 
					if(fos!=null){
    
    
						try {
    
    
							fos.close();
						} catch (IOException e) {
    
    
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if(fis!=null){
    
    
						try {
    
    
							fis.close();//关流
						} catch (IOException e) {
    
    
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}				
	}
}

在这里插入图片描述
a.txt文件
在这里插入图片描述
c.txt文件

7.FileReader字符输入流

import java.io.FileReader;
import java.io.IOException;
public class Test {
    
    
	public static void main(String[] args) throws IOException {
    
    
		FileReader reader = new FileReader("a.txt");
		int num = reader.read();
		System.out.println(num);		
		char[] cs = new char[10];
		int num2 = reader.read(cs);
		System.out.println(cs);
		System.out.println(num2);		
		int num3 = reader.read(cs);
		System.out.println(Arrays.toString(cs));
		System.out.println(num3);		
		char[] cs = new char[10];
		int len = 0;
		while((len=reader.read(cs))!=-1){
    
    
			// 将字符数组中的内容转成字符串
			String str = new String(cs,0,len);
			System.out.println(str);
		}		
		if(reader!=null){
    
    
			reader.close();
		}		
	}	
}

8.FileWriter字符输出流

import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
    
    
	public static void main(String[] args) throws IOException {
    
    
		FileWriter writer = new FileWriter("c.txt");
		char[] cs = {
    
    'a','b','c','d'};
		writer.write(cs);
		//writer.flush();// 强制刷新		
		writer.write(cs, 2, 2);		
		writer.write("fdsfdsfds");
		writer.write("abc", 0, 2);		
		writer.append('d');
		writer.append("aggfgf");
		writer.close();
	}
}

猜你喜欢

转载自blog.csdn.net/Echoxxxxx/article/details/112620936