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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  1. <P>import java.io.*;
  2. class  FileStream
  3. {
  4. public static void main(String[] args)
  5. {
  6.   read_2();
  7.   //read_1();
  8.   //write();
  9. }
  10. public static void read_2()
  11. {
  12.   FileInputStream fis = null;
  13.   try
  14.   {
  15.    fis = new FileInputStream("FileStreamDemo.txt");
  16.    int ch =0;
  17.    while((ch = fis.read())!=-1)
  18.    {
  19.     System.out.println(ch);\<FONT color=red>\打印出一串数字,如果加char 强转是一串问号
  20. </FONT>   }
  21.   }
  22.   catch (IOException e)
  23.   {
  24.    System.out.println(e.toString());
  25.   }
  26.   finally
  27.   {
  28.    if (fis!=null)
  29.    {
  30.     try
  31.     {
  32.      fis.close();
  33.     }
  34.     catch (IOException e)
  35.     {
  36.      System.out.println("close failed");
  37.     }
  38.    }
  39.   }
  40. }
  41. public static void read_1()
  42. {
  43.   FileInputStream fis = null;
  44.   try
  45.   {
  46.    fis = new FileInputStream("FileStreamDemo.txt");//FileStreamDemo.txt 这个文件里边写的是中文
  47.    int len =0;
  48.    byte[] b = new byte[1024];
  49.    while((len=fis.read(b))!=-1)
  50.    {
  51.     System.out.println(new String(b,0,len));\<FONT color=red>\可以打印出中文,这个为什么可以打印出中文??b里边装的不也是单个的字节吗? 既然是字节打印结果应该和上面那个方法的打印结果一样呀??为什么这个能打出中文,上面的却是数字??
  52. </FONT>   }
  53.   }
  54.   catch (IOException e)
  55.   {
  56.    System.out.println(e.toString());
  57.   }
  58.   finally
  59.   {
  60.    if (fis!=null)
  61.    {
  62.     try
  63.     {
  64.      fis.close();
  65.     }
  66.     catch (IOException e)
  67.     {
  68.      System.out.println(e.toString());
  69.     }
  70.    }
  71.   }
  72. }
  73. public static void write()
  74. {
  75.   FileOutputStream fos = null;
  76.   try
  77.   {
  78.    fos = new FileOutputStream("FileStreamDemo.txt");
  79.    fos.write("谁在天堂".getBytes());
  80.   }
  81.   catch (IOException e)
  82.   {
  83.    System.out.println("catch"+"写入失败 ");
  84.   }
  85.   finally
  86.   {
  87.    if(fos!=null)
  88.     try
  89.     {
  90.      fos.close();
  91.     }
  92.     catch (Exception e)
  93.     {
  94.      System.out.println(e.toString());
  95.     }
  96.   }
  97. }
  98. }</P>
复制代码

3 个回复

倒序浏览
new String(b,0,len)),自动将字节数组用系统默认的编码表进行编码了,所以会看到中文。
问号的出现是因为系统不能识别字节码,无法对字节码进行编码。
回复 使用道具 举报
这应该涉及到字符编码的问题了  inputSream的read方法读取到的是一个字节,你一个个字节的读,然后再往出打,肯定不行的!因为,一个中文是两个字节!就相当于你把中文字拆开了,然后往出打的时候,按照默认的jbk编码表,肯定编译不了!至于你第下面的read(数组)方法,就对了 因为你的字节数组足够的大  可以装下你所读到的数据!编译的时候就没问题!第一个问题的解决方案就是:你可以定义一个Stringbiulder,将每一次读到的数据添加其中,然后再一次性的转换成字符串,在进行打印!
回复 使用道具 举报
  1. FileReader fr=new FileReader("FileStreamDemo.txt");
  2.    int ch =0;
  3.    char c;
  4.    StringBuilder sb=new StringBuilder();
  5.    
  6.   // fr.read()
  7.    while((ch = fr.read())!=-1)
  8.    {
  9.           c=(char) ch;
  10.           sb.append(c);
  11.           
  12.    // System.out.println(c);//\<FONT color=red>\打印出一串数字,如果加char 强转是一串问号
  13.    }
  14.    String s=new String(sb);
  15.    System.out.println(s);
复制代码
用FileReader类字符流来读取就可以了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马