import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
/**
* * 需求:随机生成10个1-100(包含100)之间的随机整数,把其中的奇数并且是3的倍数(可包含3)按从大到小排序(要求不能重复),
* 然后按照指定格式打印到当前工程项目下的num.txt文本中
* 例如:99, 81, 75, 57, 39, 33, 27, 21, 15, 3 (注意:最后一个数字没有逗号)
*/
class Demo01 {
public static void main(String[] args) {
Random random = new Random();
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new ArrayList<Integer>());
while (list.get(i).size() < 10) {
int num = random.nextInt(50) + 1;
if (num%2 == 0) {
list.get(i).add(num);
}
}
Collections.sort(list.get(i));
Collections.reverse(list.get(i));
}
//遍历嵌套集合
System.out.println("第一种遍历方式:");
for (int i = 0; i < list.size(); i++) {
System.out.print("(");
for (int j = 0; j < list.get(i).size(); j++) {
if (j == list.get(i).size() - 1) {
System.out.print(list.get(i).get(j) + ")");
}else {
System.out.print(list.get(i).get(j) + ",");
}
}
System.out.println();
}
System.out.println("第二种遍历方式:");
//使用增强for器遍历
for (ArrayList<Integer> arrayList : list) {
StringBuffer sb = new StringBuffer();
sb.append("(");
for (Integer i : arrayList) {
sb.append(i + ",");
}
sb.setCharAt(sb.length()-1, ')');
System.out.println(sb);
}
System.out.println("第三种遍历方式:");
//使用迭代器遍历
Iterator<ArrayList<Integer>> it1 = list.iterator();
while (it1.hasNext()) {
ArrayList<java.lang.Integer> arrayList = it1.next();
StringBuffer sb = new StringBuffer();
sb.append("(");
Iterator<Integer> it2 = arrayList.iterator();
while (it2.hasNext()) {
Integer integer = it2.next();
sb.append(integer + ",");
}
sb.setCharAt(sb.length() - 1, ')');
System.out.println(sb);
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo17 {
/**
* 把指定的文件切割成10M大小的小文件,最后一个可以不够10M
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹 (3)");
File file2 = new File("F:\\迅雷下载\\疯狂动物城.mp4");
FileInputStream fis = new FileInputStream(file2);
byte[] arr = new byte[1024*1024*10];
int len;
int i = 1;
while ((len = fis.read(arr)) != -1) {
FileOutputStream fos = new FileOutputStream(new File(file,"00" + (i++) + "_" + file2.getName()));
fos.write(arr, 0, len);
fos.close();
}
fis.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
public class Demo16_1 {
/**
* 题目三:把指定目录下(不包含子目录)的所有图片,复制到另一个指定目录下(使用文件过滤器来过滤图片)
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建文件路径对象
File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹 (2)");
//通过文件过滤器获取该文件夹路径的所有图片文件名
String[] arr = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir,name);
return file.isFile() && (file.toString().endsWith(".jpg") ||
file.toString().endsWith(".png") || file.toString().endsWith(".JPG") ||
file.toString().endsWith(".PNG"));
}
});
for (String name: arr) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(file, name)));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("F:\\新建文件夹",name)));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bos.close();
bis.close();
}
}
}
|