java 打印流 递归复制子文件子文件夹 不同编码文件复制到同一文件中 序列化流反序列化流

package com.swift.jinjie;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;

/*从键盘输入一个文件夹路径,利用打印流将该文件夹下的所有文件(包括子文件夹)复制到D盘下temp文件夹下。*/

public class PrintAllToFile {

    public static void main(String[] args) throws IOException {

        File srcDir=new File("d:/abc");
        File destDir=new File("d:/temp");
        printAllFile(srcDir,destDir);
        System.out.println("包括子文件夹复制成功");
    }

    private static void printAllFile(File srcDir, File destDir) throws IOException {

        
        if(!destDir.exists()) {
            destDir.mkdirs();
        }
        File[] files=srcDir.listFiles();
        for(File file:files) {
            if(file.isDirectory()) {
                File destDirNew=new File(destDir,file.getName());
                printAllFile(file,destDirNew);
            }else {
                File destFile=new File(destDir,file.getName());
                BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
                PrintStream ps=new PrintStream(destFile);
                int len;
                byte[] buf=new byte[1024];
                while((len=bis.read(buf))!=-1) {
                    ps.write(buf, 0, len);
                }
                
                
            }
        }
    }

}

 不同编码文件复制到同一文件

package com.swift.jinjie;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*d:/abc盘下有两个文本文件,分别为a.txt和b.txt,其中a.txt编码方式是gbk,而b.txt的编码方式是utf8。要求将使用转换流实现如下功能:
将a.txt和b.txt文件的内容复制到c.txt文件中,保证内容不乱码。*/


public class MergeTxt {

    public static void main(String[] args) throws IOException, FileNotFoundException {

        File file1=new File("d:/abc/a.txt");
        File file2=new File("d:/abc/b.txt");
        File destFile=new File("d:/abc/c.txt");
        mergeFile(file1,file2,destFile);
    }

    private static void mergeFile(File file1, File file2,File destFile) throws IOException {

        InputStreamReader isr1=new InputStreamReader(new FileInputStream(file1),"gbk");
        InputStreamReader isr2=new InputStreamReader(new FileInputStream(file2),"utf-8");
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(destFile,true),"utf-8");
        int len;
        char[] buf=new char[1024];
        while((len=isr1.read(buf))!=-1) {
            osw.write(buf, 0, len);
            osw.flush();
        }
        while((len=isr2.read(buf))!=-1) {
            osw.write(buf, 0, len);
            osw.flush();
        }
        osw.close();
        isr1.close();
        isr2.close();
        System.out.println("文件复制成功");
    }

}

序列化流反序列化流

package com.swift.jinjie;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

import com.swift.baseKnowledge.Student;

/*有学生类包含学号,姓名,省份证号,Java成绩,数学成绩,英语成绩等成员变量,提供构造方法和setter和getter方法。
要求:
    * 学生信息及成绩保存到C盘的save.txt文件中
    * 学生身份证号码不能保存到文件中。
    * 程序运行时如果save.txt不存在,则
从键盘录入1个学生信息,信息录入格式如下:
    ***** 录入学生信息 *****
    请输入学号:9527
    请输入姓名:华安
    请输入身份证号:2203919831234543
    请输入Java成绩:90
    请输入数学成绩:80
    请输入英语成绩:88

    根据录入的信息创建学生对象并将学生对象保存到C盘下的save.txt文件中。
    * 如果程序运行时,save.txt文件已经存在,则显示学生信息。格式如下:
    **** 学生基本信息 *****
    学号  姓名 省份证号  Java成绩  数学成绩  英语成绩
    9527  华安   null      90        80        88*/


public class Fanxuliehua {

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

        inputInfo();
    }

    private static void inputInfo() throws FileNotFoundException, IOException, ClassNotFoundException {

        Scanner scan=new Scanner(System.in);
        for(int i=0;i<1;i++) {
            System.out.print("请输入学号:");
            String id=scan.next();
            Student student=new Student();
            student.setId(id);
            System.out.println("请输入姓名:");
            String name=scan.next();
            student.setName(name);
            System.out.println("请输入身份证号:");
            String uid=scan.next();
            student.setUid(uid);
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("save.txt"));
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream("save.txt"));
            oos.writeObject(student);
            Student stu=(Student)ois.readObject();
            System.out.println(stu);
            System.out.println("学号    姓名           身份证号             数学成绩     语文成绩     总分");
            System.out.println(stu.getId()+"  "+stu.getName()+"  "+stu.getUid()+"       "+stu.getShuxue()+"     ");
            
        }
    }

}
原文地址:https://www.cnblogs.com/qingyundian/p/8527522.html