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()+" "); } }}