gpt4 book ai didi

java - JsonGenerator 将 json 数据附加到文件而不覆盖

转载 作者:行者123 更新时间:2023-11-30 07:13:32 27 4
gpt4 key购买 nike

我正在从套接字读取字节,然后使用 jsonGenerator 写入 json 文件。问题是每次从套接字接收到流时,JsonGenerator 都会覆盖该文件。如何让它附加后续流而不是覆盖?

JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(
new File("transactions.json"), JsonEncoding.UTF8);

try {
while ( (bytesRead = in.read(bytes)) != -1 ){ // -1 indicates EOF

output= new String(bytes, "UTF-8");
String length = output.substring(0, 4).trim();
String mti = output.substring(4, 8).trim();
String resp = "000";
String newoutput = "";
String bitmap = output.substring(8, 24);
String stan = output.substring(24, 30);
String date = output.substring(30, 44);
String function_code = output.substring(44, 47);
mti = "1814";


// output to file

generator.writeStartObject();
generator.writeStringField("MTI", mti);
generator.writeStringField("lenght", length);
generator.writeStringField("stan", stan);
generator.writeStringField("date", date);
generator.writeStringField("Function Code", function_code);
generator.writeEndObject();
}
} catch (Exception e) {
System.out.println("Exceptions "+e);
}finally{
generator.close();

}

此外,当我在 while 循环外部声明生成器并由于某种原因在循环外部关闭它时,数据不会写入文件,因此我假设生成器有点像缓冲它,当您关闭它时,它会写入文件。

最佳答案

我可能在你的问题中遗漏了一些东西,但我跳出来的覆盖原因是你没有指定应该附加该文件。大多数 Java API(包括 Jackson)默认采用覆盖而不是附加。解决这个问题的简单方法就是使用:

// the second parameter specifies whether the file should be appended
try(OutputStream fos = new FileOutputStream(new File("transactions.json"), true)) {

// pass the FileOutputStream to the generator instead
JsonGenerator generator = factory.createGenerator(fos , JsonEncoding.UTF8);
}

我的回答就这样,但如果我没有指出如果你同时从多个套接字读取数据,那么你可能最终会得到交错写入的 JSON 数据,那我就失职了。

我建议将该方法包装在某种同步块(synchronized block)中,以防止这种情况发生并使其线程安全。

下面我有一个示例,说明如何重写此功能。

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* A thread-safe class that will append JSON transaction data to a file.
*/
public class TransactionWriter {
private static final JsonFactory jsonFactory = new JsonFactory();

/**
* Helper method that will read a number of UTF8 characters from an input stream and return them in a string.
*
* @param inputStream stream to read from
* @param charsToRead number of characters to read
* @return string of read characters
* @throws IOException when unable to read enough characters from the stream
*/
private static String readUtf8Chars(InputStream inputStream, int charsToRead) throws IOException {
// since we know this is UTF8 up front, we can assume one byte per char
byte[] buffer = new byte[charsToRead];

// fill the buffer
int readBytes = inputStream.read(buffer);

// check that the buffer was actually filled
if(readBytes < charsToRead)
throw new IOException("less bytes available to read than expected: " + readBytes + " instead of " + charsToRead);

// create a string from the buffer
return new String(buffer, StandardCharsets.UTF_8);
}


private final File file;
private final Object writeLock = new Object();

/**
* Constructs a new instance for an output file.
*
* @param file file to append to
*/
public TransactionWriter(File file) {
this.file = file;
}

/**
* Reads a transaction from the input stream and appends a JSON representation to this instance's output file.
*
* @param inputStream stream to read from; will be closed after this method is closed
* @throws IOException when reading or writing failed
*/
public void write(InputStream inputStream) throws IOException {
// since we have multiple threads appending to the same file, synchronize to prevent concurrency issues
synchronized(writeLock) {

// open the output stream to append to the file
try(FileOutputStream outputStream = new FileOutputStream(file, true)) {

// create the generator for the output stream
JsonGenerator generator = jsonFactory.createGenerator(outputStream, JsonEncoding.UTF8);

// write the data to the generator
generator.writeStartObject();
generator.writeStringField("length", readUtf8Chars(inputStream, 4).trim());
generator.writeStringField("MTI", readUtf8Chars(inputStream, 4).trim());
String bitmap = readUtf8Chars(inputStream, 16);
generator.writeStringField("stan", readUtf8Chars(inputStream, 8));
generator.writeStringField("date", readUtf8Chars(inputStream, 14));
generator.writeStringField("Function Code", readUtf8Chars(inputStream, 3));
generator.writeEndObject();

} finally {
// output stream is closed in try-with-resources, but also close the input stream
inputStream.close();
}
}
}
}

需要明确的是,我根本没有测试过这段代码。我只知道它在 Java 7 语言级别上编译。

关于java - JsonGenerator 将 json 数据附加到文件而不覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38784517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com