You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Groovy实现类似Python的二进制文件读取与文本替换功能?

Reading a Binary File and Replacing Text in Groovy

Absolutely! Groovy makes this task straightforward with its concise syntax and built-in file handling tools. Here's a practical breakdown and working code examples to help you implement this functionality.

Core Approach

Binary files are just streams of bytes, so the process follows three key steps:

  1. Read the file's bytes and decode them into a string (using the correct character encoding for your file).
  2. Perform the text replacement on the decoded string.
  3. Encode the modified string back into bytes and write it to the file (either overwriting the original or saving to a new file).

Basic Implementation (Small to Medium Files)

This works great for files that fit comfortably in memory:

// Define your file path and replacement details
def filePath = "/path/to/your/file.bin"
def targetText = "text-you-want-to-replace"
def replacementText = "new-text-here"
def fileCharset = StandardCharsets.UTF_8 // Use your file's actual encoding (e.g., ISO-8859-1, Windows-1252)

// Read, decode, and replace text
def originalContent = new File(filePath).getBytes().toString(fileCharset)
def modifiedContent = originalContent.replace(targetText, replacementText)

// Write the modified content back to the file
new File(filePath).write(modifiedContent.getBytes(fileCharset))

Handling Large Files (Memory-Friendly Stream Approach)

For large files where loading the entire content into memory isn't feasible, use stream-based processing to read/write in chunks:

def inputFile = "/path/to/large-binary-file.bin"
def outputFile = "/path/to/modified-file.bin"
def targetText = "old-text"
def replacementText = "new-text"
def fileCharset = StandardCharsets.UTF_8

// Process the file in chunks to avoid memory overload
new File(outputFile).withWriter(fileCharset) { writer ->
    new File(inputFile).withReader(fileCharset) { reader ->
        char[] buffer = new char[4096] // 4KB chunk size (adjust as needed)
        int bytesRead
        while ((bytesRead = reader.read(buffer)) != -1) {
            String chunk = new String(buffer, 0, bytesRead)
            writer.write(chunk.replace(targetText, replacementText))
        }
    }
}

Critical Notes to Avoid Issues

  • Encoding is Non-Negotiable: Always use the character encoding that matches your binary file. Using the wrong encoding will cause garbled text and failed replacements. If you're unsure, test with ISO-8859-1 (it preserves all byte values as characters).
  • Watch for Binary File Structure: Be careful with strictly formatted binary files (e.g., EXEs, ZIPs, databases). Replacing text can alter byte lengths and break the file's structure. Only use this method if the file contains plain text embedded in a binary container that can tolerate such changes.

内容的提问来源于stack exchange,提问作者mightguy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:05:56