操作基本数据类型的流对象DataStream
/*
可以用于操作基本数据类型数据的流对象
*/
import java.io.*;
class DataStreamDemo{
public static void main(String[] args)throws IOException{
// writeData();
// readData();
// writeUTFDemo();
// OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");
// osw.write("你好");
// osw.close();
readUTFDemo();
}
public static void writeUTFDemo()throws IOException{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("utfdate.txt"));
dos.writeUTF("你好");
dos.close();
}
public static void readUTFDemo()throws IOException{
DataInputStream dis=new DataInputStream(new FileInputStream("utfdate.txt"));
String s=dis.readUTF();
System.out.println(s);
dis.close();
}
public static void readData()throws IOException{
DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
int num=dis.readInt();
boolean b=dis.readBoolean();
double d=dis.readDouble();
System.out.println("num:"+num);
System.out.println("boolean:"+b);
System.out.println("double:"+d);
dis.close();
}
public static void writeData()throws IOException{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(9887.543);
dos.close();
}
}
操作字节数组ByteArrayStream
/*
ByteArrayInputStream:在构造的时候需要接收数据源,而且必须是一个字节数组
ByteArrayOutputStream:在构造的时候不用定义数据目的,内部已经封装了可变长度的字节数组
这两个流对象都操作的数组并没有使用系统资源,不用进行关闭
输出录入设备中的内存属于ArrayStream
*/
import java.io.*;
class ByteArrayStream{
public static void main(String[] args){
// 数据源
ByteArrayInputStream bis=new ByteArrayInputStream("ABCDEFD".getBytes());
// 数据目的
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int by=0;
while((by=bis.read())!=-1){
bos.write(by);
}
System.out.println(bos.size());
System.out.println(bos.toString());
// bos.writeTo(new FileOutputStream("a.txt")
}
}
转换流的字符编码
ASCII:美国标准信息交换码
用一个字节的7位可以表示,识别中文
ISO8859-1:拉丁码表,欧洲码表
用一个字节的8位表示
GB2312:中文编码表
GBK:中文编码表升级
Unicode:国际标准码,融合多种文字
所有文字都用两个字节来表示,Java所用
UTF-8:最多用三个字节来表示一个字符
import java.io.*;
class EncodeStream{
public static void main(String[] args)throws IOException{
// writeText();
readText();
}
public static void readText()throws IOException{
InputStreamReader isr=new InputStreamReader(new FileInputStream("gbk.txt"),"utf-8");
char[]buf=new char[10];
int len=isr.read(buf);
String str=new String(buf,0,len);
System.out.println(str);
isr.close();
}
public static void writeText()throws IOException{
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("gbk.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
}
字符编码/解码
/*
编码:字符串变成字节数组
解码:字节数组变成字符串
String-->byte[];str.getBytes();
byte[]-->String;new String(byte[]);
*/
import java.util.*;
class EncodeDemo{
public static void main(String[] args)throws Exception{
String s="你好";
// 编码
byte[]b1=s.getBytes("gbk");
System.out.println(Arrays.toString(b1));
// 解码
String s1=new String(b1,"ISO8859-1");//输出乱码
System.out.println("s1="+s1);
// 对其进行编码
byte[]b2=s1.getBytes("ISO8859-1");
System.out.println(Arrays.toString(b2));
String s2=new String(b2,"gbk");
System.out.println("s2="+s2);
}
}
字符编码
/*
联通二字GBK码与UTF-8一致
*/
class EncodeDemo2{
public static void main(String[] args)throws Exception{
String s="联通";
byte[] by=s.getBytes("gbk");
for(byte b:by){
// 转码为二进制
System.out.println(Integer.toBinaryString(b&255));
}
}
}
练习
/*
有五个学生,每个学生有3门课的成绩
从键盘输入以上数据(姓名、三门课成句)
输入格式:zhangsan,30,40,60计算出总成绩
并把学生信息和计算出的总分数高低顺序存放在“stud.txt”中
1.描述学生对象
2.定义一个可操作学生对象的工具类
思想
1.通过获取键盘录入一行数据,并将改行中的信息取出封装成学生对象
2.因为学生有很多,那么需要储存,使用到集合
需要排序,采用TreeSet
3.将集合的信息写入文件
*/
import java.util.*;
import java.io.*;
class Student implements Comparable{
private String name;
private int ma,cn,en;
private int sum;
Student(String name,int ma,int cn,int en){
this.name=name;
this.ma=ma;
this.cn=cn;
this.en=en;
sum=en+ma+en;
}
public int compareTo(Student s){
int num=new Integer(this.sum).compareTo(new Integer(s.sum));
if(num==0){
return this.name.compareTo(s.name);
}
return num;
}
public String getName(){
return name;
}
public int getSum(){
return sum;
}
public int hashCode(){
return name.hashCode()+sum*78;
}
public boolean equals(Object obj){
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配!");
}
Student s=(Student)obj;
return this.name.equals(s.name)&&this.sum==s.sum;
}
public String toString(){
return "student["+name+","+ma+","+cn+","+en+"]";
}
}
class StudentInfoTool{
public static Set getStudents()throws IOException{
return getStudents(null);
}
public static Set getStudents(Compatatorcmp)throws IOException{
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
Setstus=null;
if(cmp==null){
stus=new TreeSet();
}
else{
stus=new TreeSet(cmp);
}
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
String[]info=line.split(",");
Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
stus.add(stu);
}
bufr.close();
return stus;
}
public static void write2File(Set stus)throws IOException{
BufferedWriter bufw=new BufferedWriter(new FileWriter("stuinfo.txt"));
for(Student stu:stus){
bufw.write(stu.toString()+"\t");
bufw.write(stu.getSum()+" ");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
class StudentInfoTest{
public static void main(String[] args)throws IOException{
Comparatorcmp=Collections.reverseOrder();
Setstus=StudentInfoTool.getStudents();
StudentInfoTool.write2File(stus);
}
}