JAVA 56th-IO stream (10) Object serialization and deserialization RandomAccessFile, io serialization
Operation object
ObjectInputStream and ObjectOutputStream
The operated object must implement Serializable (Mark Interface)
ObjectOutputStream writes the basic data types and graphics of Java objects to OutputStream. You can use ObjectInputStream to read (refactor
I. Input and Output1. Stream represents any data source capable of producing data or any receiving source capable of receiving data. In Java I/O, all streams (including input and out stream) have two types:1.1 byte-oriented streamA byte-oriented stream that reads or writes information to a
The difference between a character stream and a byte streamTake a look at an example of the previous article:1 public static void main (string[] args) throws Exception 2 {3 file File = new file ("D:/writer.txt"); 4 writer ou t = new FileWriter (file); 5 //declares a String type Object 6 string str = "Hello world!!!"; 7 out.write (str); 8 out.close (); 9 // Read file operation
The difference between a character stream and a byte streamTake a look at an example of the previous article:1 Public Static voidMain (string[] args)throwsException2 {3File File =NewFile ("D:/writer.txt");4Writer out =NewFileWriter (file);5 //declares a String type object6String str = "Hello World!!!";7 out.write (str);8 out.close ();9 Ten //Read File Operation OneReader in =Newfilereader (
?
How do I correctly determine if an object is a lambda?
Why does iterable not provide the stream () and Parallelstream () methods?
Using semaphore in the parallel stream method in Java 8 could lead to deadlocks, is this a bug?
Why is it not allowed to define
Java input and output can be divided into two categories, streaming and non-streaming operations, streaming operations mainly concerned with the reading and writing of the contents of the file, non-streaming operations are mainly used for file management on disk. The following first learns streaming data operations.FlowSimilar to C + +, the concept of flow is also provided in Java to handle input and output
Java Base IO Stream (i) byte stream IO flow system is too large, involving a variety of flow objects, I think it is necessary to summarize.What is an IO stream, IO represents input, Output, and the flow is an abstraction of the data transfer from the original source to the target medium. Typical data sources and targe
Flow :Concept: The operation of a file in Java is done in a streaming fashion. A stream is an ordered set of data sequences in Java memory. Java reads the data from the source (file, memory, keyboard, network) into memory, forms a stream, and then writes those streams to ano
): Writes out one byte array at a time* Available () Gets the number of bytes read from the file* Disadvantages: There may be memory overflowFileInputStream fis = new FileInputStream ("to Youth. mp3");FileOutputStream fos = new FileOutputStream ("Copy.mp3");byte[] arr = new byte[fis.available ()];//make a byte array based on file sizeFis.read (arr);//Read all the bytes on the file into the arrayFos.write (arr);//Writes all the bytes in the array once to the fileFis.close ();Fos.close ();# # #IO流
(upper + ""); data = baiS. read ();} baiS. close ();}}
The running result is as follows:
2. ByteArrayOutputStream class
ByteArrayOutputStream writes data to the byte array in the memory. Its data is a byte array. ByteArrayOutputStream adapter design mode, which converts the byte array type to the output stream type so that the program can write the byte array.
The ByteArrayOutputStream class is constructed as follows:
ByteArrayOutputStream (): Creat
intercept eight bits). As a result, the message is that the Java byte stream reads byte into int and then goes back to byte to save it. Why not?After some thought, I had an initial answer: When reading a byte data with the input stream, there are sometimes 8 consecutive 1 cases, which represents-1 within the computer,
;> 24);//high 8-bit raf.write (i >>>); Raf.write (i >>> 8); Raf.write (i); System.out.println (Raf.getfilepointer ());//You can write a intraf.writeint (i) directly; String s = "medium"; byte[] GBK = s.getbytes ("GBK"); Raf.write (GBK); System.out.println (Raf.length ());//Read the file, you must move the pointer to the head raf.seek (0);//One-time read, read the contents of the file into a byte array byte[
[] {"Dfgha", "Tyui", "ERQQ"};Streaminteger[] Arrayint = new integer[] {10, 20, 50, 40, 60};streamList.stream (). Map (S-s.split (",") [1]). Map (Integer::p arseint). Filter (n-n >= 15). ForEach (System.out::p rintln);After you get the stream, you can use the mapping method: map (lambda expression for conversion)streamint num = integer.parseint (str);return num;});Lambda expressionS->system.out.println (s);Object Name Call member methodSystem.out::p ri
to the outside, if the stream is closed and data is written to the file, it overwrites the data. If it is closed, it appends the data.
2. As long as JAVA establishes a connection with other content (I/O, database connection, socket connection), the first time consumed, and the second time consumed resources, you must disable it when you use it.
3. the byte stream
IO flow is mainly used in hard-core, memory, keyboard and other processing equipment data operation, according to the data type of processing data can be divided into: byte stream (abstract base class is InputStream and OutputStream) and character stream (abstract base class for reader and writer). Depending on the flow direction, it can be divided into: input stream
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.