Java 8 new features
Content:1. lambda expressions2. streamAPI3. built-in function Interfaces4. default Interface Implementation Method5. Use lambda/streamAPI in android
========
1. lambda expressions:Several forms:
()->statement()->(statement)()->{statement}
Take Thread as an example:
new Thread(new Runnable(){ public void run(){ }}).start();new Thread(()->{ System.out.println(Thread.currentThread(
()) {File.createnewfile (); } Randomaccessfile RAF=NewRandomaccessfile (file, "RW"); System.out.println (Raf.getfilepointer ()); Raf.write (' A ');//One char takes two bytes, but write writes only one byte at a time (the last eight bits of the A character)System.out.println (Raf.getfilepointer ()); Raf.write (B); inti = 0x7fffffff; Raf.write (i>>> 24);//write high eight bits of IRaf.write (i >>> 16); Raf.write (i>> 8); Ra
read (). When there is no resource, it will be blocked and almost no resource is occupied.The close () method is not redundant for JAVA garbage collectors, because when an InputStream object is created, some resources will be generated in the operating system, but the garbage collector can only recycle object garbage.
4. OutputStream classThree write () methods.Flush () writes data to the buffer immediatelyClose () writes data to the buffer and close
2017-11-05 21:37:50
Properties
Properties:Propertiesclass represents a persisted set of properties. Propertiescan be saved in a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.Properties is an attribute collection class that is a collection class that can be combined with an IO stream.The propert
1:JDK4 New IO classes to understandBuffer (buffered), Channer (channel)2:jdk7 New IO classes to knowPath: Platform-independent paths.Paths: Contains a static method that returns path.public static path Get (URI uri): Determines the file path based on the given URI. Files: The tool class for manipulating files. Provides a number of ways toLearn the following simple wayspublic static long copy (Path source, outputstream out): Copy filepublic static Path Write (path path, iterable 1 // 2 files.c
longer readwhile ((N=fis.read (words))!=-1) {//6. Read the information in the file input stream until-1 endsFos.write (Words,0,n); 7. The file output stream is written to read the content length in the words array is 0-n}System.out.print ("File copy succeeded, view file");}catch (Filenotfoundexcepton e) {//The first input file may not be found here to write an exceptionE.prinntstacktrace (); Exceptions fro
efficient reading of characters, arrays, and rows.You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.1 //character buffer input stream2BufferedReader br =NewBufferedReader (NewFileReader ("A.txt"));3 intLen = 0;4 Char[] ch =New Char[1024];5 while(len = br.read (ch))! =-1){6System.out.println (NewString (Ch,0,len));//Print the contents of the a.txt in a string way7 }
also provides a way to read and write various types of methods, using a similar method, which is no longer elaborated here, interested friends can try on their own.Attention:
If you want to start reading from the file header, you must move the pointer to the head and seek () to assign the pointer to a position;
Read (byte[]) can pass through the byte array to which all the bytes are reading;
When a byte array is converted to a string, the encoding format of the primary string i
Java list Implementation Method efficiency (ArrayList, aggregate list, Vector, Stack), and java timestamp Acquisition Method Comparison, arraylistdetaillist 1. Introduction to list
List class, which stores any objects in sequence
Implement a linked list using java, and implement a linked list using java
A linked list is a non-sequential storage structure of physical storage units. The logical sequence of data nodes is achieved through the pointer connection sequence in the linked
Merge stream/Sequential stream (Sequenceinputstream):is to combine multiple input streams into a single Stream object.1 Public classSequenceinputstreamdemo {2 Public Static voidMain (string[] args)throwsException {3 //To create a sequential stream object4Sequenceinputstream in =NewSequenceinputstream (New
other programming languages. The introduction of lambda expressions in Java SE 8 and enhancements to the standard libraries improve this situation. Specifically, in two aspects of the improvement: the first aspect is the operation of the set of methods. Thanks to the introduction of the default method, interfaces in the Java Collection framework can be updated t
5 7
Print numbers greater than 5: 6 7
The following example uses a LAMBDA expression to print the square of each element in a numeric value, and note that we use the. Stream () method to convert a regular array to a stream. Java 8 adds some awesome streaming APIs. The Java.util.stream.Stream interface contain
,A class instance that implements this interface is available for the filter file name.There is only one method under this interface:accept(File dir,String name)Tests whether the specified file should be included in a list of files. : Returns if and only if the name should be included in the list of files true false .Import Java.io.*;class Filerjava implements Filenamefilter{public Boolean accept (File dir,
and close the channel in}Catch(Exception e) { - System.out.println (e.tostring ()); to } + } - } the *}3. Writing files1 PackageCom.newdream.class4;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileWriter;7 ImportJava.io.StringReader;8 9 Public classChariowriter {Ten One //writes text to the specified file A - - Public StaticString data=
server side, it is used to send data to the client. Otherwise, it is used to send data to the server side.
3. Simple TCP network program
1 import java. io. outputStream; 2 import java.net. serverSocket; 3 import java.net. socket; 4 5 public class Example4 {6 public static void main (String [] args) throws Exception {7 new TCPServer (). listen (); // create the TCPServer object and call the listen () method 8
its parameters are evaluated before the method executes. This is the result of parameters passed by value in Java.The function is different from the other. When manipulating a function, we can not evaluate the parameters and have absolute control over when the parameters are evaluated. Also, if a function has more than one parameter, they can take a different value at the same time. This can be done by applying local. But first, we'll consider how closures are implemented.Closure examplesFor fu
Common APIs for Java-IO stream File, javaiofileapi
File class
1. It is only used to indicate the information (name, size, etc.) of the file (directory) and cannot be used to access the file content.
Package cn. test; import java. io. file; import java. io. IOException; public class Demo16 {public static void main (S
to determine whether the original file at the end.1 Public classFileIO {2 Public Static voidMain (string[] args)throwsIOException {3String filename= "D:" +file.separator+ "Io.txt";4File file=NewFile (FileName);5InputStream inputstream=Newfileinputstream (file);6 byte[] by=New byte[1024];7 intCount=0;8 intTemp=0;9 while((Temp=inputstream.read ())!=-1){TenBy[count++]= (byte) temp; One A } - inputstream.close (); -SYSTEM.OUT.PRINTLN ("read-in length
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.