java之实现文件剪切操作 (输入输出流)

java 关于文件剪切的操作代码

方法一

package Onenine;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s =in.next();
        String str1 =in.next();
        String str2 = in.next();
        File f1 = new File(s+"\\"+str1);
        File f2 = new File(str2+"\\"+str1);
        BufferedReader reader = null;
        BufferedWriter writer = null;
        if(f1.exists()) {
            try {
                reader = new BufferedReader(new FileReader(f1));
                writer = new BufferedWriter(new FileWriter(f2));
                String t;
                while((t=reader.readLine())!=null){
                    writer.write(t);
                    writer.newLine();
                    writer.flush();
                }
            }catch(FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try{
                    writer.close();
                    reader.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        if(f1.delete()) {
            System.out.println("剪切成功");
        }else {
            System.out.println("剪切失败");
        }
    }
}

方法二:

package Onenine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

//功能 实现剪切
public class Cut {
    public static void cutFile(File file1, File file2) {
        FileOutputStream outputStream = null;
        InputStream inputStream = null;
        byte[] bytes = new byte[1024];
        int temp = 0;
        try {
            inputStream = new FileInputStream(file1);
            outputStream = new FileOutputStream(file2);
            while ((temp = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, temp);
                outputStream.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//实现测试类

package Onenine;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s =in.next();
        String str1 =in.next();
        String str2 = in.next();
        System.out.println(s+"\\"+str1);
        File file1 = new File(s+"\\"+str1);
        File file2 = new File(str2+"\\"+str1);
        file1.deleteOnExit();
        try {
            file2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Cut f1 = new Cut();
        f1.cutFile(file1, file2);
    }
}

你可能感兴趣的:(Java基础,java剪切)