Java completion basis (b) JavaSE basis

I / O stream

    Not much has been used, it's time to figure out.

  1. In Java I / O stream classification

    • Divided according to the flow of the stream: the input and output streams
    • Divided according to the operating unit: byte stream and character stream
    • Role flow fraction according to: node flow and processing flow
    I / O classification Byte stream Jifuryu
    Input stream InputStream Reader
    Output stream OutputStream Writer
  2. Java's I / O class libraries

    IO class very much, it is also to make them clear division of labor, each responsible for different functions to achieve. Uses include:

    • File Access
    • Network Access
    • Memory cache access
    • Internal communications thread (pipe)
    • buffer
    • filter
    • Resolve
    • Read and write text (Reader, Writer)
    • Basic data write (int, long ...)
    • Reading and writing objects

    Figure steal directly Java foundation --Java IO Detailed

        The upper panel shows the relationship between various classes

        The figure represents the various classes are responsible for media

  1. Usage basic I / O stream

    3.1 byte stream (InputStram and OutputStream)

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    
    public class AboutIO {
    	
    	public static void writeByte2File() throws IOException {
    		// TODO 自动生成的方法存根
    		String hello = "hello world";
    		byte[] bytes = hello.getBytes();
    		
    		File file = new File("G:/Java/hello.text");
    		OutputStream stream = new FileOutputStream(file);
    		stream.write(bytes);
    		stream.flush();
    		stream.close();
    	}
    	
    	public static void readFile2Bytes() throws IOException {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello.text");
    		//创建文件长度大小的byte数组
    		byte[] bytes = new byte[(int)(file.length())];
    		InputStream stream = new FileInputStream(file);
    		int size = stream.read(bytes);
    		System.out.println("大小:"+size+"内容"+new String(bytes));
    		stream.close();
    	}
    	
    
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		readFile2Bytes();
    	}
    }
    
    复制代码
    大小:11内容hello world
    复制代码

    3.2. Character streams (Reader and Writer)

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class AboutIO {
    	
    	public static void writeChar2File() throws IOException {
    		// TODO 自动生成的方法存根
    		String hello = "Hello,Hello";
    		File file = new File("G:/Java/hello1.text");
    		FileWriter writer = new FileWriter(file);
    		writer.write(hello);
    		writer.close();
    	}
    	public static void readFile2Char() throws IOException {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello1.text");
    		FileReader reader = new FileReader(file);
    		char[] chars = new char[(int)file.length()];
    		int size = reader.read(chars);
    		System.out.println("大小:"+size+",内容:"+new String(chars));
    		reader.close();
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		writeChar2File();
    		readFile2Char();
    	}
    }
    复制代码
    大小:11,内容:Hello,Hello
    复制代码

    3.3. Byte stream into a character stream

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.Reader;
    
    public class AboutIO {
    	
    	public static void byte2Char() throws IOException {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello1.text");
    		InputStream inputStream = new FileInputStream(file);
    		Reader reader = new InputStreamReader(inputStream);
    		char[] chars = new char[(int)file.length()];
    		int size = reader.read(chars);
    		System.out.println("大小:"+size+",内容:"+new String(chars));
    		inputStream.close();
    		reader.close();
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		byte2Char();
    	}
    }
    复制代码
    大小:11,内容:Hello,Hello
    复制代码

    3.4. The combination of IO class

     即将一个流放入另一个流的构造器中,组合可以得到更多功能的实现
    复制代码

    3.5. Reading and Writing Files of media File

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.Reader;
    
    public class AboutIO {
    	
    	public static void fileOption() {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello/2.text");
    		boolean exists = file.exists();
    		System.out.println("G:/Java/hello/2.text是否存在?"+exists);
    		
    		File file1 = new File("G:/Test111/test/1.text");
    		boolean mkdir = file1.mkdir();
    		System.out.println("mkdir:"+mkdir);
    		boolean file12 = file1.exists();
    		System.out.println("G:/Test111/test/1.text存在吗"+file12);
    		File file2 = new File("G:/Test222/test2.text");
    		boolean mkdirs = file2.mkdirs();
    		System.out.println("mkdirs:"+mkdirs);
    		System.out.println("mkdirs会创建多级不存在目录,mkdir只会创建一个目录,且父目录必须存在");
    		
    		File hello = new File("G:/Java/hello/1.text");	//内容Hello,Hello
    		hello.renameTo(new File("G:/Java/helloHello.text"));
    		boolean delete = hello.delete();
    		System.out.println("helloHello还存在吗?"+hello.exists());
    		boolean file11 = file1.isDirectory();
    		System.out.println("G:/Test111/test/1.text是目录吗?"+file11);
    		File file3 = new File("G:/Test222");
    		File file4 = new File("G:/Test222/test2.text");
    		File[] listFiles = file3.listFiles();
    		String[] list = file3.list();
    		for(File f : listFiles) {
    			System.out.println("G:/Test222下的listFiles方法---遍历出文件的文件名:"+f.getName());
    		}
    		for(String s:list) {
    			System.out.println("G:/Test222下的list方法---遍历出文件的文件名:"+s);
    		}		
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		//byte2Char();
    		fileOption();
    	}
    }
    复制代码
    G:/Java/hello/2.text是否存在?false
    mkdir:false
    G:/Test111/test/1.text存在吗true
    mkdirs:false
    mkdirs会创建多级不存在目录,mkdir只会创建一个目录,且父目录必须存在
    helloHello还存在吗?false
    G:/Test111/test/1.text是目录吗?true
    G:/Test222下的listFiles方法---遍历出文件的文件名:test2.text
    G:/Test222下的list方法---遍历出文件的文件名:test2.text
    复制代码

    3.6. A random location to read and write files

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.RandomAccessFile;
    import java.io.Reader;
    
    public class AboutIO {
    
    	public static void randomReadFile() throws IOException {
    		// TODO 自动生成的方法存根
    		//r--只读;rw--读写;rws--读写打开,并对内容或元数据同步写入底层存储设备;
    		//rwd--读写打开,对文件的更新同步至底层存储设备
    		File fileO = new File("G:/Java/hello3.text");
    		FileOutputStream stream = new FileOutputStream(fileO);
    		String content = "如果你和许多人一样,你可能已经决定要少花点时间盯着手机看了。\r\n" + 
    				"这是个好主意:越来越多的证据表明,我们在我们的智能手机上耗费的时间正在干扰我们的睡眠、自尊、人际关系、记忆、注意力持续时间、创造力、生产力以及解决问题和决策的能力。\r\n" + 
    				"但令我们重新思考与这些设备的关系的,还有另一个原因。通过长期提高身体主要的应激激素——皮质醇的水平,我们的手机可能会威胁我们的健康,并缩短我们的寿命。\r\n" + 
    				"到目前为止,大多数关于手机生化效应的讨论都集中在多巴胺上,这是一种帮助我们形成习惯和上瘾的大脑化学物质。就像老虎机一样,智能手机和应用程序明显是为了触发多巴胺的释放而设计的,目的就是让我们难以放下手中的设备。\r\n" + 
    				"这种对我们多巴胺系统的操纵,正是许多专家认为我们正在对我们的手机产生行为上瘾的原因。但我们的手机对皮质醇的影响可能更令人担忧。\r\n" + 
    				"皮质醇是影响战逃决策的主要激素。它的释放会引发一些生理变化,比如血压、心率和血糖的飙升,这些变化会帮助我们对紧急的人身威胁做出反应并存活下来。\r\n" + 
    				"如果你的身体确实存在危险,比如一头公牛正在向你冲来,这些反应将可以挽救你的生命。但我们的身体也会释放皮质醇以应对情绪压力,在这种情况下,心率加快并没有多大好处,比如查看手机时发现老板发来的一封愤怒的邮件。\r\n";
    		byte[] b = content.getBytes();
    		stream.write(b);
    		stream.close();
    		
    		RandomAccessFile file = new RandomAccessFile("G:/Java/hello3.text", "rw");
    		
    		file.seek(file.length()/3);
    		String s = "--插入--";
    //		s = new String(s.getBytes("ISO-8859-1"),"utf-8");
    		byte[] bytes = s.getBytes();
    		file.write(bytes);
    		
    		file.seek(10);
    		long pointer = file.getFilePointer();
    		byte[] contents = new byte[1024];
    		file.read(contents);
    		long pointEnd = file.getFilePointer();
    		System.out.println("pointerStart:"+pointer);
    		System.out.println("pointerEnd:"+pointEnd);
    		System.out.println("content:"+new String(contents));
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		//byte2Char();
    		//fileOption();
    		randomReadFile();
    	}
    }
    
    复制代码
    pointerStart:10
    pointerEnd:1034
    content:��许多人一样,你可能已经决定要少花点时间盯着手机看了。
    这是个好主意:越来越多的证据表明,我们在我们的智能手机上耗费的时间正在干扰我们的睡眠、自尊、人际关系、记忆、注意力持续时间、创造力、生产力以及解决问题和决策的能力。
    但令我们重新思考与这些设备的关系的,还有另一个原因。通过长期提高身体主要的应激激素——皮质醇的水平,我们的手机可能会威胁我们的�--插入--缩短我们的寿命。
    到目前为止,大多数关于手机生化效应的讨论都集中在多巴胺上,这是一种帮助我们形成习惯和上瘾的大脑化学物质。就像老虎机一样,智能手机和应用程序明显是为了触发多巴胺的释放而设计的,目的就是让我们难以放下手中的设备。
    这种对我们多巴胺系统的操纵,正是许多专家认为我们正在对我们的手机产生行为上瘾的原因。但我们的手机对皮质
    复制代码
    其中:
                    file.seek(file.length()/3);
    		String s = "--插入--";
    //		s = new String(s.getBytes("ISO-8859-1"),"utf-8");
    		byte[] bytes = s.getBytes();
    		file.write(bytes);
    这段如果不写,也就是不用randomAccessFile的write来写入,获得的txt文件打开时,没有乱码,但是如果使用了write,控制台输出没有问题,但是,txt文件中就会变成乱码,即便使用注释部分的代码也没能解决。
    复制代码

    Do not use the write randomAccessFile

    Use the write randomAccessFile

    3.7. Pipeline media

    In java, the role of the pipeline is to achieve the same virtual machine communication between two different threads. And pipeline related classes are: PipedInputStream and PipedOutputStream.

    package cn.wh3t;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    import java.io.RandomAccessFile;
    import java.io.Reader;
    
    
    public class AboutIO {
    	
    	public static void pipedStream() throws IOException {
    		// TODO 自动生成的方法存根
    		final PipedOutputStream pipedOutputStream = new PipedOutputStream();
    		final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
    		Thread thread1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				// TODO 自动生成的方法存根
    				try {
    					pipedOutputStream.write("piped".getBytes());
    				} catch (IOException e) {
    					// TODO: handle exception
    				}
    			}
    		});
    		Thread thread2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				// TODO 自动生成的方法存根
    				try {
    					int data = pipedInputStream.read();
    					while (data!=-1) {
    						System.out.println((char)data);
    						data = pipedInputStream.read();
    					}
    				} catch (IOException e) {
    					// TODO: handle exception
    				}finally {
    					try {
    						pipedInputStream.close();
    					} catch (IOException e2) {
    						// TODO: handle exception
    					}
    				}
    			}
    		});
    		thread1.start();
    		thread2.start();
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		//byte2Char();
    		//fileOption();
    		//randomReadFile();
    		pipedStream();
    	}
    }
    复制代码
    p
    i
    p
    e
    d
    复制代码

    Like the two pipelines, one responsible for input, a charge of output.

    3.8. Network Media

    JavaIO network that is oriented Java network programming , core socket, the same as disk operations, corresponding to the two sets of Java network programming api: IO and NIO.

    JavaAPI provided two NIO, a set of standard input and output NIO, a network programming NIO.

    NIO difference between the maximum and IO: IO data (simple and convenient, but the process is slow), NIO processing data in blocks (faster than the flow, but not as a simple stream) in a stream.

    • About Buffer

      Buffer and Channel standard is a core subject in the NIO, almost every IO operations are used to get. Any source and target data must pass through a Channel object, Buffer namely container object, all objects, whether the reader to go through the Channel Buffer.

      Buffer is essentially an array, usually a byte array, but may be other type of array. However, a buffer is not just an array importantly, it provides a structured access to data, but also can read and write process tracking system.

      Buffer read and write data using the following four general steps:

      1. Data is written to Buffer
      2. Call flip () method (the Buffer switching from write mode to read mode)
      3. Reading data from the Buffer
      4. Call clear () or compact () method (Clear emptied the entire buffer, the read data is cleared Compact any unread data is moved to the beginning of the buffer, the new data is written back into the unread data)

      Java NIO in specific Buffer types are as follows: (Data Buffer can be these basic types, Mapped some special - in simple terms MappedByteBuffer )

      • ByteBuffer
      • MappedByteBuffer
      • CharBuffer
      • DoubleBuffer
      • FloatBuffer
      • IntBuffer
      • LongBuffer
      • ShortBuffer
    • About Channel

      Different flows and Channel

      1. Channel is bi-directional, both reading and writing, and the flow is unidirectional
      2. Channel support asynchronous read and write
      3. Channel must read and write through the Buffer objects

      Java NIO in Channel types are as follows:

      • FileChannel (for reading and writing file data)
      • DatagramChannel (UDP for reading and writing data)
      • The SocketChannel (TCP for reading and writing data)
      • ServerSocketChannel (allow us to listen to the TCP link requests, each will create a SocketChannel)
    • Read and write operations NIO

      Reads data from a file, it requires three steps, write, too

      1. Get the Channel from FileInputStream
      2. Creating Buffer
      3. To read data from the Channel Buffer
      package cn.wh3t;
      
      import java.io.BufferedInputStream;
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.FileReader;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.io.PipedInputStream;
      import java.io.PipedOutputStream;
      import java.io.RandomAccessFile;
      import java.io.Reader;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;
      
      public class AboutIO {
      	
      	public static void NIOReadAndWriteByCopyFile(String src,String dst) throws IOException {
      		// TODO 自动生成的方法存根
      		FileInputStream is = new FileInputStream(src);
      		FileOutputStream os = new FileOutputStream(dst);
      		FileChannel Ichannel = is.getChannel();
      		FileChannel Ochannel = os.getChannel();
      		
      		ByteBuffer buffer = ByteBuffer.allocate(1024);
      		while (true) {
      			int num = Ichannel.read(buffer);
      			if (num == -1) {
      				break;
      			}
      			buffer.flip();//Flips this buffer. The limit is set to the current position and thenthe position is set to zero.
      			Ochannel.write(buffer);
      			buffer.clear();
      		}
      		Ichannel.close();
      		Ochannel.close();
      		is.close();
      		os.close();
      	}
      	
      	public static void main(String[] args) throws IOException {
      		//writeByte2File();
      		//readFile2Bytes();
      		//writeChar2File();
      		//readFile2Char();
      		//byte2Char();
      		//fileOption();
      		//randomReadFile();
      		//pipedStream();
      		//bufferStream();
      		//bufferChar();
      		NIOReadAndWriteByCopyFile("G:/Java/hello3.text", "G:/Java/hello4.text");
      	}
      }
      复制代码
      src为文件读取路径,dst为写入文件路径,运行完毕后,会创建一个和src一样内容的dst文件
      复制代码

      Which should be noted:

      1. Used in the while loop read () == - 1. Check whether read completion

      2. Buffer type flip () and clear ()

        Control buffer status three variables:

         1. position:跟踪写了或者读取了多少数据,它指向的是下一个字节来自哪个位置
         2. limit:代表还有多少数据可以读取或者多少空间可以写入,它的值小于等于capacity
         3. capacity:代表缓冲的最大容量,一般新建一个缓冲区的时候,limit和capacity的值默认是相等的
        复制代码

        flip, clear that the method is used to set these two values.

         public final Buffer flip() {
             //一般从Buffer读取数据前调用
             limit = position;
             position = 0;
             mark = -1; //取消标记
             return this;
         
         public final Buffer clear() {
             //一般把数据写入Buffer前调用
             position = 0;
             limit = capacity;
             mark = -1;
             return this;
        复制代码
    • Network Programming NIO (asynchronous IO)

      Brief asynchronous IO

      1. Asynchronous IO read and write data is not blocked, the general IO, when read will block until there is data to be read will block until there is data to be written during write. Not only does not block asynchronous IO, IO can also register events such as read data, new connections, when an event occurs, the system will notify.
      2. Another big advantage of asynchronous IO is: it allows a large number of inputs and outputs simultaneously execute IO. Since polling is usually synchronous IO, or create a large number of threads to handle. Using asynchronous IO, events on any number of channels can listen without polling, do not have a lot of threads.

      Selector (NIO third core object)

      • Selector is an object that can be registered to a lot Channel, listen for events that occur on each Channel, and be able to read or write Channel decided based on the event. Such a thread through managing multiple Channel, it can handle a large number of connections.
      • With the benefits of the Selector: you can use a thread to handle all the channels, switching between threads for the operating system is costly, and each thread will consume resources. So, the better thread systems. (But now with the operating system and CPU performance in terms of getting better and better multi-tasking, k overhead is also reduced, in fact, a multi-core CPU, without the use of multi-tasking is a waste of CPU power)

      Selector Channel have interested in the event are four:

      1. Connect --SelectionKey.OP_CONNECT
      2. Accept --SelectionKey.OP_ACCEPT
      3. Read --SelectionKey.OP_READ
      4. Write --SelectionKey.OP_WRITE
      • Channel trigger an event, the event has already explained Ready. Therefore, when the channel connection is successful to the server, that connect ready; accept ready is when the server accepts a request for connection channel; channel when the read data is read ready; channel has a writable data in write ready. (If you are interested in multiple events, use or ( "|") operator to connect)

      SelectionKey

      • Call to register () returned a SelectionKey. SelectionKey registered on behalf of the channel in the Selector on this. When a Selector notify you when an incoming event, it is done by providing the event SelectionKey, SelectionKey user can also cancel the registration channel. Its properties are as follows:
        1. Interest the SET at The (is a collection of events of interest to be selected)
        2. READY the SET at The (channel ready collection)
        3. Channel at The (get registered Channel)
        4. Selector at The (get registered Selector)
        5. Object attached AN (optional) (more information about an object or attach to the SelectionKey, to facilitate identification of a particular channel)

      By selecting channels Selector

      • Once Selector registered with one or more channels, you can call several overloaded select () methods, which return to ready the channel.

        • int select (): obstruction to have at least one channel-ready on your event registration
        • int select (long timeout): and select the same, but the longest block the timeout milliseconds
        • int selectNow (): does not block, no matter what the channel is ready to return immediately
      • Once called select () method, which returns a value representing one or more channels ready. You can () will return SelectionKey in virtual channenl by selector.selectedKey

      • After calling select a thread is blocked, there are ways to return from select, as long as the other thread calls selector.wakeup on that object first thread calls the select method, blocked threads will be immediately returned.

      • If there are other thread calls wakeup, but there are no threads blocked on select methods, then the next call to select the thread immediately wakeup.

      • Call the close method after using the Selector, it will close all SelectionKey Selector and registered under the Selector invalid, the channel itself is not closed.

    3.9. BufferedInputStream和BufferedOutputStream

    I.e., to increase efficiency using the IO buffer, Common IO is read byte by byte, but with a buffer, a chunk of data can be read once

    package cn.wh3t;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    import java.io.RandomAccessFile;
    import java.io.Reader;
    
    
    public class AboutIO {
    
    	public static void bufferStream() throws IOException {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello3.text");
    		byte[] bytes = new byte[(int)file.length()];
    		InputStream is = new BufferedInputStream(new FileInputStream(file),2*1024);
    		//the total number of bytes read into the buffer, or -1 if there is no more data because the end ofthe stream has been reached.
    		int size = is.read(bytes);
    		System.out.println("大小:"+size+"内容:"+new String(bytes));
    		is.close();
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		//byte2Char();
    		//fileOption();
    		//randomReadFile();
    		//pipedStream();
    		bufferStream();
    	}
    }
    复制代码
    大小:1595内容:如果你和许多人一样,你可能已经决定要少花点时间盯着手机看了。
    这是个好主意:越来越多的证据表明,我们在我们的智能手机上耗费的时间正在干扰我们的睡眠、自尊、人际关系、记忆、注意力持续时间、创造力、生产力以及解决问题和决策的能力。
    但令我们重新思考与这些设备的关系的,还有另一个原因。通过长期提高身体主要的应激激素——皮质醇的水平,我们的手机可能会威胁我们的健康,并缩短我们的寿命。
    到目前为止,大多数关于手机生化效应的讨论都集中在多巴胺上,这是一种帮助我们形成习惯和上瘾的大脑化学物质。就像老虎机一样,智能手机和应用程序明显是为了触发多巴胺的释放而设计的,目的就是让我们难以放下手中的设备。
    这种对我们多巴胺系统的操纵,正是许多专家认为我们正在对我们的手机产生行为上瘾的原因。但我们的手机对皮质醇的影响可能更令人担忧。
    皮质醇是影响战逃决策的主要激素。它的释放会引发一些生理变化,比如血压、心率和血糖的飙升,这些变化会帮助我们对紧急的人身威胁做出反应并存活下来。
    如果你的身体确实存在危险,比如一头公牛正在向你冲来,这些反应将可以挽救你的生命。但我们的身体也会释放皮质醇以应对情绪压力,在这种情况下,心率加快并没有多大好处,比如查看手机时发现老板发来的一封愤怒的邮件。
    复制代码

    About setting the size of the buffer, it is necessary to know each read block size of the disk cache and its size, while setting the optimum value.

    BufferOutputStream basically the same.

    1. BufferedReader和BufferedWriter

    And BufferedInputStream and BufferedOutputStream almost, but for the realization of a character stream.

    package cn.wh3t;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    import java.io.RandomAccessFile;
    import java.io.Reader;
    
    public class AboutIO {
    	
    	public static void bufferChar() throws IOException {
    		// TODO 自动生成的方法存根
    		File file = new File("G:/Java/hello3.text");
    		char[] chars = new char[(int)file.length()];
    		Reader reader = new BufferedReader(new FileReader(file));
    		//The number of characters read, or -1if the end of the streamhas been reached
    		int size = reader.read(chars);
    		System.out.println("大小:"+size+"内容:"+new String(chars));
    		reader.close();
    	}
    	
    	public static void main(String[] args) throws IOException {
    		//writeByte2File();
    		//readFile2Bytes();
    		//writeChar2File();
    		//readFile2Char();
    		//byte2Char();
    		//fileOption();
    		//randomReadFile();
    		//pipedStream();
    		//bufferStream();
    		bufferChar();
    	}
    }
    复制代码
    大小:541内容:如果你和许多人一样,你可能已经决定要少花点时间盯着手机看了。
    这是个好主意:越来越多的证据表明,我们在我们的智能手机上耗费的时间正在干扰我们的睡眠、自尊、人际关系、记忆、注意力持续时间、创造力、生产力以及解决问题和决策的能力。
    但令我们重新思考与这些设备的关系的,还有另一个原因。通过长期提高身体主要的应激激素——皮质醇的水平,我们的手机可能会威胁我们的健康,并缩短我们的寿命。
    到目前为止,大多数关于手机生化效应的讨论都集中在多巴胺上,这是一种帮助我们形成习惯和上瘾的大脑化学物质。就像老虎机一样,智能手机和应用程序明显是为了触发多巴胺的释放而设计的,目的就是让我们难以放下手中的设备。
    这种对我们多巴胺系统的操纵,正是许多专家认为我们正在对我们的手机产生行为上瘾的原因。但我们的手机对皮质醇的影响可能更令人担忧。
    皮质醇是影响战逃决策的主要激素。它的释放会引发一些生理变化,比如血压、心率和血糖的飙升,这些变化会帮助我们对紧急的人身威胁做出反应并存活下来。
    如果你的身体确实存在危险,比如一头公牛正在向你冲来,这些反应将可以挽救你的生命。但我们的身体也会释放皮质醇以应对情绪压力,在这种情况下,心率加快并没有多大好处,比如查看手机时发现老板发来的一封愤怒的邮件。
    复制代码

Reference:
Java foundation --Java IO explain
the basics of Java --Java NIO explain (a)
Java foundation --Java NIO Comments (two)
Java NIO simple tutorial

Reproduced in: https: //juejin.im/post/5d0857296fb9a07ef06f95a3

Guess you like

Origin blog.csdn.net/weixin_33712987/article/details/93177481