java IO 测试题
1, 在电脑D盘下存在一个文件夹myFile,通过程序在该文件夹中创建一个helloWorld.java文件,并向该文件中随机输入若干个0—9的数字
2, 在文件夹myFile中创建一千个随机拓展名为.java,.txt,.doc,文件以产生的顺序命名,如1.java,2.txt等。
3, 往myFile中的每个java文件中随机输入若干个‘A’到‘Z’的大写字母
4, 在myFile中存在视屏文件myVideo.wmv,将该文件拷贝到myFile1文件下,并播放。(帮助:
播放代码:Runtime run = Runtime.getRunTime();
Run.exec(“\”播放器路径\ ”视屏文件地址”);
5,随机删除myFile中的文件(保留约百分之五十的文件),然后将myFile中的所有.java文件复制到文件夹myFile1下
我只写了前四题,第五题还有点问题。所以,如果小伙伴们有更好的答案的话,可以发上来一起探讨探讨。谢谢^-^
package IOTest;import java.io.*;
import java.util.*;public class ioTest3 {public static void main(String[] args) throws IOException, InterruptedException {//First();
// Second();
// third();//four();five(); }private static void five() throws FileNotFoundException, IOException {File file = new File("D:\\myFile");String[] list = file.list();String fileType0 = ".txt";String fileType1 = ".java";String fileType2 = ".doc";String type = new String();String folderPath = "D:\\myFile\\";//随机删除大约百分之五十的文件for(int i=0;i<(list.length)/2;i++){int delNum = (int)(Math.random() * (list.length));//判断随机删除的文件的名字String delFilePath = folderPath + list[delNum];System.out.println(delFilePath);File delFile = new File(delFilePath);delFile.delete();}System.out.println("==================删除完毕==================");//将.java文件复制到myFile中System.out.println("在myFile1中创建文件");String[] newList = file.list(); for(String fileName : newList) {System.out.println(fileName);}System.out.println();File folder2 = new File("D:\\myFile1");String folderPath2 = "D:\\myFile1\\";for(String fileName : newList){if(fileName.endsWith(".java")){String copyPath = folderPath2 + fileName;File fileCopy = new File(copyPath);FileInputStream fis = new FileInputStream(fileCopy);fileCopy.createNewFile();FileOutputStream fos = new FileOutputStream(fileCopy);byte[] buff = new byte[10];int temp = -1;while((temp = fis.read(buff)) != -1){fos.write(buff, 0, temp);}fis.close();fos.close();}}System.out.println("执行完毕!");}private static void four() throws FileNotFoundException, IOException {File file = new File("D:\\myFile\\capture-1.avi");FileInputStream fis = new FileInputStream(file);File fileNew = new File("D:\\myFile1\\capture-1.avi");FileOutputStream fos = new FileOutputStream(fileNew);fileNew.createNewFile();byte[] buff = new byte[1024 * 1024];int temp = -1;while((temp = fis.read(buff)) != -1){fos.write(buff,0,temp);}Runtime run = Runtime.getRuntime();run.exec("\"D:\\暴风影音\\StormPlayer.exe\"D:\\myFile1\\capture-1.avi");fos.close();fis.close();System.out.println("执行完毕!");}private static void third() throws IOException {File file = new File("D:\\myFile");String[] list = file.list();for(String filename : list){if(filename.endsWith(".java")){FileWriter wr = new FileWriter("D:\\myFile\\" + filename);String str = new String();for(int i=0;i<(int)(Math.random()*100);i++) {int temp = (int)(Math.random() * 26) + 65;str +=(char)temp;}System.out.println(str);wr.write(str);wr.close();}}System.out.println("执行完毕");}private static void Second() throws IOException {String filePath = "D:\\myFile\\";String fileType1 = ".txt";String fileType2 = ".java";String fileType3 = ".doc";String str = null;String path = null;for(Integer i=0;i<50;i++){int temp = (int)(Math.random() * 10);if(temp % 3 ==0) {str = fileType1;}else if(temp %3 == 1) {str = fileType2;}else if(temp %3 == 2) {str = fileType3;}path = filePath + i.toString() + str;File file = new File(path);file.createNewFile();}}private static void First() throws IOException {File file = new File("D:\\myFile\\helloWorld.java");file.createNewFile();FileWriter fw = new FileWriter(file);int len = (int)(Math.random() * 10);for(Integer i = 0;i < len ; i++){Integer temp = (int)(Math.random() * 10);fw.write(temp.toString());}fw.close();}}
========================分割线==================================
更新:1,在D盘下创建一个文件夹myFile,并在myFile中创建一个文件:file1.txt,相file.txt中随机1000个随机字母‘A'~'Z'
使用字节流写入字母到文件中
File file = new File("D:/myFile2");file.mkdir();File file1 = new File(file,"file1.txt");file1.createNewFile();OutputStream out = new FileOutputStream(file1);for(int i=0;i<1000;i++){int ch = (int)(Math.random() * 26 + 65);out.write(ch);}out.close();
2,计数file1中‘T’的数量
File file = new File("D:/myFile2/file1.txt");InputStream in = new FileInputStream(file);int len = -1;int count =0;byte[] buff = new byte[10];while((len = in.read()) != -1){if(len == 'T'){count ++;}}System.out.println("T的数量为:" + count);in.close();
3,图片复制
java IO 测试题相关推荐
- Java IO 流 学习 总结
I/O 主要是对文件进行处理, 其主要包含3个方面的内容 1 input 输入 2 out 出入 3 File 文件 Java 流的概念 流是一组有序的数据序列,流提供了一条通道程序,可以是这条通道 ...
- Java基础(二十七)Java IO(4)字符流(Character Stream)
字符流用于处理字符数据的读取和写入,它以字符为单位. 一.Reader类与Writer类 1.Reader类是所有字符输入流的父类,它定义了操作字符输入流的各种方法. 2.Writer类是所有字符输出 ...
- Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter
Java IO流学习总结四:缓冲流-BufferedReader.BufferedWriter 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/det ...
- java openfile busy_android java.io.IOException: open failed: EBUSY (Device or resource busy)
今天遇到一个奇怪的问题, 测试在程序的下载界面,下载一个文件第一次下载成功,删除后再下载结果下载报错, 程序:file.createNewFile(); 报错:java.io.IOException: ...
- java.io.file jar_IDEA Maven 打包运行 jar java.io.FileNotFoundException: 问题?
java.io.FileNotFoundException: D:\workspaceIdea\ywjc-refactor\target\lib\javax.annotation-api-1.3.2. ...
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- Java IO系列之字节流拷贝文件性能比较
Java IO 字节流基类 InputStream--输入流, OutPutStream--输出流, 输入流用于读,输出流用于写. 字节流默认一次只读取或输出一个字节. package jonavin ...
- java io在文件结尾持续添加内容
[代码] java io在文件结尾持续添加内容 public static void write(String path, String content) {try {File f = new Fil ...
- java.io包对象读写_java.io 包中的____________和____________类主要用于对对象(Object)的读写_学小易找答案...
[多选题]连铸钢水成分控制的要求有( ). [单选题]起动机用直流电动机将电能转化为 [单选题]下列关于我国少数民族传统禁忌的说法中,错误的是( ). [多选题]下列选项中 , 属于表单控件的是 ( ...
- linux rm 提示io异常,Hadoop异常 java.io.IOException: Job status not available
Hadoop集群上跑mapreduce,在job任务执行完成退出时报 java.io.IOException: Job status not available异常.Job client请求job状态 ...
最新文章
- oracle中treat函数,Oracle 函数概览
- filter java 是单例的吗_JAVA 设计模式之 单例模式详解
- Linux 查看软件位置的命令
- Linux vim编辑器命令总结(转载)
- 三菱PWM格式文件梯形图编码格式
- SpringBoot+MyBatisPlus实现前端传递时间查询条件ajax请求后台并回显数据流程整理
- 领导,你可以做得更好
- 覆盖所有面试知识点,送大厂面经一份!
- python pca降维_机器学习的降维打击
- 修改yum的镜像服务器为阿里云
- C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)
- python 进阶案例_【python3】while和for的进阶案例
- 你好,未来! | 2018腾讯“云+未来”峰会五月启幕
- python调用按键精灵插件_按键精灵必须掌握的命令之插件命令
- obs录制视频做up主流程
- C语言 求两个数最小公倍数和最大公因数
- Go手动安装pkg包初体验
- phpstudy 报错Error while setting value ‘NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO‘ to ‘sql_mode‘
- 基于C++实现惊险刺激的Flappy Bird设计
- matlab fill 图案填充,Matlab条形图中填充图案