java 列印流 遞迴複製子檔案子檔案夾 不同編碼檔案複製到同一檔案中 序列化流還原序列化流,java序列化

來源:互聯網
上載者:User

java 列印流 遞迴複製子檔案子檔案夾 不同編碼檔案複製到同一檔案中 序列化流還原序列化流,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()+"     ");                    }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.