IO思考与实践(IO课后练习)

    技术2024-10-07  61

     

     1)将一个目录下的所有txt文件合并到1m大小的txt文件中?

     

    2

    什么是流?流是一串连续传输的数据的集合,就像水管里德水一样

    什么是节点流?用于直接操作目标设备所对应的类叫做节点流类

    什么是包装类?把流包装上,使流具有一定特性的类。

    3)编写一个函数,并写入字符串

    package IO;

    import java.io.*;

    public class StringTest {

       

           public static void main(String[] args) {

               String tmp="china";//定义输入的字符串

               StringReader sr=new StringReader(tmp);

               StringWriter sw=new StringWriter();

               transfrom(sr, sw);//调用转换流的函数

               System.out.println(sw);//把字节数组转化成字符串输出

              

           }

           public static void transfrom(StringReader in,StringWriter out)

           {

                  int ch=0;//定义一个整形变量来存放流的字节

                  try {

                      while ((ch=in.read())!=-1) { //如果读取的数不为-1,则循环继续

                         int upperCh=Character.toUpperCase((char)ch);//把小写的字符转换成大写

                         out.write(upperCh);//把转换后的数据加入到输出流中

                        

                      }

                  } catch (IOException e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                  }

              

              

              

           }

     

    }

     

    4)中国的各种编码形式

    Gb2312(中国)D6 D0(中)B9 FA(国)

    UTF-8(中国)EF BB BF(格式标识)E4 B8 AD()   E5 9B BD()

    Unicode(中国big endianFE FF(格式标识符)  4E 2D   56 FD

    Unicode(中国):FF FE(格式标识)  2D 4E    FD 56

    Big endian和普通的Unicode 编写字符时高位和低位时是颠倒的

    5)答案为注释掉的部分

    package IO;

    import java.io.*;

    public class InputReader {

     

        /**

         * @param args

         */

        public static void main(String[] args) throws Exception {

           InputStreamReader isr=new InputStreamReader(System.in,"iso8859-1");

           //InputStreamReader isr=new InputStreamReader(System.in,"gb2312");

           BufferedReader br=new BufferedReader(isr);

           System.out.println("请输入:");

           String strLine=br.readLine();

           for (int i = 0; i < strLine.length(); i++) {

               System.out.println(Integer.toHexString((int)strLine.charAt(i)));

           }

           isr.close();

           System.out.println(strLine);

           //System.out.println(new String(strLine.getBytes("iso8859-1"),"gb2312"));

     

        }

     

    }

     

    最新回复(0)