题目1:
HashMap集合存储学生对象并遍历。
需求:
创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对象,并遍历。
要求:
保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象。
代码如下:
package HashMapPrintPack02;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name == null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;}
}
package HashMapPrintPack02;import java.util.HashMap;
import java.util.Set;public class HashMapDemo {public static void main(String[] args){HashMap<Student,String> hs = new HashMap<Student,String>();Student s1 = new Student("Tom",30);Student s2 = new Student("Jack",28);Student s3 = new Student("Lily",17);Student s4 = new Student("Tom",30);hs.put(s1,"西安");hs.put(s2,"北京");hs.put(s3,"上海");hs.put(s4,"深圳");Set<Student> keySet = hs.keySet();for (Student key:keySet){String value = hs.get(key);System.out.println(key.getName()+","+key.getAge()+","+value);}}}
题目2:
ArrayList集合存储HashMap元素并遍历。
需求:
创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历。
代码如下:
package ArrayListAndHashMapPack01;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;public class ArrayListIncludeHashMapDemo {public static void main(String[] args){ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String, String>>();HashMap<String,String> hm1 = new HashMap<String,String>();hm1.put("Tom","Jack");hm1.put("Bom","Lily");array.add(hm1);HashMap<String,String> hm2 = new HashMap<String,String>();hm1.put("小明","小红");hm1.put("小兰","小绿");array.add(hm2);HashMap<String,String> hm3 = new HashMap<String,String>();hm1.put("Mike","Zliy");hm1.put("Bo","Db");array.add(hm3);for(HashMap<String,String> hm:array){Set<String> keySet = hm.keySet();for (String key:keySet){String value = hm.get(key);System.out.println(key+","+value);}}}
}
题目3:
HashMap集合存储ArrayList元素并遍历。
需求:
创建一个HashMap集合,存储三个键值对元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历。
代码如下:
package HashMapIncludeArrayListPack;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;public class HashMapIncludeArrayListDemo {public static void main(String[] args){HashMap<String, ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();ArrayList<String> sgyy = new ArrayList<String>();sgyy.add("赵云");sgyy.add("刘备");hm.put("三国演义",sgyy);ArrayList<String> xyj = new ArrayList<String>();xyj.add("唐僧");xyj.add("猴哥");hm.put("西游记",xyj);ArrayList<String> hlm = new ArrayList<String>();hlm.add("贾宝玉");hlm.add("林黛玉");hm.put("红楼梦",hlm);Set<String> keySet = hm.keySet();for (String key:keySet){System.out.println(key);ArrayList<String> value = hm.get(key);for (String s:value){System.out.println("\t"+s);}}}}
题目4:
统计字符串中每个字符串出现的次数。
需求:
键盘录入一个字符串,要求统计字符串中每个字符串出现的次数。
举例:
键盘录入"aababcabcdabcde",在控制台输出:“a(5)b(4)c(3)d(2)e(1)”
代码如下:
package CountStringNumPack;import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;public class HashMapDemp {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String line = sc.nextLine();HashMap<Character,Integer> hm = new HashMap<Character,Integer>();for (int i = 0;i<line.length();i++){char key = line.charAt(i);Integer value = hm.get(key);if (value==null){hm.put(key,1);}else {value++;hm.put(key, value);}}StringBuilder sb = new StringBuilder();Set<Character> keySet = hm.keySet();for (Character key:keySet){Integer value = hm.get(key);sb.append(key).append("(").append(value).append(")");}String ans = sb.toString();System.out.println(ans);}}
package CountStringNumPack;import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;public class HashMapDemp {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String line = sc.nextLine();TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();for (int i = 0;i<line.length();i++){char key = line.charAt(i);Integer value = hm.get(key);if (value==null){hm.put(key,1);}else {value++;hm.put(key, value);}}StringBuilder sb = new StringBuilder();Set<Character> keySet = hm.keySet();for (Character key:keySet){Integer value = hm.get(key);sb.append(key).append("(").append(value).append(")");}String ans = sb.toString();System.out.println(ans);}}
题目5:
ArrayList存储学生对象并排序。
需求:
ArrayList存储学生对象,使用Collections对ArrayList进行排序。
要求:
按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。
代码如下:
package CollectionsDemo02;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package CollectionsDemo02;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class CollectionsDemo02 {public static void main(String[] args){ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("Tom",18);Student s2 = new Student("Jack",20);Student s3 = new Student("Bom",29);Student s4 = new Student("Lily",23);array.add(s1);array.add(s2);array.add(s3);array.add(s4);Collections.sort(array, new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s1.getAge()-s2.getAge();int num2 = num==0?s1.getName().compareTo(s2.getName()):num;return num2;}});for (Student s:array){System.out.println(s.getName()+","+s.getAge());}}
}
题目6:
模拟斗地主。
需求:
通过程序实现斗地主过程中的洗牌,发牌和看牌。
代码如下:
package PokerPack01;import java.util.ArrayList;
import java.util.Collections;public class PokerDemo01 {public static void main(String[] args) {ArrayList<String> array = new ArrayList<String>();String[] colors = {"◆", "♣", "♥", "♠"};String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};for (String color : colors) {for (String number:numbers){array.add(color+number);}}array.add("大王") ;array.add("小王");Collections.shuffle(array);// System.out.println(array);ArrayList<String> tomArray = new ArrayList<String>();ArrayList<String> jackArray = new ArrayList<String>();ArrayList<String> bomArray = new ArrayList<String>();ArrayList<String> dpArray = new ArrayList<String>();for (int i = 0;i<array.size();i++){String poker = array.get(i);if (i >= array.size()-3){dpArray.add(poker);}else if (i % 3==0){tomArray.add(poker);}else if (i % 3==1){jackArray.add(poker);}else if (i % 3==2){bomArray.add(poker);}}lookPoker("Tom",tomArray);lookPoker("Jack",jackArray);lookPoker("Bom",bomArray);}public static void lookPoker(String name,ArrayList<String> array){System.out.println(name+"的牌是:");for (String poker:array){System.out.print(poker+" ");}System.out.println();}
}
题目7:
模拟斗地主2.0
需求:
通过程序实现斗地主过程中的洗牌,发牌和看牌。
要求:
对牌进行排序。
代码如下:
有时间写!!!
题目8(递归):
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
代码如下:
package DiGuiPack;public class DiGuDemo01 {public static void main(String[] args){int ans = f(20);System.out.println(ans);}public static int f(int n){if (n==1 || n==2) return 1;else return f(n-1)+f(n-2);}
}
题目9:
需求:
用递归求5的阶乘,并把结果在控制台输出。
代码如下:
package DiGuiPack;public class DiGuDemo01 {public static void main(String[] args){int ans = f(5);System.out.println(ans);}public static int f(int n){if (n==1) return 1;else return n*f(n-1);}}
题目10:
遍历目录。
需求:
给定一个路径(D:\JavaDemo),请通过递归完成遍历该目录下的所有内容,并把所有文件的绝对路径输出在控制台。
代码如下:
package DiGuiPack;import java.io.File;public class DiGuiDemo02 {public static void main(String[] args){File srcfile = new File("D:\\JavaDemo");getAllFilePath(srcfile);}public static void getAllFilePath(File srcFile){File[] fileArray = srcFile.listFiles();if (fileArray!=null){for (File file:fileArray){if (file.isDirectory()){getAllFilePath(file);}else{System.out.println(file.getAbsolutePath());}}}}}
题目11:
复制文本文件。
需求:
把“D:\JavaDemo\java.txt”复制到模块目录下的"窗里窗外.txt"
代码如下:
package InstreamPack;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class CopyTxtDemo {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D:\\JavaDemo\\java.txt");FileOutputStream fos = new FileOutputStream("D:\\JavaDemo\\窗里窗外.txt");int by;while((by = fis.read())!=-1){fos.write(by);}fis.close();fos.close();}}
题目12:
复制图片。
需求:
把"D:\JavaDemo\luf.jpg",复制到某个目录。
代码如下:
package InstreamPack;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileInputStreamDemo02 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D:\\JavaDemo\\luf.jpg");FileOutputStream fos = new FileOutputStream("D:\\JavaDemo\\tyc.jpg");byte[] bys = new byte[1024];int len;while((len=fis.read(bys))!=-1){fos.write(bys,0,len);}fis.close();fos.close();}
}
题目13:
复制视频。
需求:
把"D:\JavaDemo\字节流复制图片.avi",复制到模块目录下的"字节流复制图片.avi"
代码如下:
题目14:
复制Java文件。
需求:把“D:\JavaDemo\ArgsDemo01.java”复制一份。
代码如下:
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo01 {public static void main(String[] args) throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\JavaDemo\\ArgsDemo01.java"));OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\JavaDemo\\hello.java"));int ch;while((ch = isr.read())!=-1){osw.write(ch);}osw.close();isr.close();}
}
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo01 {public static void main(String[] args) throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\JavaDemo\\ArgsDemo01.java"));OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\JavaDemo\\hello.java"));// int ch;
// while((ch = isr.read())!=-1)
// {
// osw.write(ch);
// }char[] chs = new char[1024];int len;while((len=isr.read(chs))!=-1){osw.write(chs,0,len);}osw.close();isr.close();}
}
题目15:
复制Java文件。
需求:
把“D:\JavaDemo\ArgsDemo01.java”复制一份。
代码如下:
package CopyJavaPack01;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CopyJavaDemo02 {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("D:\\JavaDemo\\ArgsDemo01.java");FileWriter fw = new FileWriter("D:\\JavaDemo\\hellow02.java");int ch;while((ch=fr.read())!=-1){fw.write(ch);}fw.close();fr.close();}}
package CopyJavaPack01;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CopyJavaDemo02 {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("D:\\JavaDemo\\ArgsDemo01.java");FileWriter fw = new FileWriter("D:\\JavaDemo\\hellow02.java");// int ch;
// while((ch=fr.read())!=-1)
// {
// fw.write(ch);
// }char[]chs = new char[1024];int len;while((len=fr.read(chs))!=-1){fw.write(chs,0,len);}fw.close();fr.close();}}
题目16:
复制Java文件(字符缓冲流改进版)
需求:
把“D:\JavaDemo\hellow02.java”复制一份。
代码如下:
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo03 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hellow02.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\hello03.java"));int ch;while((ch=br.read())!=-1){bw.write(ch);}bw.close();br.close();}}
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo03 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hellow02.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\hello03.java"));// int ch;
// while((ch=br.read())!=-1)
// {
// bw.write(ch);
// }char [] chs = new char[1024];int len;while((len=br.read(chs))!=-1){bw.write(chs,0,len);}bw.close();br.close();}}
题目17:
复制Java文件(字符缓冲流特有功能改进版)
需求:
把“D:\JavaDemo\hello.java”复制一份。
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo04 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\java01.java"));String line;while((line=br.readLine())!=null){System.out.println(line);bw.newLine();bw.flush();}br.close();bw.close();}}
题目18:
集合到文件。
需求:
把ArrayList集合中的字符串数据写入到文本文件。
要求:
每一个字符串元素作为文件中的一行数据。
代码如下:
package test22;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class ArrayListToTxtDemo {public static void main(String[] args) throws IOException {ArrayList<String> array = new ArrayList<String>();array.add("hello");array.add("world");array.add("java");BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\array.txt"));for (String s:array){bw.write(s);bw.newLine();bw.flush();}bw.close();}
}
题目19:
文件到集合。
需求:
把文本文件中的数据读取到集合中,并遍历集合。
要求:
文件中每一行数据是一个集合元素。
代码如下:
package test22;import java.io.*;
import java.util.ArrayList;public class TxtToArrayListDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\array.txt"));ArrayList<String> array = new ArrayList<String>();String line;while((line = br.readLine())!=null){array.add(line);}br.close();for (String s:array){System.out.println(s);}}
}
题目20:
点名器。
需求:
我有一个文件里面存储了班级同学的姓名,每一行姓名占一行,要求通过程序实现随机点名器。
代码如下:
package test22;import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;public class CallNameDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\array.txt"));ArrayList<String> array = new ArrayList<String>();String line;while((line = br.readLine())!=null){array.add(line);}br.close();Random r = new Random();int idx = r.nextInt(array.size());String name = array.get(idx);System.out.println("幸运儿是:"+name);}}
题目21:
集合到文件(改进版)。
需求:
把ArrayList集合中的字符串数据写入到文本文件。
要求:
每一个字符串元素作为文件中的一行数据。
格式:
学号,姓名,年龄,居住地
举例:
001,Tom,24,北京
代码如下:
package test22;public class Student {private String sid;private String name;private int age;private String address;public Student() {}public Student(String sid, String name, int age, String address) {this.sid = sid;this.name = name;this.age = age;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
package test22;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class ArrayListToFileDemo {public static void main(String[] args) throws IOException {ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("001","Tom",20,"北京");Student s2 = new Student("002","Jack",18,"上海");Student s3 = new Student("003","Bom",22,"韶关");Student s4 = new Student("004","Lily",23,"美国");array.add(s1);array.add(s2);array.add(s3);array.add(s4);BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\students.txt"));for (Student s:array){StringBuilder sb = new StringBuilder();sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}}
题目22:
文件到集合(改进版)。
需求:
把文本文件中的数据读取到集合中,并遍历集合。
要求:
文件中每一行数据是一个集合元素。
举例:
001,Tom,29,北京
代码如下:
package test22;public class Student {private String sid;private String name;private int age;private String address;public Student() {}public Student(String sid, String name, int age, String address) {this.sid = sid;this.name = name;this.age = age;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
package test22;import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;public class FileToArrayListDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\students.txt"));ArrayList<Student> array = new ArrayList<Student>();String line;while((line = br.readLine())!=null){String[] strArray = line.split(",");Student s = new Student();s.setSid(strArray[0]);s.setName(strArray[1]);s.setAge(Integer.parseInt(strArray[2]));s.setAddress(strArray[3]);array.add(s);}br.close();for (Student s:array){System.out.println(s.getSid()+","+s.getName()+","+s.getAge()+","+s.getAddress());}}
}
题目23:
集合到文件(数据排序改进版)
需求:
键盘录入5个学生信息(学生,语文成绩,数学成绩,英语成绩)。要求按照成绩总分从高到低写入文本文件。
格式:
姓名,语文成绩,数学成绩,英语成绩
举例:
Tom,98,99,100
代码如下:
package ArrayListToFilePack;public class Student {private String name;private int chinese;private int math;private int english;public Student() {}public Student(String name, int chinese, int math, int english) {this.name = name;this.chinese = chinese;this.math = math;this.english = english;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public int getSum(){return chinese+math+english;}}
package ArrayListToFilePack;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;public class TreeSetToFileDemo {public static void main(String[] args) throws IOException {TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num1 = s2.getSum()-s1.getSum();int num2 = num1==0? s1.getChinese()- s2.getChinese() :num1;int num3 = num2==0?s1.getMath()-s2.getMath():num2;int num4 = num3==0?s1.getName().compareTo(s2.getName()):num3;return num4;}});for (int i =0;i<5;i++){Scanner sc = new Scanner(System.in);System.out.println("请录入第"+(i+1)+"个学生信息");System.out.println("name = ");String name = sc.nextLine();System.out.println("语文成绩 = ");int chinese = sc.nextInt();System.out.println("数学成绩 = ");int math = sc.nextInt();System.out.println("英语成绩 = ");int english = sc.nextInt();Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);ts.add(s);}BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\ts.txt"));for (Student s :ts){StringBuilder sb = new StringBuilder();sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}
}
题目24:
复制单级文件夹。
需求:
把"D:\JavaDemo\hello"这个文件夹复制到"D:\JavaDemo02"这个文件夹里。
代码如下:
package CopyFolderPack;import java.io.*;public class CopyFolderDemo {public static void main(String[] args) throws IOException {File srcFolder = new File("D:\\JavaDemo\\hello");String srcFolderName = srcFolder.getName();File destFolder = new File("D:\\JavaDemo02",srcFolderName);if (!destFolder.exists()){destFolder.mkdir();}File[] listFiles = srcFolder.listFiles();for (File srcFile : listFiles){String srcFileName = srcFile.getName();File destFile = new File(destFolder,srcFileName);copyFile(srcFile,destFile);}}private static void copyFile(File srcFile, File destFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while((len = bis.read())!=-1){bos.write(bys,0,len);}bos.close();bis.close();}
}
题目25:
复制多级文件夹。
需求:
把"D:\JavaDemo"复制到"D:\JavaDemo02"这个文件夹里。
代码如下:
package CopyFolderPack;import java.io.*;public class CopyFoldersDemo {public static void main(String[] args) throws IOException {File srcFile = new File("D:\\JavaDemo");File destFile = new File("D:\\JavaDemo02");copyFolder(srcFile,destFile);}//复制文件夹private static void copyFolder(File srcFile, File destFile) throws IOException {if (srcFile.isDirectory()){String srcFileName = srcFile.getName();File newFolder = new File(destFile,srcFileName);if (!newFolder.exists()){newFolder.mkdir();}File[] fileArray = srcFile.listFiles();for (File file:fileArray){copyFolder(file,newFolder);}}else{File newFile = new File(destFile,srcFile.getName());copyFile(srcFile,newFile);}}private static void copyFile(File srcFile, File destFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while((len = bis.read(bys))!=-1){bos.write(bys,0,len);}bos.close();bis.close();}}
题目26:
复制Java文件(打印流改进版)
需求:
把"D:\JavaDemo\hello.java"复制到"D:\JavaDemo03\java01.java"
。
代码如下:
package CopyJavaPack02;import java.io.*;public class CopyJavaDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo03\\java01.java"));String line;while((line = br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();}
}
package CopyJavaPack02;import java.io.*;public class CopyJavaDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));PrintWriter bw = new PrintWriter(new FileWriter("D:\\JavaDemo03\\java01.java"),true);String line;while((line = br.readLine())!=null){bw.println(line);}bw.close();br.close();}
}
题目27:
游戏次数。
需求:
请写程序实现猜数字小游戏只能试玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值。
game.txt:
代码如下:
在这里插入代码片package PropertiesPack;import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;public class PropertiesTest {public static void main(String[] args) throws IOException {Properties prop = new Properties();FileReader fr = new FileReader("D:\\JavaDemo\\game.txt");prop.load(fr);fr.close();String count = prop.getProperty("count");int number = Integer.parseInt(count);if (number >= 3){System.out.println("游戏试玩已结束,想玩请充值");}else{GuessNumber.start();number++;prop.setProperty("count",String.valueOf(number));FileWriter fw = new FileWriter("D:\\JavaDemo\\game.txt");prop.store(fw,null);fw.close();}}}
题目28:
卖票。
需求:
某电影院目前正在上映国产大片,共有100张票,而它有3个窗口卖票,请设计一个程序模拟该电影院卖票。
代码如下:
package SellTicketPack;public class SellTicket implements Runnable{private int tickets = 100;@Overridepublic void run() {while (true) {if (tickets > 0) {System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");tickets--;}}}}
package SellTicketPack;public class SellTicketDemo {public static void main(String[] args){SellTicket st = new SellTicket();Thread t1 = new Thread(st,"窗口1");Thread t2 = new Thread(st,"窗口2");Thread t3 = new Thread(st,"窗口3");t1.start();t2.start();t3.start();}}
题目29:
卖票问题(进阶)。
点击下方链接:
卖票问题(进阶)