java io流学设置编码_Java学习日志(21-2-IO流-基本数据类型与字节数组对象与、编码解码)...
操作基本數據類型的流對象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);
}
}
總結
以上是生活随笔為你收集整理的java io流学设置编码_Java学习日志(21-2-IO流-基本数据类型与字节数组对象与、编码解码)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北向资金大幅流出是什么意思?
- 下一篇: java接口那一节是哪的知识_Java中