Who holds a lot of empty dance "- see Java, C # big competition (2)

zhaozj2021-02-17  53

Realize flow operation

The stream data in Java and C # is very similar. If you are familiar with the flow of Java, you will not be too difficult to complete the flow operation in C #. The most important difference between Java and C # implementation operations is not only the appropriate read or write methods that need to be implemented, but also the Stream class in C # can also be used as "readers" or "author" and accurately reflect the attribute of the Stream capacity.

Table 2: Realization of Stream operation in Java and C #

Java C #

read:

Implemented with the read () method in java.io.inputStream, you can also implement other two Read methods in InputStream

The value of the canread attribute in system.io.stream is true, at least one of the read, readbyte, or becomread / endread method can be used.

Positioning:

The SKIP method in java.io.inputStream can be used to move the pointer forward in the file, or you can use the Marksupported, Mark and RESET methods to move the pointer backward.

Use System.io.Stream to notify the program, which can be positioned, using the SEEK method to complete the positioning operation.

write:

Use the Write method in java.io.outputStream to implement write operations (you can also use other WRITE methods in OutputStream to complete your write.)

After returning true values ​​from the CanWrite property in System.io.Stream, at least one of the three methods of WRITE, WRITEBYTE or BeGinWrite / EndWrite is used to complete the write operation.

The Stream class in C # provides a variety of options when completing a certain function, the most important read and write (both need one byte array, offset, length three parameters) These two methods are sufficient. Because all methods are executed, the simple READ / WRITE method will be able to give the required functionality. The default READBYTE and WRITEBYTE will perform conversion between LONG type variables and byte arrays, asynchronously executing the BeginRead and BeginWrite methods to execute the READ or WRITE method in a stand-alone thread.

Most of this manuscript is discussed by the System.io.Stream class in C #, here we also need to discuss the two classes of System.io.TextReader and System.io.TextWriter, these two classes The Java handling I / O is very similar, one of which is responsible for processing the read operation, and the other class is responsible for processing writing. The stream object in C # masks how to read, write related details, TextReader and TextWriter, separately store the byte of the read or written, respectively. The most common class derived from the above two is System.io.StreamReader and System.io.StreamWrtiter class, which can operate a Stream object, and System.Text.Encoding object specifies a word throttle? Convert to a character stream. (By default, C # uses UTF-8 to encode / decode.)

If you need to use a stream similar to the stream, you need to operate the characters instead of bytes, using the subclasses of the TextReader and TextWriter more simpler than using the Stream class. Although StreamReader and StreamWriter class implement stream operation can be implemented using StreamReader and StreamWriter class if Stream is used.

File system I / O

Completion of disk operation in Java is very simple, usually it is to operate java.io.file objects and use java.io.fileinputStream or java.io.fileoutputStream. As we see above, C # is the same as Java in many ways, but there are some differences. Like Java, there is no particularly close connection between the C # file object and the underlying file system. We can create a file object for a non-existing file, or create a File object for the existing file, and move it elsewhere in the case where C # does not know. Due to the static method of a stream with CreateText or AppendText, the file system returns a stream, and there is different ways with Java, the role of the File object in C # is much more important.

To create a new file or write content to the file in Java, you must use FileInputStream:

FileOutputStream Fos = New FileOutputStream ("Brand-New-file.txt");

Fos.write (...)

But you can use it in C #:

Stream s = file.create ("Brand-new-file.txt");

Or use:

Streamwriter SW = file.createtext ("Brand-new-file.txt");

Come get a new file Stream or streamWriter. (Add in Java can be done by setting one of the build attributes of the FileOutputStream builders.) Java allows Java.IO.FileInputStream to read content from the file, while C # has static methods such as Open WRITE and OpenText. Finally, C # provides more detailed control in the Open method, this method allows the file permission and access environment.

Table 3: Method for reading and writing of operating files

Java C #

Create a new file:

Use java.io.fileoutputstream

Use static file.create method, static createText or instance method method CreateText

Write the file:

Use java.io.fileoutputStream.

Use a static or instance of the OpenWrite method.

Add text to your file:

Use Java.IO.FileOutputStream, but you need to use a method with an Append parameter.

Use a static or instance of the appendtext method.

Read text from the file:

Use java.io.fileoutputStream.

The OpenRead, OpenText method of fate dynamics or instances.

The improvement in C # is a file.copy method. What is involuntarulus most Java programmers is related to the file system I / O problem that the file cannot be moved correctly. Java.IO.File contains a RenameTo method that renames files, but it is only valid within the file system. In most cases, programmers must write their own file mobility commands, usually use java.io.fileinputstream and java.io.fileoutputstream copy files, and then delete the original file. The COPY method in C # can be easily moving files, but File.Move cannot move files across volumes and file systems.

The C # file system does not have to deal with the cross-platform problem that Java must handle, so there is no object similar to java.io.pathsepa Gu or java.io.file.seParator function. Unfortunately, this means that there is no Java.IO.File constructor who is loved by the majority of programmers in C #:

Public File (File Parent, String Child) Using C # programmers can use the following command to construct a new System.IO.File object:

FILE PARENT ...

File Child New File (Parent.FullName "//" ChildName);

Understanding networking

Two languages ​​provide some abstract layers around the basic protocol. The abstraction of Java.Net.Socket classes in Java is much higher than the System.Net.Sockets. Socket class in C #.

Java and C # provide different abstraction layers of the network, and programmers can use different network interfaces to complete the operation of the network.

Table 4: Network level in Java and C #

Java C #

Answer / Request:

Java.net.url and java.net.urlconnection.

System.Net.WebRequest.

protocol:

TCP / IP protocol uses java.net.socket and java.net.serversocket;

The UDP protocol uses java.net.datagramsocket and java.net.multicastsocket.

CP / IP protocol uses system.net.sockets.tcplistener and system.net.sockets.tcpclient;

UDP protocol uses tsystem.net.sockets.udpclient

Original socket layer:

No.

System.net.sockets.socket

The response / request layer can be used for the request of the HTTP class, where one of which starts to start a connection, send some bytes of data, then stop, wait for the other party as a response sent back to some bytes. For more flexible operations like streams, the protocol layer is used in larger. For most Java programmers, unless you need to complete a very high network operation, you don't need to directly control your socket. If needed. C # still provides the ability to control the original Berkeley socket.

转载请注明原文地址:https://www.9cbs.com/read-29930.html

New Post(0)