在 java 中使用 InputStream 和 OutputStream 传输时图像不显示

image not showing when transferred using InputStream and OutputStream in java

这是我的测试程序。我需要它来申请 somewhere.This 可能很小,很抱歉。但我仍然是初学者。请帮助我。

try{
        File file1 = new File("c:\Users\prasad\Desktop\bugatti.jpg");
        File file2 = new File("c:\Users\prasad\Desktop\hello.jpg");
        file2.createNewFile();
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
        String data = null;
        StringBuilder imageBuild = new StringBuilder();
        while((data = reader.readLine())!=null){
            imageBuild.append(data);
        }
        reader.close();
        BufferedWriter writer = new BufferedWriter(new PrintWriter(new FileOutputStream(file2)));
        writer.write(imageBuild.toString());
        writer.close();
    }catch(IOException e){
        e.printStackTrace();
    }

这是文件1

这是文件 2

您可以执行以下两个操作之一:

private static void copyFile(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}

或者,如果您想使用流,也可以这样:

private static void copyFile(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
    input = new FileInputStream(source);
    output = new FileOutputStream(dest);
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buf)) > 0) {
        output.write(buf, 0, bytesRead);
    }
} finally {
    input.close();
    output.close();
}
}

图像不包含线条甚至字符。因此,您不应该使用 readLine() 甚至 ReadersWriters. 您应该直接使用输入和输出流重写复制循环。