文件的改名和复制,删除

文件改名

一个:
public class YanShiDeno {
public static void main(String[] args) {
File file = new File(“C:\Users\Administrator\Desktop\wsds2d\aaa1.png”);
file.renameTo(new File(file.getParentFile()+"\"+“bbb.jpg”));//改变了源文件路径,并重命名或者不改变源文件路径只重命名

}

}
一级目录下多个文件改名
package ykofdjijide;

import java.io.File;
import java.util.Random;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) {
    File file = new File(“C:\Users\Administrator\Desktop\wsds2d”);
    File[] files = file.listFiles();//罗列出此目录下的所有文件
    for (File file1 : files) { //遍历这个目录下的文件
    file1.renameTo(new File(file1.getParentFile(),“bbbb”+(new Random().nextInt(10)) + “.png”));
    }

    }
    }
    多级选择性改名:
    package ykofdjijide;

import eydgyyge.Gaiming;

import java.io.File;
import java.util.Random;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) {
    File file = new File(“C:\Users\Administrator\Desktop\wsds2d”);
    gaiming(file);

    }

    private static void gaiming(File file) {
    File[] files = file.listFiles();
    for (File file1 : files) {
    if (file1.isFile()&&file1.getName().endsWith(".jpg")){//是文件而且文件名中含有".jpg"
    file1.renameTo(new File(file1.getParentFile()+"\"+“aaaa”+(new Random().nextInt(10))+“png”));
    }else if(!file1.isFile()){//文件夹
    gaiming(file1);//递归再次罗列此文件夹
    }
    }
    }
    }
    多级选择删除
    package ykofdjijide;

import java.io.File;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) {
    File file = new File(“C:\Users\Administrator\Desktop\wsds2d”);
    gaiming(file);

    }

    private static void gaiming(File file) {
    File[] files = file.listFiles();
    for (File file1 : files) {
    if (file1.isFile()&&file1.getName().endsWith(“png”)){//是文件而且文件名中含有".jpg"
    file1.delete(); //删除文件
    }else if(!file1.isFile()){//文件夹
    gaiming(file1);//递归再次罗列此文件夹
    }
    }
    }
    }

Io流.复制

复制一个文件:
package ykofdjijide;

import java.io.*;

/**

  • @Author: Administrator
  • @CreateTime: 2019-01-17 14:35
    /
    public class YanShiDeno {
    public static void main(String[] args) throws IOException {
    FileInputStream in = new FileInputStream(“C:\Users\Administrator\Desktop\wsds2d - 副本\月半小夜曲 (Live)-中国好声音.mp3”);
    FileOutputStream out = new FileOutputStream(new File(“D:\月半小夜曲 (Live)-中国好声音.mp3”));
    byte[] bytes = new byte[1024
    8];//缓存区
    int num=0;
    while ((num=in.read(bytes))!=-1){
    out.write(bytes,0,num);
    out.flush();
    }
    in.close();
    out.close();
    }
    }
    复制一级目录文件
    package ykofdjijide;

import java.io.*;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) throws IOException {
    File jiu = new File(“C:\Users\Administrator\Desktop\wsds2d”);
    File xin = new File(“D:\wsds2d”);
    if(!xin.exists()){//判断有没有目标文件夹
    xin.mkdirs();//没有则新建
    }
    bianli(jiu, xin);
    }

    private static void bianli(File jiu, File xin) throws IOException {
    File[] files = jiu.listFiles();
    for (File file : files) {
    fuzhi(file, xin);
    }
    }

    private static void fuzhi(File file, File xin) throws IOException {
    FileInputStream in = new FileInputStream(file);
    FileOutputStream out = new FileOutputStream(new File(xin, file.getName()));
    byte[] bytes = new byte[1024 * 8];
    int num=0;
    while ((num=in.read(bytes))!=-1){
    out.write(bytes,0,num);
    out.flush();
    }
    in.close();
    out.close();
    }
    }
    复制多级文件夹
    package ykofdjijide;

import java.io.*;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) throws IOException {
    File jiu = new File(“C:\Users\Public\Desktop\激活.驱动”);
    File xin = new File(“D:\激活.驱动”);
    if(!xin.exists()){//判断有没有目标文件夹
    xin.mkdirs();//没有则新建
    }
    bianli(jiu, xin);
    }

    private static void bianli(File jiu, File xin) throws IOException {
    File[] files = jiu.listFiles();
    for (File file : files) {
    if (file.isFile()){
    fuzhi(file, xin);
    }
    else {
    File file1 = new File(xin,file.getName());
    if (!file1.exists()) {//判断有没有目标文件夹
    file1.mkdirs();//没有则新建
    }
    bianli(file,file1);
    }
    }
    }

    private static void fuzhi(File file, File xin) throws IOException {
    FileInputStream in = new FileInputStream(file);
    FileOutputStream out = new FileOutputStream(new File(xin, file.getName()));
    byte[] bytes = new byte[1024 * 8];
    int num=0;
    while ((num=in.read(bytes))!=-1){
    out.write(bytes,0,num);
    out.flush();
    }
    in.close();
    out.close();
    }
    }
    复制过程中选择性改名
    package ykofdjijide;

import java.io.*;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-17 14:35
    */
    public class YanShiDeno {
    public static void main(String[] args) throws IOException {
    File jiu = new File(“C:\Users\Administrator\Desktop\wsds2d - 副本”);
    File xin = new File(“D:\wsds2d - 副本”);
    if(!xin.exists()){//判断有没有目标文件夹
    xin.mkdirs();//没有则新建
    }
    bianli(jiu, xin);
    }

    private static void bianli(File jiu, File xin) throws IOException {
    File[] files = jiu.listFiles();
    for (File file : files) {
    if (file.isFile()){
    fuzhi(file, xin);
    }
    else {
    File file1 = new File(xin,file.getName());
    if (!file1.exists()) {//判断有没有目标文件夹
    file1.mkdirs();//没有则新建
    }
    bianli(file,file1);
    }
    }
    }

    private static void fuzhi(File file, File xin) throws IOException {
    FileInputStream in = new FileInputStream(file);
    File file1 = new File(xin, file.getName());
    FileOutputStream out = new FileOutputStream(file1);
    byte[] bytes = new byte[1024 * 8];
    int num=0;
    while ((num=in.read(bytes))!=-1){
    out.write(bytes,0,num);
    out.flush();
    }
    in.close();
    out.close();
    faiming(file1);//抽取一个改名方法
    }

    private static void faiming(File file1) {
    if(file1.getName().endsWith(".jpg")){
    file1.renameTo(new File(file1.getParentFile()+"\"+file1.getName().replace(".jpg",".png")));
    }
    }
    }
    集合中的文本文件复制到txt文件中
    package gdeyuuw;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**

  • @Author: Administrator

  • @CreateTime: 2019-01-16 12:48
    */
    public class FuZhi {
    public static void main(String[] args) throws IOException {
    TreeSet jihe = new TreeSet<>(new Comparator() {
    @Override
    public int compare(Student s1, Student s2) {
    int num=s1.zongFen()-s2.zongFen();
    int num2=num==0?s1.getName().compareTo(s2.getName()):num;
    return num2;
    }
    });
    for (int i = 0; i < 3; i++) {

         Scanner sc = new Scanner(System.in);
         System.out.println("请输入姓名");
         String s = sc.nextLine();
         System.out.println("请输入数学成绩");
         Integer shux = new Integer(sc.nextLine());
         System.out.println("请输入语文成绩");
         Integer yuwen = new Integer(sc.nextLine());
         System.out.println("请输入英语成绩");
         Integer yingyu = new Integer(sc.nextLine());
         jihe.add(new Student(s,yuwen,shux,yingyu));
     }
     BufferedWriter out = new BufferedWriter(new FileWriter("a.txt"));
     out.write("姓名\t"+"语文成绩\t"+"数学\t"+"英语\t"+"总分");
     out.flush();
     out.newLine();
     for (Student student : jihe) {
         out.write(student.getName()+"\t"+student.getYuwen()+"\t"+student.getShux()+"\t"+student.getYingyu()+"\t"+student.zongFen());
         out.flush();
         out.newLine();
    
     }
     out.close();
    

    }

}

おすすめ

転載: blog.csdn.net/weixin_43791033/article/details/86526882
おすすめ