Java DataOutputStream writeLong()方法与示例

DataOutputStream类writeLong()方法 (DataOutputStream Class writeLong() method)

  • writeLong() method is available in java.io package.

    writeLong()方法在java.io包中可用。

  • writeLong() method is used to write the given long value to the basic DataOutputStream as 8 bytes (i.e. 64 bit) and the variable counter is plus by 8 on successful execution.

    writeLong()方法用于将给定的long值作为8个字节(即64位)写入基本DataOutputStream,并且成功执行时变量计数器加8。

  • writeLong() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    writeLong()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • writeLong() method may throw an exception at the time of writing long.

    writeLong()方法在写入很长时间时可能会引发异常。

    IOException: This exception may throw while getting any input/output error.

    IOException :在获取任何输入/输出错误时,可能引发此异常。

Syntax:

句法:

    public final void writeLong(long val);

Parameter(s):

参数:

  • long val – represents the long value to be written to the data output stream.

    long val –表示要写入数据输出流的long值。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example 
// of void writeLong(long val) method 
// of DataOutputStream

public class GetSignersOfClass {
 public static void main(String[] args) throws Exception {
  // Creating an instance of String
  String str = new String();

  // It returns the Class object represented by the String class
  // object
  Class cl = str.getClass();

  // By using getSigners() method is to get the signers of the Class
  Object[] o = cl.getSigners();
  System.out.println(cl.getName() + " " + "Signers :" + o);
 }
}

Output

输出量

java.lang.String Signers :null


翻译自: https://www.includehelp.com/java/dataoutputstream-writelong-method-with-example.aspx

要实现在Java中添加一个可以发送文件的功能,可以使用Java的Socket编程来实现。以下是一个简单的示例代码: ```java import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import javax.swing.JFileChooser; import javax.swing.JFrame; public class SendFileActionListener implements ActionListener { private Socket socket; public SendFileActionListener(Socket socket) { this.socket = socket; } @Override public void actionPerformed(ActionEvent e) { JFrame parent = new JFrame(); JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(parent); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { FileInputStream fis = new FileInputStream(file); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); // 发送文件名和文件长度 dos.writeUTF(file.getName()); dos.writeLong(file.length()); // 发送文件内容 byte[] buffer = new byte[4096]; int read = 0; while ((read = fis.read(buffer)) > 0) { dos.write(buffer, 0, read); } fis.close(); dos.flush(); } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 在这个示例中,我们创建了一个名为SendFileActionListener的ActionListener类,它接收一个Socket对象作为构造函数的参数。在actionPerformed方法中,我们首先使用JFileChooser来让用户选择要发送的文件。然后,我们打开这个文件并使用DataOutputStream将文件名和文件长度发送到服务器端。接着,我们使用一个循环来读取文件内容并将其发送到服务器端。最后,我们关闭文件输入流和数据输出流,以及刷新数据输出流。 在主程序中,我们可以将这个ActionListener添加到一个按钮或菜单项中,以便用户可以点击来触发文件发送操作。例如: ```java Socket socket = new Socket("localhost", 8080); SendFileActionListener sendFileActionListener = new SendFileActionListener(socket); JButton sendButton = new JButton("Send File"); sendButton.addActionListener(sendFileActionListener); ``` 在这个示例中,我们创建了一个名为socket的Socket对象,它连接到本地主机的8080端口。然后,我们创建了一个名为sendButton的JButton对象,并将SendFileActionListener添加为其ActionListener。这样,当用户点击sendButton时,就会触发文件发送操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值