在我看来,问题在于阅读器和流的混合......
现在,我尝试使用...
try (OutputStream output = new FileOutputStream(filepath);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output))) {
writer.write("Number: 176");
writer.newLine();
writer.write("Title: Elephant");
writer.newLine();
writer.write("Text: This image shows a cute elephant.");
writer.newLine();
writer.flush();
ImageIO.write(image, "png", output);
output.flush();
} catch (Exception exp) {
exp.printStackTrace();
}
写入文件。希望是更新输入流位置,并将内容写入文件末尾...
和
String filepath = "myfile.txt";
BufferedImage image = null;
try (InputStream is = new FileInputStream(new File(filepath));
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String number = br.readLine();
String title = br.readLine();
String text = br.readLine();
System.out.println(number);
System.out.println(title);
System.out.println(text);
ImageInputStream iis = ImageIO.createImageInputStream(is);
image = ImageIO.read(iis);
ImageIO.write(image, "png", new File("test.png"));
} catch (Exception exp) {
exp.printStackTrace();
}
阅读它。
但这似乎不起作用。 “可能”发生的是文件位置没有更新到我们需要它正确读取图像的正确位置。
我做的是这个......
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
public class ReadWriteImage {
public static void main(String[] args) {
try {
writeTest();
readTest();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void readTest() throws IOException {
String filepath = "myfile.txt";
BufferedImage image = null;
try (InputStream is = new FileInputStream(new File(filepath))) {
int lineCount = 0;
StringBuilder sb = new StringBuilder(128);
String number = null;
String title = null;
String text = null;
int b = -1;
while (lineCount < 3 && (b = is.read()) != -1) {
if ((char)b == '\n') {
switch (lineCount) {
case 0:
number = sb.toString();
break;
case 1:
title = sb.toString();
break;
case 2:
text = sb.toString();
break;
}
sb.delete(0, sb.length());
lineCount++;
} else {
sb.append((char)b);
}
}
System.out.println(number);
System.out.println(title);
System.out.println(text);
ImageInputStream iis = ImageIO.createImageInputStream(is);
image = ImageIO.read(iis);
ImageIO.write(image, "png", new File("test.png"));
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static void writeTest() throws IOException {
String filepath = "myfile.txt";
BufferedImage image = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/thumnails/2005-09-29-3957_400.jpg"));
try (FileOutputStream output = new FileOutputStream(filepath)) {
output.write("Number: 176\n".getBytes());
output.write("Title: Elephant\n".getBytes());
output.write("Text: This image shows a cute elephant.\n".getBytes());
ImageIO.write(image, "png", output);
output.flush();
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
基本上,我只是直接使用了OutputStream和InputStream...
更新
文件内容以...开头
Number: 176
Title: Elephant
Text: This image shows a cute elephant.
âPNG
欢迎来到 2021
所以,序数答案是前段时间做出的,世界已经在前进。除非您有非常特殊的不需要(即传输速度/带宽限制),否则我强烈建议您使用 JSON 和 Base64 之类的东西来发送混合在一起的二进制和文本。
主要原因,实际上编码/解码和维护要简单得多。最初的解决方案很难添加新内容,因为两端都需要能够支持更改,而 JSON 之类的东西(如果做得好)可以避免添加和删除内容。构建更复杂的解析工作流也更容易,因为您不依赖于流的“读/写”位置。
这个例子使用了org.json 库,但是你可以使用任何你想要的库。
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws IOException {
new Main();
}
public Main() throws IOException {
read(write());
}
public String write() throws IOException {
BufferedImage image = ImageIO.read(getClass().getResource("/images/Background.png"));
JSONObject jobj = new JSONObject();
jobj.put("number", 176);
jobj.put("title", "Elephant");
jobj.put("text", "This image shows a cute elepant");
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(image, "png", baos);
baos.flush();
byte[] encode = Base64.getEncoder().encode(baos.toByteArray());
String text = new String(encode, StandardCharsets.UTF_8);
jobj.put("image", text);
}
String text = jobj.toString();
return text;
}
public void read(String jsonText) throws IOException {
JSONObject jobj = new JSONObject(jsonText);
System.out.println("Number = " + jobj.getInt("number"));
System.out.println("Title = " + jobj.getString("title"));
System.out.println("Text = " + jobj.getString("text"));
String encodedImage = jobj.getString("image");
byte[] imageBytes = Base64.getDecoder().decode(encodedImage.getBytes(StandardCharsets.UTF_8));
try (ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes)) {
BufferedImage image = ImageIO.read(bais);
JLabel label = new JLabel(new ImageIcon(image));
label.setText("<html>Number = " + jobj.getInt("number") + "<br>Title = " + jobj.getString("title") + "<br>Text = " + jobj.getString("text"));
JOptionPane.showMessageDialog(null, label);
}
}
}
如果我想发送 2 个文件怎么办?
然后使用JSONArray。 org.json 相当强大,有关详细信息,请参阅 Introduction to JSON-Java 。 Google 实现(GSON?)还提供了许多功能,可用于从 POJO 编码/解码 json,如果这是你的事