Java print stream recursive copy sub-file subfolders copy different encoding files to the same file serialized stream deserialization stream, java serialization

Source: Internet
Author: User

Java print stream recursive copy sub-file subfolders copy different encoding files to the same file serialized stream deserialization stream, java serialization

Package com. swift. jinjie; import java. io. bufferedInputStream; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. printStream;/* enter a folder path from the keyboard and copy all files (including subfolders) in the folder to the temp folder on disk D using the print stream. */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 ("including subfolders copied successfully");} 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 B Yte [1024]; while (len = bis. read (buf ))! =-1) {ps. write (buf, 0, len );}}}}}

Copy different encoding files to the same file

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:/abcdisk contains two upload files, a.txt and B .txt. In this example, a.txt adopts GBK, while B .txt uses utf8. You must use the conversion stream to implement the following functions: copy the.txt and B .txt files to the c.txt file to ensure that the content is not garbled. */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 FileI NputStream (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 ("file copied successfully ");}}

Deserialization

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. producer; import com. swift. baseKnowledge. student;/* includes Student ID, name, Province ID, Java score, mathematics score, English score, and other member variables. The constructor and setter and getter methods are provided. Requirements: * student information and student ID card numbers are saved to the save.txt file on the C drive. * student ID card numbers cannot be saved to the file. * If save.txt does not exist during program operation, enter one student information on the keyboard. The information input format is as follows: ***** enter the student information ***** enter the student ID: 9527. Enter the name: please enter your ID card number: 2203919831234543. Please enter your Java score: 90. Please enter your math score: 80. Enter your English score: 88. The student ID is saved to the save.txt file on the C drive. * If the save.txt file already exists at the program runtime, the student information is displayed. The format is as follows: * *** student basic information *** student ID name province ID number Java score mathematical score English score 9527 huaan 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 {today scan = new today (System. in); for (int I = 0; I <1; I ++) {System. out. print ("Enter student id:"); String id = scan. next (); Student student = new Student (); student. setId (id); System. out. println ("Enter name:"); String name = scan. next (); student. setName (name); System. out. println ("Enter your ID card number:"); 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 ("student ID name ID card number mathematical score Chinese score total score"); System. out. println (stu. getId () + "" + stu. getName () + "" + stu. getUid () + "" + stu. getShuxue () + "");}}}

 

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.