Java 如何使用FileOutputStream写入图像
我想知道如何使用Java 如何使用FileOutputStream写入图像,java,file,Java,File,我想知道如何使用FileOutputStream编写图像,因为FileOutputStream用于像image这样的字节数据,视频和音频对于文本数据来说,使用FileReader和FileWriter更好、更充分,所以我想做的是我有一个img.png文件,我可以使用 FileInputStream fin=new FileInputStream(new File("C:\\Folder1\\img.png")); 然后使用read方法读取,当时我想使用 FileOutputStream fou
FileOutputStream
编写图像,因为FileOutputStream
用于像image这样的字节数据,视频和音频对于文本数据来说,使用FileReader
和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();
}
}
只需将文件打开到循环外部即可
而不是
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();
希望这对您为每个字节输入重新创建文件有所帮助
与目前发布的所有字节解决方案相比,以下解决方案的效率要高得多:
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);
}
//尝试使用在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();
发布完整的代码,你在哪里读写我没问题,我发现我的错误很好,谢谢你的努力,我的错误是我在循环中一次又一次地创建新图像:-)我的代码一天比一天增加非常好:-p不要在循环中刷新。
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);
}
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();