首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

【Java中IO流】OutputStreamWriter的用法,该怎么解决

2013-11-30 
【Java中IO流】OutputStreamWriter的用法最近复习了以下java的io流,发现有两个转换流, 分别为: InputStreamR

【Java中IO流】OutputStreamWriter的用法
最近复习了以下java的io流,发现有两个转换流, 分别为: InputStreamReader OutputStreamWriter , OutputStreamWriter是将输入的字符流变为字节流, 我写了一个例子,但怎么行不通? 麻烦大家看一下:


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();
    }
}
OutputStreamWriter
[解决办法]
楼主虽然不不知道你代码错在哪,但是我帮你写了一个

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();
}
}

    }
}

[解决办法]
OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream("D:\\杂乱\\头像_new.jpg"));
 
        String str = buff.toString();
        output.write(str, 0, str.length());
写入的是字符,所以生成图片失败
[解决办法]
看到楼主的分享了,我之前在学习流的时候也遇到过很多困惑,现在有些经验了跟楼主分享一下。
通常操作多媒体文件,流中是以二进制的格式进行的,所以建议使用字节流来操作,楼主的方式忽略了一下节点:
1.读的时候使用字节流,写的时候用的是字节转换流,这样的转换过程会发生问题的。
2.读的时候每一次往img数组中读入,然后添加的StringBuffered中的时候,最后一次有可能没有读满数组,所以会导致多余的字节,同时你是否也发现复制后的文件变大了。

我给楼主分享一版代码,希望对你有帮助:
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.读入和写出用的流尽量保持一致。

祝楼主学习进步,加油!
[解决办法]
引用:
Quote: 引用:

OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream("D:\\杂乱\\头像_new.jpg"));
 
        String str = buff.toString();
        output.write(str, 0, str.length());
写入的是字符,所以生成图片失败


这里不是应经 把字符流转换为字节流了吗, OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream("D:\\杂乱\\头像_new.jpg"));

这个转换可能不是这样理解的吧,你看这里output.write(str, 0, str.length());,写入的明显是字符
[解决办法]
我也帮楼主修改了一下代码:
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吧,不一致,输入流和输出流应该一致,这样建立起来的管道才能匹配!!
[解决办法]
引用:
Quote: 引用:

OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream("D:\\杂乱\\头像_new.jpg"));
 
        String str = buff.toString();
        output.write(str, 0, str.length());
写入的是字符,所以生成图片失败


这里不是应经 把字符流转换为字节流了吗, OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream("D:\\杂乱\\头像_new.jpg"));

不好意思,是我理解错了
[解决办法]
其实楼主的问题就是楼主两个管道不同!一个是字节流,一个是字符流,如果楼主只想单纯的练字节流转字符流,那么不如练习下从文件中读取文字!
[解决办法]
在字节和字符转换的时候可能会有问题
byte[] img = new byte[1024];
        while (input.read(img) != -1)
        {
            buff.append(new String(img));//最后一次读取的时候byte可能没读满,所以比实际长度要大
        }
这样写的话,新生成的文件大小与原文件不一样,
如果把byte数组的长度改为1的话,文件的大小就一样了,但新生成的文件还是打不开
所以可能在字符转字节的时候又出现问题了,这可应该和OutputStreamWriter有关系
[解决办法]
图片文件的原始文件为字节流文件,用字符流去操作不合适,如果你操作的是txt文件,会得到你想要的结果,我也尝试过了,这是我针对你的这些问题的一些总结,可以去看看,http://blog.csdn.net/harderxin/article/details/17019755

热点排行