F:\txt有100个txt文件,java随机读取10个txt文件,然后将之重命名复制粘贴到D:\txt中。
F:\txt有100个txt文件,java随机读取10个txt文件,然后将之重命名复制粘贴到D:\txt中。重命名规律为1,2,3...10.txt。完整java代码可运...
F:\txt有100个txt文件,java随机读取10个txt文件,然后将之重命名复制粘贴到D:\txt中。重命名规律为1,2,3...10.txt。完整java代码可运行。合格后再额外奖励20分。
展开
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class CreateFiel
{
public static void main(String[] args) throws Exception
{
//才c盘的txt先生成100个文件,文件的内容都是 Hello world i
String path = "C:/txt/";
for(int i = 1; i <= 100; i++)
{
File file = new File(path + i + "qq.txt");
if(file.createNewFile())
{
FileWriter fw = new FileWriter(file);
fw.write("Hello world " + i);
fw.close();
}
}
//开始读取并且拷贝
File[] files = new File(path).listFiles();//全部的文件100个
if(files != null && files.length >= 99)
{
Random rd = new Random();
for(int i = 1; i <= 10; i++)
{
File f = files[rd.nextInt(100)];
BufferedReader fr = new BufferedReader(new FileReader(f));
BufferedWriter fw = new BufferedWriter(new FileWriter(new File("D:/tttt/" + i + ".txt")));
String line = null;
while((line = fr.readLine()) != null)
{
fw.write(line);
}
fw.close();
fr.close();
}
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class CreateFiel
{
public static void main(String[] args) throws Exception
{
//才c盘的txt先生成100个文件,文件的内容都是 Hello world i
String path = "C:/txt/";
for(int i = 1; i <= 100; i++)
{
File file = new File(path + i + "qq.txt");
if(file.createNewFile())
{
FileWriter fw = new FileWriter(file);
fw.write("Hello world " + i);
fw.close();
}
}
//开始读取并且拷贝
File[] files = new File(path).listFiles();//全部的文件100个
if(files != null && files.length >= 99)
{
Random rd = new Random();
for(int i = 1; i <= 10; i++)
{
File f = files[rd.nextInt(100)];
BufferedReader fr = new BufferedReader(new FileReader(f));
BufferedWriter fw = new BufferedWriter(new FileWriter(new File("D:/tttt/" + i + ".txt")));
String line = null;
while((line = fr.readLine()) != null)
{
fw.write(line);
}
fw.close();
fr.close();
}
}
}
}
展开全部
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 2013-7-12
* @author <a href="mailto:yupeiwendy@163.com">yupeiwen</a>
*
*/
public class RandomCopy {
public static void main(String[] args) throws IOException {
//取得F:/txt下所有文件
File txtFolder = new File("F:/txt");
File[] txt = txtFolder.listFiles();
//验证是否是100个
if(txt.length!=100){
System.out.println("F:/txt文件夹下的txt文件数量不是100个");
return;
}
File newtxtFolder = new File("D:/txt");
if(!newtxtFolder.exists())
newtxtFolder.mkdir();
//取得随机10个
List<Integer> li = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Integer j = (int)(Math.random()*100);
while(li.contains(j)){
j = (int)(Math.random()*100);
}
li.add(j);
}
Iterator<Integer> iterator = li.iterator();
int n = 1;
while(iterator.hasNext()){
File newtxt = new File(newtxtFolder.getAbsolutePath()+"\\"+n+".txt");
int x = iterator.next().intValue();
FileInputStream inputStream = new FileInputStream(txt[x]);
BufferedInputStream in = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream(newtxt);
BufferedOutputStream out = new BufferedOutputStream(outputStream);
int y;
while((y=in.read())!=-1){
out.write(y);
}
in.close();
out.close();
n++;
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 2013-7-12
* @author <a href="mailto:yupeiwendy@163.com">yupeiwen</a>
*
*/
public class RandomCopy {
public static void main(String[] args) throws IOException {
//取得F:/txt下所有文件
File txtFolder = new File("F:/txt");
File[] txt = txtFolder.listFiles();
//验证是否是100个
if(txt.length!=100){
System.out.println("F:/txt文件夹下的txt文件数量不是100个");
return;
}
File newtxtFolder = new File("D:/txt");
if(!newtxtFolder.exists())
newtxtFolder.mkdir();
//取得随机10个
List<Integer> li = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Integer j = (int)(Math.random()*100);
while(li.contains(j)){
j = (int)(Math.random()*100);
}
li.add(j);
}
Iterator<Integer> iterator = li.iterator();
int n = 1;
while(iterator.hasNext()){
File newtxt = new File(newtxtFolder.getAbsolutePath()+"\\"+n+".txt");
int x = iterator.next().intValue();
FileInputStream inputStream = new FileInputStream(txt[x]);
BufferedInputStream in = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream(newtxt);
BufferedOutputStream out = new BufferedOutputStream(outputStream);
int y;
while((y=in.read())!=-1){
out.write(y);
}
in.close();
out.close();
n++;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-11
展开全部
java.io.File
全部的文件名读到数组中,然后随机取数组的下标索引、取文件
全部的文件名读到数组中,然后随机取数组的下标索引、取文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |