Home > Java > javaTutorial > body text

Memo for beginners to learn Java (5)

黄舟
Release: 2016-12-20 13:49:45
Original
1097 people have browsed it


Supplement some basic knowledge of Java.

1. Exceptions
Java’s handling of exceptions is the same as that of Delphi. It is not to deliberately avoid its occurrence, but to remedy it after it occurs.
Delphi’s exception handling is simply the statement

Try
Except/ /After an exception occurs, it will be transferred here for execution
Finally//No matter whether the exception occurs or not, it will be transferred here for execution
End

Similar to this, the basic form of Java's exception handling is as follows
try{
}catch( ExceptionType1 e){
file://Handling of exception 1
}catch(ExceptionType2 e){
file://Handling of exception 2
throw(e)//Throw an exception, and raise in Delphi It's the same thing
}

What I want to add is that for most exceptions, if you want to explicitly throw them in a normally running program rather than a program that catches exceptions, the Java compiler needs you to tell you what you want in advance. The thrown exception must be declared, otherwise the compilation will not be allowed. This task is completed by throws.

2. Java’s input and output stream
2.1 Output
System.out.PRint file:// Here out is a static method Oh
System.out.println
System.err.print file://err is also the standard output like out. As for the difference, I don’t know yet
System.err.println
2.2 Input
System.in.read ()
2.3 File operations
Only a few annotated examples are enough.
The first one is a program that displays basic information about files

import java.io.*;//Transfer to io-related classes
class fileinfo{
file://Note, the main function must be a static method

public static void main(String args[])throws IOException{
File fileToCheck;//Create an instance using a file object
if (args.length>0){
for (int i=0;ifileToCheck=new File(args[i]);//Allocate space for the file object
info(fileToCheck);//The info referenced here must be a static method member
}
}
else{
System.out.println( "no file given");
}
}

public static void info(File f)throws IOException{
System.out.println("Name:"+f.getName());
System.out.println( "Path:"+f.getPath());
if (f.exists()){
System.out.println("File exists.");
System.out.print((f.canRead()? " and is Readable":""));//Determination function, if the conditions are met, output the former, otherwise output the latter
System.out.print((f.canWrite()?"and is Writable":"") );
System.out.print(".");
System.out.println("File is"+f.length()+"bytes.");
}
else{
System.out.println( "File does not exist.");
}
}
}

The second example is a small program that stores phone information. The user enters the name and phone number, and the program stores it in the phone.numbers file through FileOutputStream To achieve

import java.io.*;

class phones{
static FileOutputStream fos;
public static final int lineLength=81;
public static void main(String args[])throws IOException{
byte[] phone= new byte[lineLength];
byte[] name=new byte[lineLength];
int i;
fos=new FileOutputStream("phone.numbers");
while(true){
System.err.println("Enter a name(enter ′done′ to quit)");
readLine(name);
if ("done".equalsIgnoreCase(new String(name,0,0,4))){
break;
}
System. err.println("Enter the phone number");
readLine(phone);
for (i=0;phone[i]!=0;i++){
fos.write(phone[i]);
}
fos.write(′,′);
for (i=0;name[i]!=0;i++){
fos.write(name[i]);
}
fos.write(′ ′);
}
fos.close();
}

private static void readLine(byte line[])throws IOException{
int i=0,b=0;
while((i<(lineLength-1))&&(( b=System.in.read())!=′ ′)){
line[i++]=(byte)b;
}
line[i]=(byte)(0);
}
}

2.4 Streams
are nothing more than two
output streams, let us write
input streams, and let us read
There are many types of input and output streams in the java.io package
1. FileInputStream and FileOutputStream node streams
2. BufferedInputStream and BufferedOutputStream filtered stream
3. DataInputStream and DataOutputStream Enhanced filtered stream
4. PipedInputStream and PipledOutputStream Streams for threads

Once you have grasped the concept of streams, you can start learning Sockets. Regarding the role of Sockets, I already talked about it yesterday .

Now, we will create a simple communication program to gain substantial familiarity with Socket. The program consists of two parts, the client (RemoteFileClient) and the server (RemoteFileServer). The client makes a request to the server, asking Read the file information on the server. The server will respond to the request, pass the corresponding file information to the client, and pass the corresponding file information to the client.

First we create the RemoteFileClient class:
import java.io.*;/ The /java.io package provides tools for reading and writing streams, and is also the only way to communicate with TCP sockets
import java.net.*;//The java.net package provides socket tools.

public class RemoteFileClient {
protected String hostIp;
protected int hostPort;
protected BufferedReader socketReader;//Object responsible for reading data
protected PrintWriter socketWriter;//Object responsible for writing data

The constructor of the file:// class has two parameters: the IP address of the remote host (hostIp) and the port number ( hostPort). The constructor assigns them to instance variables
public RemoteFileClient(String aHostIp, int aHostPort) {
hostIp = aHostIp;
hostPort = aHostPort;
}
public static void main(String[] args) {
}
file://Connect to the remote server
public void setUpConnection() {
}
file://Request file information from the remote server
public String getFile(String fileNameToGet) {
}
file://Disconnect from the remote server Open
public void tearDownConnection() {
}
}

First implement main()
public static void main(String[] args) {
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1", 3000);// In order to facilitate debugging, we treat the local server as a remote server
remoteFileClient.setUpConnection();//Connect. You cannot use setUpConnection directly because it is a non-static variable. You need to reference the instance after creating it. You can read my diary on the first day. It is very specific.
String fileContents =
remoteFileClient.getFile("RemoteFile.txt") ;//Read

remoteFileClient.tearDownConnection();//Disconnect

System.out.println(fileContents);//Output the read content
}

The steps are very clear. Then we look at the connection and reading separately , how to realize disconnection
1. Connection
public void setUpConnection() {
try {
Socket client = new Socket(hostIp, hostPort);//Create Socket object

OutputStream outToServerStream=client.getOutputStream();
InputStream inFromServerStream=client.getInputStream();
socketReader = new BufferedReader(new InputStreamReader(inFromServerStream));
file://Wrap Socket's InputStream into BufferedReader so that we can read the lines of the stream

socketWriter = new PrintWriter( outToServerStream);
file://Wrap Socket's OutputStream into PrintWriter so that we can send file requests to the server

} catch (UnknownHostException e) {
System.out.println("Error setting up socket connection: unknown host at " + hostIp + ":" + hostPort);
file://Exception handling for Socket object creation error
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e) ;
file://Exception handling for IO errors
}
}

2. Read
public String getFile(String fileNameToGet) {
StringBuffer fileLines = new StringBuffer();//StringBuffer objects are also String objects, but are smaller than It is more flexible, here is used to store the read content

try {
socketWriter.println(fileNameToGet);
socketWriter.flush();//The file storage address is output to the socketWriter, and then the buffer is cleared to let this address Send to the server

String line = null;
while ((line = socketReader.readLine()) != null)
fileLines.append(line + " ");
file://Now that it has been sent to the server , then we all have to wait for the response. The program here is to wait for the server to pass the file content we need
} catch (IOException e) {
System.out.println("Error reading from file: " + fileNameToGet);
}

return fileLines.toString();//Don’t forget to convert the content in the buffer to String and return
}

3. Disconnect
public void tearDownConnection() {
try {
socketWriter.close() ;
socketReader.close();
} catch (IOException e) {
System.out.println("Error tearing down socket connection: " + e);
}
}

tearDownConnection() method just don’t close what we are doing BufferedReader and PrintWriter are created on Socket's InputStream and OutputStream. Doing this will close the underlying stream we get from the Socket, so we must catch possible IOException

Okay, now we can summarize the steps to create the client program
1. Instantiate the Socket with the IP port number of the machine to be connected ( Throw Exception if there is a problem).
2. Get the stream on the Socket for reading and writing.
3. Pack the stream into an instance of BufferedReader/PrintWriter.
4. Read and write the Socket. Specifically, send the file address information to the server on the Writer. Read the file information from the server on Reader
5. Close the open stream.

The following is the code list of RemoteFileClient

import java.io.*;
import java.net.*;

public class RemoteFileClient {
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
protected String hostIp;
protected int hostPort ;

public RemoteFileClient(String aHostIp, int aHostPort) {
hostIp = aHostIp;
hostPort = aHostPort;
}
public String getFile(String fileNameToGet) {
StringBuffer fileLines = new StringBuffer();

try {
socketWriter. println(fileNameToGet);
socketWriter.flush();

String line = null;
while ((line = socketReader.readLine()) != null)
fileLines.append(line + " ");
} catch (IOException e) {
System.out.println("Error reading from file: " + fileNameToGet);
}

return fileLines.toString();
}
public static void main(String[] args) {
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1", 3000);
remoteFileClient.setUpConnection();
String fileContents = remoteFileClient.getFile("RemoteFile.txt");
remoteFileClient.tearDownConnection();

System.out.println(fileContents);
}
public void setUpConnection() {
try {
Socket client = new Socket(hostIp, hostPort);

OutputStream outToServerStream=client.getOutputStream();
InputStream inFromServerStream=client.getInputStream();
socketReader = new BufferedReader(new InputStreamReader(inFromServerStream));
socketWriter = new PrintWriter(outToServerStream);

} catch (UnknownHostException e) {
System.out.println("Error setting up socket connection: unknown host at " + hostIp + ":" + hostPort);
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
} catch (IOException e) {
System.out.println("Error tearing down socket connection: " + e);
}
}
}

好了,现在来看服务器端的程序怎么写.
创建RemoteClientServer类:
import java.io.*;
import java.net.*;

public class RemoteFileServer {
protected int listenPort = 3000;
public static void main(String[] args) {
}
public void acceptConnections() {
}
public void handleConnection(Socket incomingConnection) {
}
}

跟客户机中一样,首先导入 java.net 的 java.io。接着,给我们的类一个实例变量以保存端口,我们从该端口侦听进入的连接。缺省情况下,端口是 3000。
acceptConnections()将答应客户机连接到服务器
handleConnection()负责与客户机 Socket 交互以将您所请求的文件的内容发送到客户机。

首先看main()

public static void main(String[] args) {
RemoteFileServer server = new RemoteFileServer();
server.acceptConnections();
}
非常简单,因为主函数无非是让服务器进入监听状态,所以直接调用acceptConnection().需要注重的是,必须先创建RemoteFileServer()的实例,而不是直接调用.

那么服务器是怎样通过acceptConnection()来监听客户机的连接呢?并且假如兼听到了,又怎样处理呢?我们来看
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort);//同客户机的Socket对应,在服务器端,我们需要ServerSocket对象,参数是兼听的端口号
Socket incomingConnection = null;//创建一个客户端的Socket变量,以接收从客户端监听到的Socket
while (true) {
incomingConnection = server.accept();//调用该 ServerSocket 的 accept() 来告诉它开始侦听,
handleConnection(incomingConnection);
}
file://不断监听直到来了一个连接请求,然后交由handleConnection处理
} catch (BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
}
}

无论何时假如创建了一个无法绑定到指定端口(可能是因为别的什么控制了该端口)的 ServerSocket,Java 代码都将抛出一个错误。所以这里我们必须捕捉可能的 BindException。同时,与在客户机端上时一样,我们必须捕捉 IOException,当我们试图在 ServerSocket 上接受连接时,它就会被抛出。可以通过用毫秒数调用 setSoTimeout() 来为 accept() 调用设置超时,以避免实际长时间的等待。调用 setSoTimeout() 将使 accept() 经过指定占用时间后抛出 IOException

最要害的处理在handleConnection()中,这时已经连接到了客户端的Socket,要从该Socket中读取客户端的请求并且响应。

public void handleConnection(Socket incomingConnection) {
try {
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();

file://首先获取同Socket相关联的流outputToSocket和InputStream
file://其中outputToSocket是要返回给客户端Socket的流
file://InputStream是客户端发来的请求,在这里就是文件路径,即"RemoteFile.txt"

BufferedReader streamReader =
new BufferedReader(new InputStreamReader(inputFromSocket));

file://首先要将InputStream转换到BufferedReader中

FileReader fileReader = new FileReader(new File(streamReader.readLine()));
file://Read the file path from BufferedReader and create a new object FileReader

BufferedReader bufferedFileReader = new BufferedReader(fileReader);

file:/ /Create the BufferedReader object again, this time it reads the contents of the file

PrintWriter streamWriter =
new PrintWriter(OutputStream);

file://Pack the Socket's outputToSocket stream into PrintWriter so that we can send file information to Client

String line = null;
while ((line = bufferedFileReader.readLine()) != null) {
streamWriter.println(line);
}
file://Read the file information from bufferedFileReader, and then Output to the client via streamWriter

fileReader.close();
streamWriter.close();//Pay attention to the order in which the two streams of the Socket are closed
streamReader.close();
file://Close all streams after completion

} catch (Exception e) {
System.out.println("Error handling a client: " + e);
}
}

Please pay attention to the order in which the stream is closed after completing all operations. The streamWriter is closed after the streamReader is closed. Before. This is not accidental. If the closing order is reversed, the client will not get any file information. You can debug it and see. Why is this? The reason is that if you close the streamReader before closing the streamWriter, you can go Nothing is written in streamWriter, but no data can pass through the channel (the channel is closed). But the strange thing is, haven't I already output in the previous streamWriter.println()? Do I have to wait until all streams are closed? Only the information output to the client can be reached? I tried to block
streamWriter.close();
streamReader.close();
to see if normal communication can still be achieved. It turned out that it didn’t work and the program crashed. Maybe it was Because the channel is not closed, it can at least be explained that only by closing the channel normally in a certain order can the transmission of communication data be completed, otherwise the client will not receive the information.

Finally, let’s summarize the steps for creating a server-side program.
1. Instantiate a ServerSocket with a port you want it to listen to for incoming client connections (such as 3000 in the program) (throw an Exception if there is a problem).
2. Call ServerSocket's accept() in a loop to listen for connections
3. Get the client's Socket stream for read and write operations
4. Pack the stream
5. Read and write the client's Socket
6. Close the open stream (remember , never close the Reader before closing the Writer), complete the communication

The following is the code list of
RemoteFileServer

import java.io.*;
import java.net.*;

public class RemoteFileServer {
int listenPort;
public RemoteFileServer(int aListenPort) {
listenPort = aListenPort;
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort);
Socket incomingConnection = null;
while (true) {
incomingConnection = server .accept();
handleConnection(incomingConnection);
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch (IOException e) {
System. out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
}
}
public void handleConnection(Socket incomingConnection) {
try {
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection. getInputStream();

BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));

FileReader fileReader = new FileReader(new File(streamReader.readLine()));

BufferedReader bufferedFileReader = new BufferedReader(fileReader);
PrintWriter streamWriter = new PrintWriter(outputToSocket);
String line = null;
while ((line = bufferedFileReader.readLine()) != null) {
streamWriter.println(line);
}

fileReader.close();
streamWriter.close();
streamReader.close();
} catch (Exception e) {
System.out.println("Error handling a client: " + e);
}
}
public static void main( String[] args) {
RemoteFileServer server = new RemoteFileServer(3000);
server.acceptConnections();
}
}

Okay, I finally got started with Socket

The above is the memo (5) for beginners learning Java Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!