JAVA高手帮忙看看~

来源:百度知道 编辑:UC知道 时间:2024/09/20 04:21:01
我想写一个自动复制的小程序,利用随机数进行无限复制,写得差不多了。当年不会运用死循环来无限复制~高手帮看看~谢谢了~复制和随机数已经写好了。就差死循环了。
package b12;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;//输入
import java.io.FileInputStream;//读写
import java.io.IOException;//异常处理
import java.io.InputStream;

public class Zy {

public static void main(String[] args) {
long[] random = new long[100];
for (int i = 0; i < 100; i++) {
random[i] = Math.round(Math.floor((Math.random() * 1)));
int b=2;

int a=0;
FileInputStream in=null;
FileOutputStream out=null;
if(b>1){
try {
in=new FileInputStream("E:\\李聪\\1.txt");
out=new FileOutputStream("E:\\李聪\\"+random[i]+".txt");
a=in.read();
while(a!=-1){
out.write(a);
a = in.read();

}

System.out.println("复制成功");<

两点,
1.Math.random() 的取值范围在[0,1)之前,我记得是不包含1
你这样做, Math.round(Math.floor((Math.random() * 1)));
结果只能是0,应适当的扩大数的范围,如:
Math.round(Math.floor((Math.random() * 1000)));

2.我不知道你为什么要死循环,如果你想要死循环,那
long[] random = new long[100];
变成
long random = new long;

for (int i = 0; i < 100; i++) {
变成
for (int i = 0; true; i++) {

random[i] = Math.round(Math.floor((Math.random() * 1)));
变成
random = Math.round(Math.floor((Math.random() * 1)));

out=new FileOutputStream("E:\\李聪\\"+random[i]+".txt");
变成
out=new FileOutputStream("E:\\李聪\\"+random+".txt");

这样应该能达到你的要求,
但这样最多生成1000个不同名的文件

如果你想要更多,那就扩大数的范围.

死循环可以写成for(;;)或者while(ture)!写成这两种的话在程序可以无限制生成TXT文件!但是由于操作系统的重名规则限制只能给每个文件名生成一个文件!所以你想要生成多少个文件就看你math.random()的取值范围了!
ps:你写的这段程序不管怎么看都是一个捣鬼程序

package com.cn.nst;
/**