本程序实现图片文件复制,请在阅读程序基础上完成填空(共5个空)。import java.io.*;public class TestBinFileCopy{public static void main(String[] args) throws IOException {String source_path = "d:\\myfig.jpg";String destination_path = "d:\\myfig_copy.jpg";// 创建BinFileCopy类的实例化对象,并调用copy方法完成图片文件复制(1).copy(source_path, destination_path);}}classBinFileCopy{// 用字节流完成二进制文件的复制public voidcopy(String source_path, String destination_path) throws IOException {// 下面的语句用于声明并创建长度为16的字节数组byte[] bin =(2) ;FileInputStream fis = new FileInputStream(source_path);FileOutputStream fos =(3) ;while (fis.read(bin) !=(4) ) {(5) ;}// 此处为关闭流对象的代码(略)}}
查看答案
本程序利用缓冲流实现用户信息文件(users.txt)的读写操作,请在阅读程序的基础上完成填空(共5个空)。import java.io.*;import java.util.*;class User {private String id;private String type;private String password;private String name;private byte sex;private String city;public User(String id, String type, String password, String name, byte sex, String city) {super();this.id = id;this.type = type;this.password = password;this.name = name;this.sex = sex;this.city = city;}public User(String[] userInfo){this.id = userInfo[0];this.type = userInfo[1];this.password = userInfo[2];this.name = userInfo[3];this.sex = Byte.parseByte(userInfo[4]);this.city = userInfo[5];}// 此处为各属性的setter和getter方法(略)...@Overridepublic String toString() {return id + ":" + type + ":" + password + ":"+ name + ":" + sex + ":" + city; // 此处的冒号均为英文符号}}public class TestIOForUserFile {public static void main(String[] args) {List list = new ArrayList();list.add(new User("u00001", "普通用户", "pw00001", "崔翰林", (byte)1, "太原"));list.add(new User("u00002", "普通用户", "pw00002", "李红梅", (byte)0, "北京"));list.add(new User("u00003", "普通用户", "pw00003", "张松民", (byte)1, "上海"));list.add(new User("u00010", "管理员", "pw00010", "孟令君", (byte)1, "深圳"));Iterator iter = list.iterator();String fileName = "users.txt";FileWriter fw = null;BufferedWriter bw = null;try {fw = (1)(fileName, true);bw = new BufferedWriter(fw);while (iter.hasNext()) {User user = iter.next();//下面的语句将user的信息(可以通过user.toString()方法获取得到)写入缓冲输出流bw(2);bw.newLine();}} catch (IOException e) {e.printStackTrace();} finally {// 此处为关闭流对象代码(略)}List list2 = new ArrayList();FileReader fr = null;BufferedReader br = null;String[] userInfo;User user2;String s;try {fr = new FileReader(fileName);br =(3);while((s =(4)) != null){userInfo = s.split(":");// 调用User的单参数构造方法创建User对象user2 =(5);list2.add(user2);}for (User user: list2) {System.out.println(user);}} catch (IOException e) {e.printStackTrace();} finally {// 此处为关闭流对象代码(略)}}}
本程序使用多线程并发模拟多个发卡人同时发卡,利用简单的同步锁(关键字synchronized)实现线程同步控制。请在阅读程序的基础上完成填空(共5个空)。public class TestIssueClimbingCard {public static void main(String[] args) {IssueClimbingCard icc = (1);Thread staffA = new Thread(icc, "发卡人A");Thread staffB = new Thread(icc, "发卡人B");Thread staffC = new Thread(icc, "发卡人C");staffA.start();staffB.start();staffC.start();}}class IssueClimbingCard implements (2) {private int ClimbingCardNum = 1;@Overridepublic void (3) {while(true){(4) (this){ //同步块if(ClimbingCardNum <= 100) {try {// 此处睡眠10毫秒(5);} catch (InterruptedExceptione) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":发放第"+ ClimbingCardNum+ "号登山卡;");ClimbingCardNum ++;} else {System.out.println(Thread.currentThread().getName() + ":100张卡已全部发放完毕!");break;}}}}}
本程序利用List集合实现Student对象的存储与管理。请在阅读程序基础上完成填空(共5个空)。import java.util.*;class Student{private int id;private String name;private int age;public Student(int id,String name,int age){this.id=id;this.name=name;this.age=age;}// 此处为各属性的setter和getter方法(略)...public String toString(){return name + ":" + id + ":" + age;// 注:字符串中的冒号为英文符号}}public class TestList {public static void main(String[] args) {//声明List对象,创建LinkedList对象List list = (1);Student stu1 = new Student(20001, "蔡长锦", 20);Student stu2 = new Student(20006, "皮慧芳", 21);Student stu3 = new Student(20003, "董凯莉", 19);Student stu4 = new Student(20008, "孟卫东", 26);Student stu5 = new Student(20009, "齐贵田", 22);list.add(stu1);list.add(stu2);list.add(stu3);list.add(stu4);list.add(stu5);// 使用普通for循环遍历所有的Student对象for (int i = 0; i < list.size(); i++) {System.out.println((2) );}// 下面的语句删除学号为20006的学生(3) ;// 使用迭代器遍历所有的Student对象Iterator iter = (4) ;while((5) ){System.out.println(iter.next());}}}
本程序利用字符流实现文本文件的复制,请在阅读程序的基础上完成填空(共5个空)。import java.io.*;public class TestTextFileCopy {public static void main(String[] args) throws IOException {String source_path = "d:/aboutnuc.txt";String destination_path = "d:/aboutnuc_copy";(1).copy(source_path, destination_path);}}class TextFileCopy{// 用字符流完成文本文件的复制public void copy(String source_path, String destination_path) throws IOException {// 声明并创建长度为10的字符数组char[] charBuf = (2) ;FileReader fr = (3);FileWriter fw = new FileWriter(destination_path);int n;while ((n = (4) ) != (5) ) {fw.write(charBuf, 0, n);}System.out.println();// 此处为关闭流对象的代码(略)...}}