1.一般而言,兔子在出生两个月后,就有繁殖能力,一对兔子每个月能生出一对小兔子来。如果所有兔都不死,那么一年以后可以繁殖多少对兔子? 通过递归和循环来解决这个问题
提示:
我们不妨拿新出生的一对小兔子分析一下:
第一个月小兔子没有繁殖能力,所以还是一对;
两个月后,生下一对小兔民数共有两对;
三个月以后,老兔子又生下一对,因为小兔子还没有繁殖能力,所以一共是三对;
package recursion;
public class Fibonaccisequence {
/**
* 斐波那契数列
* @param n
* @return
*/
public static int fibonacci(int n)
{
if(n==0||n==1)
return 1;
return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args) {
System.out.println(fibonacci(3));
}
}
2.使用File,获得一个文件是否存在,如果不存在,则创建.如果存在,则显示这个文件的大小,路径,最后修改时间.
package ioStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class class02 {
public static void main(String[] args) {
File file=new File("D:\\新建文件夹 (2)\\3.txt");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
System.out.println("该文件的大小为:"+file.length());
System.out.println("该文件的的路径为:"+file.getAbsolutePath());
System.out.println("该文件的最后修改时间为:"+new Date(file.lastModified()));
}
}
}
3.创建一个多层目录 "f:/java/java1/java2"
package ioStream;
import java.io.File;
public class class03 {
public static void main(String[] args) {
File file=new File("d://java//java1//java2");
System.out.println(file.mkdirs());
}
}
4.File类,自学createTempFile方法的使用,在一个目录中,创建500个临时文件;
package ioStream;
import java.io.File;
import java.io.IOException;
public class class04 {
public static void main(String[] args) {
File file =new File ("D:\\新建文件夹 (2)\\liyifeng");
for(int i=0;i<500;i++)
{
try {
file.createTempFile("aaa", ".txt", file);
} catch (IOException e) {
System.out.println("文件创建失败!!!");
}
}
}
}
//二分法查找 [1,3,4,5,7,8,9,23,45]
package recursion;
public class binarysearch {
// public static int search(int[] a,int low,int high,int m){
//
// if(low>high){
// return -1;
// }
//
// int mid=(low+high)/2;
//
// if(m==a[mid]){
// return mid;
// }
// else if(m<a[mid]){
// return search(a,low,mid-1,m);
// }
// else{
// return search(a,mid+1,high,m);
// }
// }
public static int binnary(int[] a,int start,int end,int values)
{
if(start>end)
{
return -1;
}
int mid=(start+end)/2;
if(values<a[mid])
{
return binnary(a, start, mid-1, values);
}else if(a[mid]<values)
{
return binnary(a, mid+1, end, values);
}else
{
return mid;
}
}
public static void main(String[] args) {
int [] a= {1,3,6,8,9,20,33};
System.out.println(binnary(a,0,a.length-1,8));
}
}
//汉诺塔;
package ioStream;
public class TowerOfHanoi {
public static void hanoi(int n,String A,String B,String C)
{
if(n==1)
{
System.out.println("move"+n+": "+A+"-->"+C);
}else
{
hanoi(n-1,A,C,B);//首先将上面的(n-1)个盘子从A杆借助C杆移至B杆
System.out.println("move"+n+": "+A+"-->"+C);//然后将编号为n的盘子从A杆移至C杆
hanoi(n-1,B,A,C);//最后将上面的(n-1)个盘子从B杆借助A杆移至C杆
}
}
public static void main(String[] args) {
hanoi(3,"A","B","C");
}
}
5.删除一个给定的目录,这上目录不为空目录,使用递归来实现
package ioStream;
import java.io.File;
public class 文件的删除 {
/**
* 删除某个文件
* @param file
*/
public static void delete(File file)
{
if(file.isDirectory())
{ File[] files=file.listFiles();
for(int i=0;i<files.length;i++)
{
delete(files[i]);
}
}
//删除自己
System.out.println(file.getAbsolutePath());
file.delete();
}
public static void main(String[] args) {
File file =new File("D:\\2017 练习");
delete(file);
}
}
6.写一个方法,可以复制一个文件
package ioStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class filecopy {
public static void copy(File src,File target)
{
FileReader fr=null;
FileWriter fw=null;
try {
fr= new FileReader(src);
fw= new FileWriter(target);
char[] cbuf=new char[100];//缓冲区
int size=0;//每次读到的字符的数量
while ((size=fr.read(cbuf))!=-1)
{
fw.write(cbuf, 0, size);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy1(File src,File target) throws Exception
{ //1. 两个流
FileOutputStream out=null;
FileInputStream in=null;
in= new FileInputStream(src);
out =new FileOutputStream(target);
int size=0;
//2.定义一个字节数组作为缓冲
byte [] bt=new byte[1024];
//3.边读边写
while((size=in.read(bt))!=-1)
{
out.write(bt, 0, size);
}
//4.关闭在资源
out.close();
in.close();
}
public static void main(String[] args) {
try {
copy1(new File("d:/李易峰.txt"),new File("d:/new李易峰.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
7.写一个方法,可以复制一个目录,(此目录不为空)
package ioStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 文件夹的复制 {
public static void main(String[] args) throws IOException {
File file1=new File("D:/李易峰.txt");
File file2=new File("d:/nn李易峰.txt");//创建一个新目录
if(!file2.exists()){
file2.mkdirs();
}
copyDirectory(file1,file2);
}
public static void copyFile(File file1,File file2){//复制文件的方法
try(
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);//创建字符输入输出流
){
byte [] data=new byte[12];
int d=-1;
while((d=fis.read(data))!=-1){
fos.write(data,0,d);//将其复制到另一个文件里
}
}catch(IOException e){
e.printStackTrace();
}
}
public static void copyDirectory(File file1,File file2) throws IOException{//复制目录的方法
if(!file1.exists()){
System.out.println("该复制文件不存在");
}
else{
File [] fs=file1.listFiles();
for(File file:fs){
File copy=new File(file2,file.getName());//创建同名的文件或目录
System.out.println(copy);
if(!file.isDirectory()){
copy.createNewFile();//是文件的话,调用方法,复制文件
copyFile(file,copy);
}
else{
copy.mkdir();
copyDirectory(file,copy);//是目录的话递归下去,继续创建目录下的文件或目录
}
}
}
}
}
8.写一个方法,可以将一个非空目录中的所有文件的层次显示出来;
package ioStream;
import java.io.File;
import java.io.IOException;
/**
*
* 显示某个目录中的所有文件
*
*/
public class 遍历目录的所有文件 {
public static void show(File file)
{ //显示文件信息
System.out.println(file.getAbsolutePath());
if(file.isDirectory()) {
//获得目录下的所有文件
File[] files=file.listFiles();
for(int i=0;i<files.length;i++)
{ //形成递归调用
show(files[i]);
}
}
}
public static void main(String[] args) {
File file=new File("D:\\SHDownload");
show(file);
}
}
9.io流的分类有哪些?(简答)
流的分类方式
按方向分:(相对程序来说的)
输入流;
输出流;
按流中传输的内容分
字节流
字符流
按功能分:
节点流:直接连接某种资源的流,比如File流;
处理流:对某个流进行一个包装,以增加额外的功能;最终形成一个流链
四个顶层的流
InputStream:字节输入流;
OutputStream:字节输出流;
Reader:字符输入流;
Writer:字符输出流;
10.使用FileOutputStream,向"f:/output" 输出一句话"hello world",并能过FileInputStream流读出这段文字
package ioStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class class10 {
public static void main(String[] args) throws Exception {
//字符输出流
FileOutputStream out=new FileOutputStream("d:/李易峰66.txt");
String s = "hello world";
byte[] b = s.getBytes();
out.write(b);
//字符输入流
FileInputStream input=new FileInputStream("d:/李易峰66.txt");
int size=0;
byte [] c = new byte[1024];
size=input.read(c);
String s1=new String(c,0,size);
System.out.println(s1);
}
}
11.使用FileWriter,向"f:/output" 输出一句话"hello world",并能过FileReader流读出这段文字
package ioStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class class11 {
public static void main(String[] args) throws Exception {
FileWriter write=new FileWriter("d:/李易峰77.txt");
write.write("hellow word");
write.close();
FileReader read=new FileReader("d:/李易峰77.txt");
char[] b=new char[12];
int size=0;
size=read.read(b);
System.out.println(new String(b,0,size));
read.close();
}
}
13.通过Scanner读两句话,并写到一个文件中.通过PrintWriter来实现
package ioStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class class13 {
public static void main(String[] args) throws Exception {
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
String s1=scanner.next();
PrintWriter writer=new PrintWriter("d:/李易峰33.txt");
writer.println(s);
writer.println(s1);
writer.close();
}
}
14.使用BufferedWriter,向"f:/output" 输出一句话"hello world",并能过BufferedReader,流读出这段文字
package ioStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class class14 {
public static void main(String[] args) throws Exception {
BufferedWriter write=new BufferedWriter(new FileWriter("d:/李易峰22.txt"));
write.write("hello word");
write.close();
BufferedReader read=new BufferedReader(new FileReader("d:/李易峰22.txt"));
char[] b =new char[12];
int size=0;
size=read.read(b);
System.out.println(new String(b,0,size));
read.close();
}
}
15.使用DataOutputStream向文件中输出int,double,String类型的数据
package ioStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class class15 {
public static void main(String[] args) throws Exception {
DataOutputStream out =new DataOutputStream((new FileOutputStream("d:/李易峰55.txt")));
out.writeInt(666);
out.writeDouble(3.14);
out.writeUTF("李易峰");
out.close();
DataInputStream input=new DataInputStream(new FileInputStream("d:/李易峰55.txt"));
System.out.println(input.readInt());
System.out.println(input.readDouble());
System.out.println(input.readUTF());
input.close();
}
}
16.定义一个学生类 Student,有属性,Date brith,String name,int height,String degree,定义两个对象,将此对象写到文件中,并在另外一个工程中,将些内容读出.
package ioStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
class Student implements Serializable {
private String name;
private Date birth;
private String degree;
private int height;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return "Student [name=" + name + ", birth=" + sdf.format(birth) + ", degree=" + degree + ", height=" + height + "]";
}
public Student( String name, Date birth,String degree,int height) {
super();
this.birth = birth;
this.name = name;
this.height = height;
this.degree = degree;
}
public Student() {
super();
}
}
public class objectinputstream {
public static void main(String[] args) throws Exception{
write("d://b.txt");
read("d://b.txt");
}
public static void write(String file) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeInt(3332);
oos.writeBoolean(false);
oos.writeUTF("你是风儿我是沙");
String s="2000-02-19";
String s1="1998-7-6";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d=sdf.parse(s);
java.util.Date d1=sdf.parse(s1);
List<Student> list = new ArrayList<Student>();
list.add(new Student("狄仁杰",d,"大二",178));
list.add(new Student("白起",d1,"大二",187));
oos.writeObject(list);
oos.close();
}
public static void read(String file) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readUTF());
List <Student> list=(List<Student>) ois.readObject();
for(Student i:list)
System.out.println(i);
ois.close();
}
}
17.将Student对象转换成byte[],并将些byte数据写到文件中,再写一个程序读出此byte数组,将此byte数组转换成Student.
package ioStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class class17 {
public static void main(String[] args) throws Exception {
Student s=new Student("李易峰",new Date(),"哈哈",66);
// FileOutputStream out =new FileOutputStream("d:/李易峰99.txt");
// byte [] b=s.toString().getBytes();
// out.write(b);
// out.close();
// FileInputStream input =new FileInputStream("d:/李易峰99.txt");
// byte[] bb=new byte[1024];
// int size=0;
// size=input.read(b);
// System.out.println(new String(b,0,size));
File file = new File("d:/李易峰99.txt");
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(s);
bytes=bo.toByteArray();
fos.write(bytes, 0, bytes.length);
fos.close();
bo.close();
oo.close();
FileInputStream fis = new FileInputStream(file);
byte[] byte8 =new byte[1024];
int len = 0;
while((len=fis.read(byte8))!=-1);
ByteArrayInputStream bi = new ByteArrayInputStream(byte8);
ObjectInputStream oi = new ObjectInputStream(bi);
Student s8 = (Student)oi.readObject();
System.out.println(s8);
bi.close();
oi.close();
}
}
18,使用Properties类对文件进行读写操作.
package ioStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
class Properties商品问题
{ // 获取某种商品的数量,如果存在,获得数据,不存在,返回null
public static String getNum(String name) throws Exception
{
BufferedReader br=null;
br=new BufferedReader(new FileReader("d:/李易峰.txt"));
String s=null;
while ((s=br.readLine())!=null)
{
String[] values=s.split("=");
if(name.equalsIgnoreCase(values[0]))
{
return values[1];
}
}
return null;
}
// 获取某种商品的数量,如果存在,获得数据,不存在,返回null
public static String getGoodsNum(String name)
{
Properties p=new Properties();
try {
p.load(new FileReader("d:/李易峰.txt"));
return p.getProperty(name);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//增加商品;如没有,添加,如有此商品,替换;
public static void putGoods(String key,String value)
{ Properties p=new Properties();
try {
p.load(new FileReader("d:/李易峰.txt"));
//写到内存中
p.setProperty(key, value);
String test = null ;
//写回文件中
p.store(new FileWriter("d:/李易峰.txt"),test );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main (String[] args) throws Exception
{ putGoods("李易峰", "100");
System.out.println(getNum("李易峰"));
}
}