Java print Stream recursively copies sub-files subfolders different encoded files are copied to the same file in the serialized stream deserialization stream

Source: Internet
Author: User
Tags file copy

 PackageCom.swift.jinjie;ImportJava.io.BufferedInputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.PrintStream;/*Enter a folder path from the keyboard, and use the print stream to copy all files (including subfolders) under the folder to the Temp folder under the D drive. */ Public classPrintalltofile { Public Static voidMain (string[] args)throwsIOException {File srcdir=NewFile ("D:/abc"); File DestDir=NewFile ("D:/temp");        Printallfile (Srcdir,destdir); System.out.println ("Include sub-folders replication succeeded"); }    Private Static voidPrintallfile (file Srcdir, file DestDir)throwsIOException {if(!destdir.exists ())        {Destdir.mkdirs (); } file[] Files=Srcdir.listfiles ();  for(File file:files) {if(File.isdirectory ()) {file Destdirnew=NewFile (Destdir,file.getname ());            Printallfile (file,destdirnew); }Else{File DestFile=NewFile (Destdir,file.getname ()); Bufferedinputstream bis=NewBufferedinputstream (Newfileinputstream (file)); PrintStream PS=NewPrintStream (destfile); intLen; byte[] buf=New byte[1024];  while((Len=bis.read (BUF))!=-1) {ps.write (buf,0, Len); }                                            }        }    }}

Copying different encoded files to the same file

 PackageCom.swift.jinjie;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;/*D:/ABC disk has two text files, respectively, A.txt and B.txt, wherein a.txt encoding is GBK, and b.txt encoding method is UTF8. It is required to use the conversion stream to implement the following functions: Copy the contents of the A.txt and B.txt files to the C.txt file to ensure that the content is not garbled. */ Public classMergetxt { Public Static voidMain (string[] args)throwsIOException, FileNotFoundException {File file1=NewFile ("D:/abc/a.txt"); File file2=NewFile ("D:/abc/b.txt"); File DestFile=NewFile ("D:/abc/c.txt");    Mergefile (File1,file2,destfile); }    Private Static voidMergefile (file file1, file File2,file destfile)throwsIOException {inputstreamreader ISR1=NewInputStreamReader (NewFileInputStream (File1), "GBK"); InputStreamReader ISR2=NewInputStreamReader (NewFileInputStream (File2), "Utf-8"); OutputStreamWriter OSW=NewOutputStreamWriter (NewFileOutputStream (DestFile,true), "Utf-8"); intLen; 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 ("File copy succeeded"); }}

Serialized Stream Deserialization Stream

 PackageCom.swift.jinjie;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.util.Scanner;Importcom.swift.baseKnowledge.Student;/*There are student classes including number, name, province certificate number, Java score, math score, English score and other member variables, providing the construction method and setter and getter method.    Requirements: * Student information and results are saved to the Save.txt file in the C drive * The student ID number cannot be saved to the file.    * When the program runs if Save.txt does not exist, then input 1 student information from the keyboard, the information input format is as follows: * * * * * * * * * * * * * * * * * * Please enter the student number: 9527 Please enter your name: Huaan Please enter your ID number: 2203919831234543    Please input Java score: 90 Please enter Math score: 80 Please input English score: 88 based on the information entered, create student objects and save the student object to the Save.txt file under the C drive. * If the Save.txt file already exists when the program is running, the student information is displayed. The format is as follows: * * * students ' basic INFORMATION * * * * Student number Name Province certificate number Java score Math score 9527 Huaan null*/ Public classFanxuliehua { Public Static voidMain (string[] args)throwsFileNotFoundException, IOException, classnotfoundexception {inputinfo (); }    Private Static voidInputinfo ()throwsFileNotFoundException, IOException, classnotfoundexception {Scanner scan=NewScanner (system.in);  for(inti=0;i<1;i++) {System.out.print ("Please enter the study number:"); String ID=Scan.next (); Student Student=NewStudent ();            Student.setid (ID); System.out.println ("Please enter your name:"); String name=Scan.next ();            Student.setname (name); System.out.println ("Please enter your ID number:"); String UID=Scan.next ();            Student.setuid (UID); ObjectOutputStream Oos=NewObjectOutputStream (NewFileOutputStream ("Save.txt")); ObjectInputStream Ois=NewObjectInputStream (NewFileInputStream ("Save.txt"));            Oos.writeobject (student); Student Stu=(Student) ois.readobject ();            System.out.println (Stu); System.out.println ("Study number name ID card number Mathematics results Chinese score"); System.out.println (Stu.getid ()+ "" "+stu.getname () +" "+stu.getuid () +" "+stu.getshuxue () +" "); }    }}

Java Print Stream recursively copies sub-files subfolders different encoded files are copied to the same file in the serialized stream deserialization stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.