如何使用FileOutputStream编写图像

时间:2016-12-20 03:36:29

标签: java file

我想知道如何使用FileOutputStream来编写图像,因为FileOutputStream用于字节数据,如图像,视频和音频,否则对于文本数据来说,它更好,更充分地使用FileReader 1}}和FileWriter所以我想要的是我有一个img.png文件,我可以使用

阅读它
FileInputStream fin=new FileInputStream(new File("C:\\Folder1\\img.png"));

并使用read方法阅读它,当时我想使用

创建具有不同名称的相同图像
FileOutputStream fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));

并使用fout.write编写它们但是当我这样做时,图像被创建但无法看到它并且其大小以字节为单位

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fileinputstreamexample;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 *
 * @author Love Poet
 */
public class FileInputStreamExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        FileOutputStream fout = null;
        FileInputStream fin=new FileInputStream(new File("C:\\Folder1\\img.png"));
        int i;
        while((i=fin.read())!=-1)
        {
            fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));
            fout.write(i);
            fout.flush();
        }
        System.out.println("File readed Succesfully");
        System.out.println("File Written Succesfully");
        fout.close();
        fin.close();
    }

}

5 个答案:

答案 0 :(得分:1)

public class DownloadImage {
    public static void main(String[] args){
        FileOutputStream out = null;
        FileInputStream in = null;
        int cursor;
    try{
        in = new FileInputStream(new File("C:\\Users\\ganesh.r\\Desktop\\My Stuff\\dp.jpeg"));
        out = new FileOutputStream("TestImage.jpeg");
        while((cursor = in.read())!=-1){
            out.write(cursor);
        }
    }
    finally{
        if(in!=null){
            in.close();
        }
        if(out!=null){
            out.close();
        }
        System.out.println("Read and Write complete");
    }
    }

}

希望这有帮助

答案 1 :(得分:0)

只需将文件打开到循环外部。

而不是

while((i=fin.read())!=-1)
        {
            fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));
            fout.write(i);
            fout.flush();
        }

这样做

fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));
while((i=fin.read())!=-1)
        {
            fout.write(i);
            # flush inside loop only if the chunks u r reading are HUGE, and u want+accomodate to partial flush.
        }
       fout.flush();

答案 2 :(得分:0)

try {
    FileInputStream fin=new FileInputStream(new File("C:\\Folder1\\img.png")); 
    FileOutputStream fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));

        int content;
        while ((content = fin.read()) != -1) {
            fout.write(content);
         }

        System.out.println("Finished");
    } catch (IOException e) {
        e.printStackTrace();
    } 

答案 3 :(得分:0)

您正在为每个字节输入重新创建文件。

以下内容远比目前发布的所有逐字节解决方案更有效:

FileInputStream fin=new FileInputStream(new File("C:\\Folder1\\img.png")); 
FileOutputStream fout=new FileOutputStream(new File("C:\\Folder1\\newimg.png"));

int count;
byte[] buffer = new byte[8192]; // or more if you like
while ((count = fin.read(buffer)) != -1) {
    fout.write(buffer, 0, count);
}

答案 4 :(得分:0)

//尝试用于在Word文件中添加图像-

    XWPFDocument document = new XWPFDocument();
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();


    run.addBreak();
    run.setText("Name File-test" );
    run.addBreak();


    String image;
    String testpath;
    File file = new File("Path\\Images\\");
    File[] files = file.listFiles();
    for (File f : files) {
        int format = Document.PICTURE_TYPE_JPEG;

        if (f.getName().endsWith(".png")) {
            image = f.getName();
            testpath = f.getAbsolutePath();
            // System.out.println(testpath);
            // System.out.println(image);
            run.addBreak();
            run.setText(image);
            run.addBreak();
            run.addPicture(new FileInputStream(testpath), format, testpath, Units.toEMU(400), Units.toEMU(500));
        }
    }
   FileOutputStream wordDoc =
        new FileOutputStream("Path- .docx");
    document.write(wordDoc);
    wordDoc.close();