本文共 3728 字,大约阅读时间需要 12 分钟。
import java.io.*;public class Exam1 { public static void main(String[] args) { File file = new File("G://HelloWorld.txt"); // 创建文件 if (!file.exists()){ try { file.createNewFile(); System.out.println("创建文件成功"); } catch (IOException e) { e.printStackTrace(); } } // 判断文件 if (file.isDirectory()){ System.out.println("这是个目录"); }else { System.out.println("这是一个文件"); } // 创建文件夹 File dir = new File("G://IOTest"); if (!dir.exists()){ dir.mkdir(); } FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("G://HelloWorld.txt"); out = new FileOutputStream("G://IOTest//HelloWorld.txt"); int len = 0; while ((len=in.read())!=-1){ out.write(len); } System.out.println("文件移动成功"); // 遍历输出文件 System.out.println("IOTest目录下的文件如下:"); File[] list = dir.listFiles(); for (File file1:list){ System.out.println(file1.getName()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
public class Exam2 { public static void main(String[] args) { // x 公鸡 只 // y 母鸡 只 // z 小鸡 只 // 5*x + 3*y + (1/3)*z =100 // 总价钱 15x + 9y + z = 300 // x + y + z =100 //总数量 x + y + z = 100 // 14x + 8y = 200 // 公鸡 for (int i=0;i<=20;i++){ // 母鸡 for (int j=0;j<34;j++){ // 小鸡 for(int k=0;k<=100;k++){ if (15*i+9*j+k==300&&i+j+k==100){ System.out.println("公鸡"+i+",母鸡"+j+",小鸡"+k); } } } } }
import java.util.HashMap;import java.util.Iterator;class BankAccount{ // 声明变量 String id; String name; double money; // 构造方法 public BankAccount(String id, String name, double money) { this.id = id; this.name = name; this.money = money; } // 重写toString @Override public String toString() { return "储户id:" + id + '\t' + "姓名" + name + '\t' + "余额" + money; }}public class Exam3 { public static void main(String[] args) { // 创建HashMap HashMaphashMap = new HashMap<>(); // 添加元素 hashMap.put("101",new BankAccount("101","祝枝山",10000.0)); hashMap.put("102",new BankAccount("102","文征明",20000.0)); hashMap.put("103",new BankAccount("103","唐伯虎",30000.0)); // 检索目标元素 if (hashMap.containsKey("102")){ System.out.println("检索id为102的储户信息如下:"+"\n"+hashMap.get("102")); } // 遍历打印 System.out.println("遍历哈希表结果如下:"); Iterator iterator = hashMap.keySet().iterator(); while (iterator.hasNext()){ Object key = iterator.next(); Object value = hashMap.get(key); System.out.println(value); } }}
转载地址:http://kweq.baihongyu.com/