![这里写图片描述](https://img-blog.csdn.net/20170729162915404?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM4NzM3Nzg5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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;
x = '\u0061' ;
System.out.println(x);
int i = '徐' ;
char xx = '\u5F90' ;
System.out.println(xx);
}
@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));
}
}