今天是周二,现在刚上班半个小时,但是感觉好困啊。下午和立伟聊了一会儿天,他现在倒是爽得很,每天在打LOL,好像我每天也在用LOL的内容写练习题哈。别看这小子考研的时候没那么用功,考的倒是很高,人家还是挺厉害的。晚上看到了姚伊杨的朋友圈,他是不是去复旦的夏令营了呢,感觉这小子也挺狠啊,复读到了中南,还是这个平台有用啊,本科985硕士去哪里都不算吃亏。有点小酸,嘿嘿嘿。不说别的了,等会儿和杰哥聊聊天就睡觉了,明天继续加油!
package homework;
/**
* 一般说来操作系统都会安装在C盘,所以会有一个 C:\WINDOWS目录。
*
* 遍历这个目录下所有的文件(不用遍历子目录)
*
* 找出这些文件里,最大的和最小(非0)的那个文件,打印出他们的文件名
*/
import java.io.File;
public class Test {
public static void main(String[] args) {
File f = new File("c:\\windows");
File[] fs = f.listFiles();
if (fs == null){
return;
}
long minSize = Integer.MAX_VALUE;
long maxSize = 0;
File minFile = null;
File maxFile = null;
for (int i = 0;i < fs.length;i++){
long length = fs[i].length();
if (fs[i].isDirectory()){
continue;
}
if (length > maxSize){
maxSize = length;
maxFile = fs[i];
}
if (length != 0 || length < minSi***Size = length;
minFile = fs[i];
}
}
System.out.printf("最大的文件是%s,其大小是%,d字节%n",maxFile.getAbsoluteFile(),maxFile.length());
System.out.printf("最小的文件是%s,其大小是%,d字节%n",minFile.getAbsoluteFile(),minFile.length());
}
}
package homework;
import java.io.File;
/**
* 同上,遍历子目录
*/
public class Test {
static long minSize = Integer.MAX_VALUE;
static long maxSize = 0;
static File minFile = null;
static File maxFile = null;
//采用递归的方法来遍历子目录
private static void listFiles(File file){
if(file.isFile()){
if(file.length() > maxSize){
maxSize = file.length();
maxFile = file;
}
if(file.length() != 0 && file.length() < minSi***Size = file.length();
minFile = file;
}
}
if (file.isDirectory()){
File[] fs = file.listFiles();
if(fs != null){
for (int i = 0;i < fs.length;i++) {
listFiles(fs[i]);
}
}
}
}
public static void main(String[] args) {
File f = new File("c:\\windows");
listFiles(f);
System.out.printf("最大的文件是%s,其大小是%,d字节%n",maxFile.getAbsoluteFile(),maxFile.length());
System.out.printf("最小的文件是%s,其大小是%,d字节%n",minFile.getAbsoluteFile(),minFile.length());
}
}package homework;
import java.io.*;
public class Test {
public static void main(String[] args) {
File file = new File("D:\\xyz\\lol.txt");
File dir = file.getParentFile();
if (!dir.exists()){
dir.mkdirs();
}
FileOutputStream fis = null;
try {
fis = new FileOutputStream(file);
//创建字节数组,其长度就是文件的长度
byte[] data = {99,100};
//以字节流的形式读取文件所有内容
fis.write(data);
//使用完应该关闭
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}package homework;
import java.io.*;
/**
* 以字节流的形式向文件写入数据 中的例子,当lol2.txt不存在的时候,是会自动创建lol2.txt文件的。
* 但是,如果是写入数据到d:/xyz/lol2.txt,而目录xyz又不存在的话,就会抛出异常。
* 那么怎么自动创建xyz目录?
* 如果是多层目录 d:/xyz/abc/def/lol2.txt 呢?
*/
public class Test {
public static void main(String[] args) {
File file = new File("d:/xyz/abc/def/lol2.txt");
File dir = file.getParentFile();
if (!dir.exists()){
dir.mkdirs();
}
FileOutputStream fis = null;
try {
fis = new FileOutputStream(file);
//创建字节数组,其长度就是文件的长度
byte[] data = {99,100};
//以字节流的形式读取文件所有内容
fis.write(data);
//使用完应该关闭
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}package homework;
import java.io.*;
import java.util.Arrays;
/**
* 找到一个大于100k的文件,按照100k为单位,拆分成多个子文件,并且以编号作为文件名结束。
* 比如文件 eclipse.exe,大小是309k。
* 拆分之后,成为
* eclipse.exe-0
* eclipse.exe-1
* eclipse.exe-2
* eclipse.exe-3
* 拆分的思路,先把源文件的所有内容读取到内存中,然后从内存中挨个分到子文件里
* srcFile 要拆分的源文件,按照这个大小,拆分
*/
public class Test {
public static void main(String[] args) {
int eachSize = 100 * 1024; // 每个部分为100k,102400字节
File srcFile = new File("C:\\Users\\Administrator\\Downloads\\corejava10.zip");
splitFile(srcFile, eachSize);
}
public static void splitFile(File srcFile, int eachSize) {
if (srcFile.length() == 0) {
//抛出异常
throw new RuntimeException("文件长度为0,不可拆分");
}
//定义一个byte数组存放源文件内容
byte[] fileContent = new byte[(int) srcFile.length()];
// 先把文件读取到数组中
try {
FileInputStream fis = new FileInputStream(srcFile);
fis.read(fileContent);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 计算需要被划分成多少份子文件
int fileNumber = 0;
if (srcFile.length() % eachSize == 0) {
fileNumber = (int) srcFile.length() / eachSize;
}
if (srcFile.length() % eachSize != 0) {
fileNumber = (int) srcFile.length() / eachSize + 1;
}
for (int i = 0; i < fileNumber; i++) {
String eachFileName = srcFile.getName() + "-" + i;
File eachFile = new File(srcFile.getParent(), eachFileName);
//定义一个byte数组,放置每一部分
byte[] eachContent = new byte[0];
//如果不是最后一个部分
if (i != fileNumber - 1) {
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, eachSize * (i + 1));
}
if (i == fileNumber - 1) {
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, fileContent.length);
}
try {
// 写出去
FileOutputStream fos = new FileOutputStream(eachFile);
fos.write(eachContent);
// 记得关闭
fos.close();
System.out.printf("输出子文件%s,其大小是 %,d字节%n", eachFile.getAbsoluteFile(), eachFile.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}package homework;
/**
* 合并的思路,就是从eclipse.exe-0开始,读取到一个文件,就开始写出到 eclipse.exe 中,直到没有文件可以读
* @param folder
* 需要合并的文件所处于的目录
* @param fileName
* 需要合并的文件的名称
* @throws FileNotFoundException
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
murgeFile("C:\\Users\\Administrator\\Downloads","corejava10.zip");
}
private static void murgeFile(String folder, String fileName) {
try {
//定义要整合的文件
File destFile = new File(folder, fileName);
//定义文件输出流
FileOutputStream fos = new FileOutputStream(destFile);
int index = 0;
while (true) {
//定义子文件
File eachFile = new File(folder, fileName + "-" + index++);
//子文件不存在就结束掉
if (!eachFile.exists()){
break;
}
//读取每一个子文件的内容
FileInputStream fis = new FileInputStream(eachFile);
//定义byte数组存放子文件
byte[] eachContent = new byte[(int) eachFile.length()];
//以字节流的形式读取文件所有内容
fis.read(eachContent);
fis.close();
//把子文件的内容写出去
fos.write(eachContent);
//清空缓存
fos.flush();
System.out.printf("把子文件 %s写出到目标文件中%n",eachFile);
}
fos.close();
System.out.printf("最后目标文件的大小:%,d字节" , destFile.length());
} catch (Exception e) {
e.printStackTrace();
}
}
}package homework;
import java.io.*;
/**
* 标准的关闭流的方式
* 1. 首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally.
* 2. 在finally关闭之前,要先判断该引用是否为空
* 3. 关闭的时候,需要再一次进行try catch处理
*/
public class Test {
public static void main(String[] args) {
File file = new File("D:\\xyz\\lol.txt");
//首先把流的引用声明在try的外面
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] all = new byte[(int)file.length()];
//以字节流的形式读取文件内容,其实就是把文件字节内容存放在了数组中,
//然后遍历数组就ok
fis.read(all);
for (byte b : all) {
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 在finally 里关闭流
if (fis != null){
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}package homework;
import java.io.*;
/**
* 把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
* 这种编写代码的方式叫做 try-with-resources, 这是从JDK7开始支持的技术
*
* 所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。
* 并且在try, catch, finally结束的时候自动关闭,回收相关资源。
*/
public class Test {
public static void main(String[] args) {
File file = new File("D:\\xyz\\lol.txt");
//把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
try(FileInputStream fis = new FileInputStream(file)) {
byte[] all = new byte[(int) file.length()];
fis.read(all);
for (byte b : all) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}package homework;
import java.io.*;
import java.util.Arrays;
/**
* 更改为在finally中关闭流的方式
* 找到一个大于100k的文件,按照100k为单位,拆分成多个子文件,并且以编号作为文件名结束。
* 比如文件 eclipse.exe,大小是309k。
* 拆分之后,成为
* eclipse.exe-0
* eclipse.exe-1
* eclipse.exe-2
* eclipse.exe-3
* 拆分的思路,先把源文件的所有内容读取到内存中,然后从内存中挨个分到子文件里
* srcFile 要拆分的源文件,按照这个大小,拆分
*/
public class Test {
public static void main(String[] args) {
int eachSize = 100 * 1024; // 每个部分为100k,102400字节
File srcFile = new File("C:\\Users\\Administrator\\Downloads\\corejava10.zip");
splitFile(srcFile, eachSize);
}
public static void splitFile(File srcFile, int eachSize) {
if (srcFile.length() == 0) {
//抛出异常,直接用throw就可以了
throw new RuntimeException("文件长度为0,不可拆分");
}
//定义一个byte数组存放源文件内容
byte[] fileContent = new byte[(int) srcFile.length()];
FileInputStream fis = null;
// 先把文件读取到数组中,采用在finally中关闭流的方式
try {
fis = new FileInputStream(srcFile);
fis.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 计算需要被划分成多少份子文件
int fileNumber = 0;
if (srcFile.length() % eachSize == 0) {
fileNumber = (int) srcFile.length() / eachSize;
}
if (srcFile.length() % eachSize != 0) {
fileNumber = (int) srcFile.length() / eachSize + 1;
}
for (int i = 0; i < fileNumber; i++) {
String eachFileName = srcFile.getName() + "-" + i;
File eachFile = new File(srcFile.getParent(), eachFileName);
//定义一个byte数组,放置每一部分
byte[] eachContent = new byte[0];
//如果不是最后一个部分
if (i != fileNumber - 1) {
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, eachSize * (i + 1));
}
if (i == fileNumber - 1) {
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, fileContent.length);
}
FileOutputStream fos = null;
try {
// 写出去
fos = new FileOutputStream(eachFile);
fos.write(eachContent);
System.out.printf("输出子文件%s,其大小是 %,d字节%n", eachFile.getAbsoluteFile(), eachFile.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}package homework;
/**
* 修改成 try() 方式关闭流
* 合并的思路,就是从eclipse.exe-0开始,读取到一个文件,就开始写出到 eclipse.exe 中,直到没有文件可以读
* @param folder
* 需要合并的文件所处于的目录
* @param fileName
* 需要合并的文件的名称
* @throws FileNotFoundException
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) {
murgeFile("C:\\Users\\Administrator\\Downloads","corejava10.zip");
}
private static void murgeFile(String folder, String fileName) {
//定义要整合的文件,定义文件输出流
File destFile = new File(folder, fileName);
try(FileOutputStream fos = new FileOutputStream(destFile)) {
int index = 0;
while (true) {
//定义子文件
File eachFile = new File(folder, fileName + "-" + index++);
//子文件不存在就结束掉
if (!eachFile.exists()){
break;
}
//定义byte数组存放子文件
byte[] eachContent = new byte[(int) eachFile.length()];
//读取每一个子文件的内容
try(FileInputStream fis = new FileInputStream(eachFile)) {
//以字节流的形式读取文件所有内容
fis.read(eachContent);
}
//把子文件的内容写出去
fos.write(eachContent);
//清空缓存
fos.flush();
System.out.printf("把子文件 %s写出到目标文件中%n",eachFile);
}
System.out.printf("最后目标文件的大小:%,d字节" , destFile.length());
} catch (Exception e) {
e.printStackTrace();
}
}
}
京公网安备 11010502036488号