使用套接字读取字节文件 - Java

时间:2022-03-21 20:20:56

I have a byte file that contains tcp packets. I want to use sockets to read those packets.

我有一个包含TCP包的字节文件。我想使用套接字来读取这些数据包。

so is it possible to use Sockets to read this file without connection ?

那么是否可以使用套接字来读取此文件而无需连接?

2 个解决方案

#1


2  

FileInputStream mInStream = new FileInputStream("file path").
byte[] buffer = new byte[1024]; 

    // Keep listening to the InputStream 
    while (true) {
        try {
         bytes = mInStream.read(buffer, 0, buffer.length);
        }catch {} 
    }

#2


1  

No, but you can use a FileInputStream and read bytes from the files like the following snippet:

不,但您可以使用FileInputStream并从文件中读取字节,如下面的代码段:

byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream("path_to_file").

while((fis.read(buffer) != -1)
    // do something with the bytes readed

#1


2  

FileInputStream mInStream = new FileInputStream("file path").
byte[] buffer = new byte[1024]; 

    // Keep listening to the InputStream 
    while (true) {
        try {
         bytes = mInStream.read(buffer, 0, buffer.length);
        }catch {} 
    }

#2


1  

No, but you can use a FileInputStream and read bytes from the files like the following snippet:

不,但您可以使用FileInputStream并从文件中读取字节,如下面的代码段:

byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream("path_to_file").

while((fis.read(buffer) != -1)
    // do something with the bytes readed