猜拳选择器
package com.jredu.homework; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.Scanner; public class trueHomework { public String[] gameData = { "剪刀", "石头", "布" }; /** * 产生电脑随机出拳数字 */ public int getPCNumber() { return (new Random()).nextInt(3) + 1; } /** * 获取用户输入数字 */ public int getUserNumber() { Scanner sc = new Scanner(System.in ); int n = sc.nextInt(); return n; } /** * 判断game结果 */ public String getGameResult(int userNumber, int pcNumber) { int result = userNumber - pcNumber; /** * 1 剪刀 2石头 3布 -1,2 负 0平 1,-2胜 */ String resultString = ""; if (result == 0) { resultString = "平"; } else if (result == -1 || result == 2) { resultString = "负"; } else { resultString = "胜"; } return resultString; } public String getGameStartTime() { Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy年M月d日 HH:mm"); return sf.format(date); } public String readUserData() { File f = new File("G:/IO数据/game.txt."); if (!f.exists()) { return ""; } StringBuilder userData = new StringBuilder(); try { FileInputStream fis = new FileInputStream(f); byte[] data = new byte[1024]; int len; while ((len = fis.read(data)) != -1) { userData.append(new String(data, 0, len)); } fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return userData.toString(); } /** * 写入用户游戏数据 */ public void writeUserGameData(String userData) { File f = new File("G:/IO数据/game.txt"); if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } try { FileOutputStream fos = new FileOutputStream(f, true); fos.write(userData.getBytes()); fos.flush(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { trueHomework game = new trueHomework(); System.out.println("游戏开始。。。 您的历史成绩是:"); System.out.println(game.readUserData()); System.out.println("1- 剪刀 2-石头 3-布"); String isGoon = "y"; int gameCount = 0; String gameStartTime = game.getGameStartTime(); System.out.println(gameStartTime); StringBuilder userData = new StringBuilder(gameStartTime); userData.append(" "); do { gameCount++; System.out.println("1- 剪刀 2-石头 3-布"); int pcNumber = game.getPCNumber(); System.out.println("请选择出拳"); int userNumber = game.getUserNumber(); String resultString = game.getGameResult(userNumber, pcNumber); StringBuilder outputString = new StringBuilder("第"); outputString.append(gameCount); outputString.append("局 电脑:"); outputString.append(game.gameData[pcNumber - 1]); outputString.append(" 用户:"); outputString.append(game.gameData[userNumber - 1]); outputString.append(" "); outputString.append(resultString); // "第1局 电脑:剪刀 用户:石头 胜"; System.out.println(outputString.toString()); userData.append(outputString.toString()); userData.append(" "); System.out.println("是否继续y/n"); Scanner sc = new Scanner(System.in ); isGoon = sc.next(); } while (!isGoon.equals("n")); game.writeUserGameData(userData.toString()); System.out.println("游戏结束"); } }
结果图:
将某个磁盘下的目录复制到另一个目录下(可含目录嵌套)
package com.jredu.iodemo; import java.io .File; import java.io .FileInputStream; import java.io .FileNotFoundException; import java.io .FileOutputStream; import java.io .IOException; public class AllFileCopyTest { //把一个目录内的全部文件复制到另一个目录里面 public void copyFile(File sourcefile,File destFile){ try { FileInputStream fis=new FileInputStream(sourcefile); FileOutputStream fos=new FileOutputStream(destFile.getAbsolutePath()); int len=-1; byte[] data=new byte[1024]; while((len=fis.read(data))!=-1){ fos.write(data,0,len); } fis.close(); fos.flush(); fos.close(); System.out.println("copied"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void copy(String sourcePath,String destPath){ File f=new File(sourcePath); File destFile=new File(destPath);//G:/new_io if(!destFile.exists()){ destFile.mkdir(); } for(File file:f.listFiles()){ if(file.isDirectory()){ //G:/new_io/new File subFile=new File(destFile.getAbsoluteFile() +File.separator +file.getName()); if(!subFile.exists()){ subFile.mkdir(); } //G:/io/new G:/new_id/new copy(file.getAbsolutePath(),subFile.getAbsolutePath()); }else{ //System.out.println(file.getAbsolutePath()+" "+destFile.getAbsolutePath()); //d:/new_io/abc.data copyFile(file, new File(destFile.getAbsolutePath() +File.separator +file.getName())); } } } public static void main(String[] args) { AllFileCopyTest test=new AllFileCopyTest(); test.copy("G:/IOO", "G:/new_IOO"); } }
结果图:
FileReader,BufferedReader和FileWriter,BufferedWriter
package com.jredu.iodemo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) { try { FileReader fr = new FileReader("G:/IOO/dsa.txt"); // BufferReader是高层流,只能借助底层流去实现(FileReader) // 流链 BufferedReader br = new BufferedReader(fr); try { String s = null; while ((s = br.readLine()) != null) { System.out.println(s);// 输出的S是文档内的内容 } fr.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } try { FileWriter fw = new FileWriter("G:/ad.txt"); BufferedWriter bW = new BufferedWriter(fw); bW.write("hello file"); bW.newLine();// 相当于做了一个换行 bW.write("Ok"); fw.flush(); bW.flush(); fw.close(); bW.close(); } catch (IOException e) { e.printStackTrace(); } } }
结果图: