62,634
社区成员




public class SwitchWriter
{
public static void main(String[] args) throws IOException
{
InputStream input = new FileInputStream(new File("D:\\杂乱\\头像.jpg"));
StringBuffer buff = new StringBuffer();
byte[] img = new byte[1024];
while (input.read(img) != -1)
{
buff.append(new String(img));
}
//生成图片失败,这时怎么回事啊?
OutputStreamWriter output = new OutputStreamWriter(
new FileOutputStream("D:\\杂乱\\头像_new.jpg"));
String str = buff.toString();
output.write(str, 0, str.length());
output.flush();
output.close();
input.close();
}
}
public class SwitchWriter {
public static void main(String[] args) throws IOException {
InputStreamReader input = new InputStreamReader(new FileInputStream(
new File("E:\\娱乐\\图片\\我的收藏\\头像\\头像.gif")), "GBK");
OutputStreamWriter output = new OutputStreamWriter(
new FileOutputStream(new File(
"E:\\娱乐\\图片\\我的收藏\\头像\\头像_new.gif")), "GBK");
int img;
while ((img = input.read()) != -1) {
output.write(img);
}
output.flush();
output.close();
input.close();
}
}
package com.harderxin.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SwitchWriter
{
public static void main(String[] args) throws IOException
{
InputStream input = new FileInputStream(new File("C:\\local.png"));
OutputStream output =
new FileOutputStream("C:\\local_new.png");
byte[] img = new byte[1024];
while (input.read(img) != -1)
{
output.write(img, 0, img.length);
}
output.flush();
output.close();
input.close();
}
}
楼主的问题所在应该是输入流是FileInputStream,而输出流为OutputStreamWriter吧,不一致,输入流和输出流应该一致,这样建立起来的管道才能匹配!!public class SwitchWriter {
public static void main(String[] args) {
try {
copy("E:\\login_icon.png", "E:\\test.png");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 复制文件,使用字节流
*
* @param sourcePath
* @param targetPath
* @throws IOException
*/
public static void copy(String sourcePath, String targetPath)
throws IOException {
FileInputStream fis = new FileInputStream(sourcePath);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(targetPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] value = new byte[1024];
int len = -1;
while ((len = bis.read(value)) != -1) {
bos.write(value, 0, len);
}
bos.close();
bis.close();
}
}
最后有几点我的体会:
1.操作非文本的文件的时候使用字节流,为了提高效率,建议使用字节包装流,就是BufferInputStream
2.读入和写出用的流尽量保持一致。
祝楼主学习进步,加油!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class SwitchWriter {
public static void main(String[] args) {
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(new File("D:\\杂乱\\头像.jpg"));
output = new FileOutputStream("D:\\杂乱\\头像_new.jpg");
int a;
while ((a = input.read()) != -1) {
output.write(a);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}