> For the complete documentation index, see [llms.txt](https://pcap.ardikars.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pcap.ardikars.com/getting-started/write-the-packets-to-a-capture-file.md).

# Write the packets to a capture file

Note: Pcap.dumpOpen(String) will override if the "savefile" already exists; If you need to add a captured packet to the existing "savefile" you can use Pcap.dumpOpenAppend(String) instead.

Docs[: libpcap](https://www.tcpdump.org/manpages/pcap.3pcap.html), [javadoc](https://javadoc.io/doc/com.ardikars.pcap/pcap-spi/latest/pcap/spi/Dumper.html).

{% tabs %}
{% tab title="Java" %}

```
public static void main(String[] _args)
    throws ErrorException, PermissionDeniedException, PromiscuousModePermissionDeniedException,
        TimestampPrecisionNotSupportedException, RadioFrequencyModeNotSupportedException,
        NoSuchDeviceException, ActivatedException, InterfaceNotUpException,
        InterfaceNotSupportTimestampTypeException {
  var service = Service.Creator.create("PcapService");
  try (var pcap = service.live(service.interfaces(), new DefaultLiveOptions())) {
    try (var dumper = pcap.dumpOpen("savefile.pcap")) {
      try {
        pcap.loop(
            10,
            (args, header, buffer) -> {
              System.out.println("Args     : " + args);
              System.out.println("Header   : " + header);
              System.out.println("Packet   : " + buffer);
              dumper.dump(header, buffer);
            },
            "Hello pcap!");
      } catch (BreakException e) {
        System.err.println(e.getMessage());
      } catch (ErrorException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```
fun main() {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), DefaultLiveOptions())
  val dumper = pcap.dumpOpen("savefile.pcap")
  try {
    pcap.loop(
      10,
      { args: String, header: PacketHeader, buffer: PacketBuffer ->
        println("Args     : $args")
        println("Header   : $header")
        println("Packet   : $buffer")
        dumper.dump(header, buffer)
      },
      "Hello pcap!"
    )
  } catch (e: BreakException) {
    System.err.println(e.message)
  } catch (e: ErrorException) {
    System.err.println(e.message)
  }
  dumper.close()
  pcap.close()
}
```

{% endtab %}

{% tab title="Scala" %}

```
def main(_args: Array[String]): Unit = {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), new DefaultLiveOptions())
  val dumper = pcap.dumpOpen("savefile.pcap");
  try pcap.loop(10, (args: String, header: PacketHeader, buffer: PacketBuffer) => {
    println("Args     : ", args)
    println("Header   : ", header)
    println("Packet   : ", buffer)
    dumper.dump(header, buffer)
  }, "Hello pcap!")
  catch {
    case e: BreakException =>
      System.err.println(e.getMessage)
    case e: ErrorException =>
      System.err.println(e.getMessage)
  }
  dumper.close()
  pcap.close()
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pcap.ardikars.com/getting-started/write-the-packets-to-a-capture-file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
