A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ehuashao 中级黑马   /  2014-4-1 10:40  /  1519 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ehuashao 于 2014-4-1 14:14 编辑
  1. public class FileTest {

  2.         public static void main(String[] args) throws Exception {
  3.                 FileInputStream fin = new FileInputStream(new File("f://Demo.txt"));
  4.                 FileOutputStream fout =new FileOutputStream("f://11.txt");               
  5.                 InputStreamReader in = new InputStreamReader(fin);               
  6.                 OutputStreamWriter out = new OutputStreamWriter(fout,"gb2312");
  7.                 int  b;
  8.                 while((b=in.read()) != -1){
  9.                         fout.write(b);
  10.                 }               
  11.                
  12.         }
  13. }
复制代码
对java io流,完全不会啊,自己摸索着写,字节流读入,然后转字符流,输出的中文全是乱码,设置了输出编码为  utf-8依然乱码求解啊。

4 个回复

倒序浏览
你先照老师的来
read到一个byte[] 数组里
再输出这个数组

慢慢的就会明白了那个b 是不能直接write的
回复 使用道具 举报
本帖最后由 一诺千金 于 2014-4-1 11:03 编辑

package com.itheima;

import java.io.*;

public class Tost {
        

        public static void main(String[] args) throws Exception {
        FileInputStream fin = new FileInputStream(new File("G://demo.txt"));
        FileOutputStream fout =new FileOutputStream("G://11.txt");               
        //InputStreamReader in = new InputStreamReader(fin);     ----------------------------------这里注释掉           
        OutputStreamWriter out = new OutputStreamWriter(fout,"gb2312");
        int  b;
        while((b=fin.read()) != -1){
                fout.write(b);
        }               
               
        }
}你转换了两次,读取流用的默认的和输出流指定的冲突了,默认为当前系统自己的,解析的时候你有用的别的。所以乱了套了
回复 使用道具 举报
本帖最后由 黄晓鑫 于 2014-4-1 11:09 编辑

:lol:lol:lol
回复 使用道具 举报
一诺千金 发表于 2014-4-1 10:59
package com.itheima;

import java.io.*;

已解决,谢了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马