IO-demo

作者: Eve0 | 来源:发表于2017-09-04 10:40 被阅读0次
    /**
     * Unit test for simple App.
     */
    public class AppTest {
        /**
         * 从文件中读取内容
         */
        @Test
        public void inputstreamTest() {
            try {
                InputStream inputStream = new FileInputStream(new File("d:/hello.txt"));
    
                byte[] bytearr = new byte[200];
                int length = 0;
                while ((length = inputStream.read(bytearr, 0, 200)) != -1) {
                    String str = new String(bytearr, 0, length,"gbk");
                    System.out.println("str:"+str);
                }
    
                inputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
        /**
         * 向文件中写入数据
         * @throws IOException
         */
        @Test
        public void outpustreamTest() throws IOException {
            String str = "你好,我是信息。";
            byte[] bytearr = str.getBytes("gbk");
            OutputStream outputStream = new FileOutputStream(new File("d:/aa.txt"));
            outputStream.write(bytearr);
            outputStream.close();
            
        }
        /**
         * 使用数组字节输入流方式读取
         * @throws IOException
         */
        @Test
        public void byteArrayInputStreamTest() throws IOException {
            String str = "你好";
            byte[] buff = str.getBytes("gbk");
            System.out.println("#buff:"+buff.length);
            InputStream inputstream = new ByteArrayInputStream(buff);
            int length = 0;
            while(-1!=(length=inputstream.read())) {
                System.out.println("length:"+length);
            }
        } 
        
        /**
         * 使用数组字节输出流向文件中写入
         * @throws IOException
         */
        @Test
        public void byteArrayOutputStreamTest() throws IOException{
            String str = "hello";
            byte[] buff = str.getBytes("gbk");
            
            ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
            outputstream.write(buff);
            
            byte[] bytearr = outputstream.toByteArray();
            for(byte b : bytearr) {
                System.out.println((char)b);
            }
            OutputStream os = new FileOutputStream("d:/aa1.txt");
            outputstream.writeTo(os);
            os.close();
            outputstream.close();
            
            
        }
        
        @Test
        public void fileReaderTest() throws Exception {
            FileReader fReader = new FileReader("d:/aa.txt");
            char[] cbuf = new char[200];
            int length = 200;
            length = fReader.read(cbuf, 0, length);
            for(int i  = 0 ;i < cbuf.length;i++) {
                System.out.println(cbuf[i]);
            }
            fReader.close();
        }
        @Test
        public void fileWriterTest() throws Exception {
            FileWriter fWriter = new FileWriter("d:/bb.txt");
            String hhh = "你好我找二狗子";
            char[] cbuf = hhh.toCharArray();
            for(int i = 0 ;i < cbuf.length;i ++) {
                System.out.println(cbuf[i]);
            }
            fWriter.write(cbuf);
            
            fWriter.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:IO-demo

          本文链接:https://www.haomeiwen.com/subject/innsjxtx.html