当前位置:网站首页>Review the first three IO streams

Review the first three IO streams

2022-06-13 03:36:00 Mouth strong programmer

package com.zking.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.URL;

public class MyDemo01 {
	public static void main(String[] args) throws Exception {
		//S2- senior API
		//1.IO flow   2.NET Network programming 	3. Multithreading 
		
		//2.IO The core of the stream : Read and write files 
		//2.1 File class —— Describe the folders and file objects on the disk 
		//   How to describe a file or folder    adopt File Class constructor 
		//File file = new File("D:\\a.txt");
		File file = new File("D:\\a.txt");
		// New features 
		file.createNewFile();
		file.mkdir();
		file.mkdirs();
		// Judgment function 
		file.exists();
		file.isFile();
		file.isDirectory();
		file.isHidden();
		// Delete function 
		file.delete();
		// obtain 
		file.getPath();
		file.getAbsolutePath();
		file.getName();
		file.getParent();
		file.lastModified();
		file.length();
		
		
		file.list();
		file.listFiles();
		
		
		// recursive : The method itself calls itself ----- Method 
		
//		File file2 = new File("C:\\");
//		findFileAll(file2);
		
		
		//IO flow : Read and write 
		//I---Input   Input     -------》  Read 
		//O---Output  Output  ---------》 write in 
		
		// In the direction of the flow ------- Input stream ( Read )   Output stream ( write in )
		// According to the type of flow ------- Byte stream  ( byte )     Character stream ( character )
		// According to the way the stream is handled ----- Node flow      Processing flow (Buffered)
		
		// Byte stream    And byte processing stream 
		// Handling of all documents 
		//FileInputStream
		//FileOutputStream
		/*
		 technological process :
			1. adopt File Object to build a file path object 
			2. Building a byte input stream or output stream object will File Objects are packaged 
			3. Call the read or write method to process 
			4. All critical resources 
		*/
		
		// write in a To D:\\a.txt
		
		File newFile = new File("D:\\a.txt");
		FileOutputStream fos = new FileOutputStream(newFile);
		fos.write('a');
		fos.close();
		System.out.println(" Operation is completed ");
		
		
		
		// Processing flow --- Buffer stream of bytes 
		//BufferedInputStream
		//BufferedOutputStream
		
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		
		
		//day03   Serialization and deserialization ————————IO flow    Stream object 
		// serialize : Save objects to a file by streaming    ObjectOutputStream
		// Deserialization : Read the objects in the file to the memory by streaming  ObjectInputStream
		
		Student stu = new Student(" Zhang San ");// object 
		
		File file3 = new File("D:\\sb.bak");
		FileOutputStream fos2 = new FileOutputStream(file3);
		ObjectOutputStream oos = new ObjectOutputStream(fos2);
		oos.writeObject(stu);
		oos.close();
		fos2.close();
		
		
		//day04- Character stream 
		//Writer   write in    Output 
		//Reader   Read    Input 
		
		// Stream to file 
//		FileWriter
//		FileReader
		
//		BufferedWriter
//		BufferedReader
		
//		 Intermediate conversion flow ----- Consider a coding problem 
//		InputStreamReader
//		OutputStreamWriter
		
//		InputStreamReader isr = new InputStreamReader(in, cs);
		// Chinese code :  GBK   GB2312  UTF-8
		// pure English        ISO-8859-1
		
//		 Network programming      URL
		//URL---- Uniform resource placeholder 
		//http://193.168.3.82:8080/web_01/index.html?username=admin
		// Address | website 
		// agreement 
		// domain name 
		// Port number 
		// Resource name 
		// Specific page 
		// Parameters 
		
		
		URL url = new URL("https://www.shijuepi.com/uploads/allimg/200918/1-20091Q10417.jpg");
		InputStream is = url.openStream();
		BufferedInputStream bis = new BufferedInputStream(is);
		
		// Output stream ---- Role of preservation 
		File fileImg = new File("D:\\a.jpg");
		FileOutputStream fosImg= new FileOutputStream(fileImg);
		BufferedOutputStream bosImg = new BufferedOutputStream(fosImg);
		
		int len = 0;
		while(-1!=(len = bis.read())) {
			bosImg.write(len);
			bosImg.flush();
		}
		
		// close resource 
		//
		System.out.println("OK");
		
		
	}
	
	// lookup D All files on disk 
	public static void findFileAll(File file) {
		if(file.isFile()) {// It's a document 
			System.out.println(file.getName());
		}else if(file.isDirectory()){
			File[] listFiles = file.listFiles();
			// Filter the hidden files of the system 
			if(null == listFiles) {
				return;
			}
			// Traverse 
			for (File file2 : listFiles) {
				findFileAll(file2);
			}
		}
	}
	
	

	
	
}

 io Stream save object serialization   Use objects with deserialization

package com.zking.test;

import java.io.Serializable;

public class Student implements Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String name;

	public Student(String name) {
		super();
		this.name = name;
	}
	
	
}

原网站

版权声明
本文为[Mouth strong programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280529564197.html