复制单级文件夹 需求: 复制D:\course这文件夹到E:\course 分析:
- a: 封装D:\course为一个File对象
- b: 封装E:\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
- c: 获取a中的File对应的路径下所有的文件对应的File数组
- d: 遍历数组,获取每一个元素,进行复制
- e: 释放资源
public static void main(String[] args) {
copy("D:\\course", "D:\\copy");
System.out.println("共移动文件数目:" + size);
}
public static void copy(String sourcePath, String newPath) {
File file = new File(sourcePath);
if (file != null && file.exists()) {
String name = newPath + "/" + sourcePath.substring(sourcePath.lastIndexOf("/") + 1, sourcePath.length());
// 创建文件夹
File dir = new File(name);
if (!dir.exists()) {
dir.mkdir();
}
copyDir(sourcePath, name);
} else {
return;
}
}
private static int size;
private static void copyDir(String sourcePath, String newPath) {
File sourceFile = new File(sourcePath);
if (sourceFile.exists() && sourceFile != null) {// 文件存在
if (sourceFile.isFile()) {
copyFile(sourcePath, newPath);
System.out.println(sourcePath + " ---> " + newPath);
size++;
} else if (sourceFile.isDirectory()) {
// 创建文件夹
File dir = new File(newPath);
if (!dir.exists()) {
dir.mkdir();
}
// 获取文件夹内部的文件
// 递归调用
for (File con : sourceFile.listFiles()) {
copyDir(sourcePath + "/" + con.getName(), newPath + "/" + con.getName());
// System.out.println(sourcePath + "/" + con.getName() + "-----" + newPath + "/" + con.getName());
}
}
} else {
return;
}
}
private static void copyFile(String soucePath, String newPath) {
// 1、确定源
File sourceFile = new File(soucePath);
File newFile = new File(newPath);
// 2、确定流
InputStream fin = null;
OutputStream fout = null;
try {
fin = new FileInputStream(sourceFile);
fout = new FileOutputStream(newFile);
// 3、确定操作
byte[] flush = new byte[1024];
int len = -1;
while ((len = fin.read(flush)) != -1) {
fout.write(flush, 0, len);
}
// 清空缓冲区
fout.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4、关闭流,先打开的后关闭
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
删除多级目录
public class MyTest {
public static void main(String[] args) {
//删除多级目录
File file = new File("C:\\Users\\Desktop\\demo");
//递归删除
deleteFolder(file);
}
private static void deleteFolder(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()) {
f.delete();
}else{
//递归
deleteFolder(f);
}
}
//删除空文件夹
file.delete();
}
}
修改文件的后缀名
public class MyTest2 {
public static void main(String[] args) {
//修改文件的后缀名
File file = new File("C:\\Users\\Desktop\\demo");
//递归修改
updateFiles(file);
}
private static void updateFiles(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile() && f.getName().endsWith(".png")) {
//获取父路径
String parent = f.getParent();
//获取文件名
String name = f.getName();
//修改文件名
name = name.substring(0, name.lastIndexOf(".")) + ".jpg";
//构建目标文件,跟源文件目标保持一致
File targetFile = new File(parent, name);
f.renameTo(targetFile);
} else {
//递归 传过去的文件夹里面,会有文件夹和文件
updateFiles(f);
}
}
}
}
}
录入学生成绩到文件中
测试类
public class MyTest {
public static void main(String[] args) throws IOException {
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getTotalScore() - s2.getTotalScore();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return -num2;
}
});
for (int i = 1; i <= 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第" + i + "个学生的姓名");
String name = sc.nextLine();
System.out.println("请输入第" + i + "个学生的语文成绩");
int yw = sc.nextInt();
System.out.println("请输入第" + i + "个学生的数学成绩");
int sx = sc.nextInt();
System.out.println("请输入第" + i + "个学生的英语成绩");
int yy = sc.nextInt();
Student student = new Student(name, yw, sx, yy);
treeSet.add(student);
}
String fileName = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(new Date(System.currentTimeMillis()));
fileName=fileName+"-score.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
String title = "序号\t姓名\t语文\t数学\t英语\t总分";
System.out.println(title);
writer.write(title);
writer.newLine();
writer.flush();
int i = 0;
for (Student student : treeSet) {
i++;
String name = student.getName();
int chineseScore = student.getChineseScore();
int mathScore = student.getMathScore();
int englishScore = student.getEnglishScore();
int totalScore = student.getTotalScore();
System.out.println(i + "\t" + name + "\t" + chineseScore + "\t" + mathScore + "\t" + englishScore + "\t" + totalScore);
writer.write(i + "\t" + name + "\t" + chineseScore + "\t" + mathScore + "\t" + englishScore + "\t" + totalScore);
writer.newLine();
writer.flush();
}
writer.close();
System.out.println("录入完毕");
}
}
学生类
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student() {
}
public Student(String name, int chineseScore, int mathScore, int englishScore) {
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getTotalScore(){
return chineseScore+mathScore+englishScore;
}
}
把一个文件复制多份
public class MyTest {
public static void main(String[] args) throws IOException {
//把一个文件复制多份
RandomAccessFile in = new RandomAccessFile("C:\\Users\\Desktop\\mv.jpg","rw");
byte[] bytes = new byte[1024 * 8];
int len=0;
for (int i = 1; i <=10; i++) {
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Desktop\\", i + "mv.jpg"));
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
out.close();
in.seek(0);
}
in.close();
}
}
把文件拆分成 1M 1份,最后一份有多少算多少
public class MyTest3 {
public static void main(String[] args) throws IOException {
//chaiFen();
File file = new File("C:\\Users\\ShenMouMou\\Desktop\\music");
File[] files = file.listFiles();
Vector<FileInputStream> vector = new Vector<>();
for (File f : files) {
FileInputStream minIn = new FileInputStream(f);
vector.add(minIn);
}
System.out.println(vector);
Enumeration<FileInputStream> elements = vector.elements();
SequenceInputStream in = new SequenceInputStream(elements);
FileOutputStream out = new FileOutputStream(new File(file, "人生.mp3"));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
//删除零碎的文件
File[] files2 = file.listFiles();
for (File f1: files2) {
if(f1.length()<=1024*1024&&f1.getName().endsWith(".mp3")){
f1.delete();
}
}
}
private static void chaiFen() throws IOException {
//把文件拆分成 1M 1份,最后一份有多少算多少
FileInputStream in = new FileInputStream("人生.mp3");
File file = new File("C:\\Users\\ShenMouMou\\Desktop\\music");
if (!file.exists()) {
file.mkdirs();
}
byte[] bytes = new byte[1024 * 1024];
int len = 0;
int i = 1;
while ((len = in.read(bytes)) != -1) {
FileOutputStream out = new FileOutputStream(new File(file, (i++) + ".mp3"));
out.write(bytes, 0, len);
out.close();
}
in.close();
}
}
有一个配置文件,数据是键=值形式,判断有没有lisi这个键,如果有把,值改成100
public class MyTest2 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream("user.properties"));
if (properties.containsKey("lisi")) {
properties.setProperty("lisi","王五");
properties.store(new FileWriter("user.properties"),null);
}else{
System.out.println("没有这个键");
}
}
}
|