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,图片复制



猜你喜欢

转载自blog.csdn.net/draught_bear/article/details/78628874