流总结

这里写图片描述

FileOutputStream(File file,boolean append)可以追加文件
filereader、filewriter无法更改字符集
package com.it18zhang.java.test;

import com.it18zhang.java.util.DataUtil;
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by Administrator on 2017/7/28.
 */
public class TestStream {

    /**
     * 以缓冲区方式读取
     */
    @Test
    public void test1() throws Exception {
        FileInputStream fis = new FileInputStream("d:/1.txt") ;
        byte[] buf = new byte[1024] ;
        int len = -1 ;
        while((len = fis.read(buf)) != -1){
            System.out.println(new String(buf,0,len));
        }
        fis.close();
    }

    /**
     * 以缓冲区方式读取
     * byte : -128 ~ 127
     *
     */
    @Test
    public void test2() throws Exception {
        FileInputStream fis = new FileInputStream("d:/1.txt") ;
        int b = -1 ;
        while((b = fis.read()) != -1){
            //
            char c = (char)b ;
            System.out.print(c);
        }
        fis.close();
    }

    @Test
    public void test3() throws Exception {
        FileInputStream fis = new FileInputStream("d:/Koala.gif") ;
        int b = -1 ;
        while((b = fis.read()) != -1){
            //
            System.out.print(b + ",");
        }
        fis.close();
    }

    /**
     *测试字符集
     */
    @Test
    public void testCharset() throws Exception {
        String str = "a中bc" ;
        System.out.println((char)63);
        byte[] bytes = str.getBytes("utf-8");
        System.out.println(new String(bytes,"unicode"));
        System.out.println(Charset.defaultCharset());
        System.out.println("dd");
    }

    /**
     * 读取文件,使用平台默认的字符集编码.
     * new String(buf,0,len,"gbk")
     */

    @Test
    public void test4() throws Exception {
        FileInputStream fis = new FileInputStream("d:/2.txt");
        byte[] buf = new byte[6] ;
        int len = -1;
        while ((len = fis.read(buf)) != -1) {
            //
            System.out.print(new String(buf,0,len,"gbk"));
        }
        fis.close();
    }

    /**
     * 写入文件,使用gbk编码。
     */
    @Test
    public void test5() throws Exception {
        FileOutputStream fos = new FileOutputStream("d:/2.txt");
        fos.write("a中bc".getBytes("gbk"));
        fos.close();
    }


    /**
     * 写入文件
     */
    @Test
    public void test6() throws Exception {
        String str = "a中bc" ;
        byte[] bytes = str.getBytes("iso8859-1");
        System.out.println(new String(bytes,"iso8859-1"));
    }

    /**
     *unicode表示法
     */
    @Test
    public void test7() throws Exception {
        char x = 'a' ;
        x = 97;
        //unicode表示法
        x = '\u0061' ;
        System.out.println(x);
        int i = '徐' ;
        char xx = '\u5F90' ;
        System.out.println(xx);
//
//      char a = 'u+1f4a1' ;                //' ' '
//      char b = '\u005c' ;             //' \ '
//      //char c = '\u005c\u0027' ;     //单引号的unicode表示法,idea ????
//
//      System.out.println(a);
//      System.out.println(b);
    }

    @Test
    public void test8(){
        int cols = 0 ;
        for(int i = 0 ; i <= 0xffff ; i ++){
            cols ++ ;
            System.out.print(i + ":" + (char)i + ",");
            if(cols == 30){
                cols = 0 ;
                System.out.println();
            }
        }
    }

    /**
     * 使用平台默认的utf-8字符集读取文件.
     */
    @Test
    public void test9() throws Exception {
        FileReader fr = new FileReader("d:/2.txt");
        int ch = -1 ;
        while((ch = fr.read()) != -1) {
            System.out.print((char)ch);
        }
        fr.close();
    }

    /**
     * 使用平台默认的utf-8字符集读取文件.
     */
    @Test
    public void test10() throws Exception {
        FileInputStream fis = new FileInputStream("d:/3.txt");
        InputStreamReader isr = new InputStreamReader(fis , "gbk");
        int ch = 0 ;
        while((ch = isr.read()) != -1){
            System.out.print((char)ch);
        }
        isr.close();
        fis.close();
    }

    /**
     */
    @Test
    public void test11() throws Exception {
        int i = 255 ;
        byte[] bytes = DataUtil.int2ByteArr(i);
        System.out.println(DataUtil.byteArr2Int(bytes));
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值