------- android培训、java培训、期待与您交流! ----------
打印流
字节打印流
PrintStream
构造函数可以接受的参数类型
1,file对象 File
2,字符串路径,String
3,字节输出流 OutputStream
字符打印流
PrintWriter
构造函数可以接受的参数类型
1,file对象 File
2,字符串路径,String
3,字节输出流 OutputStream
4,字符输出流 Write
class PrintWriterDemo{
public static void main(String[] args) throws IOException{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out,true);
String line = null;
while ((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line.toUpperCase());
}
bufr.close();
out.close();
}
}
RandomAccessFile
该类不是IO体系子类,直接继承Object
但是它是IO包中的成员,而且具备读写功能
内部封装了一个数组,而且通过指针对数组的元素进行操作
通过getFilePointer获取指针位置;通过seek改变指针位置,来进行指定的数据读取和写入
可以直接写入基本数据类型
其实完成读写的原理是内部封装了字节输入流和输出流
通过构造函数可以看出,该类只可以操作文件,而且操作文件还有模式:只读r,读写rw等
如果模式为只读r,不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常
如果模式为读写rw,操作地文件不存在会自动创建,如果存在则不会覆盖
public static void readFile()throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
//调整对象中的指针
//raf.seek(8*1);
//跳过指定的字节数,只能往前面跳
//raf.skipBytes(8);
byte[] buf = new byte[4];
raf.read(buf);
String name =new String(buf);
int age = raf.readInt();
sop("name="+name);
sop("age="+age);
raf.close();
}
//指定位置添加
public static void writeFile_2()throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.seek(8*3);
raf.write("周七".getBytes());
raf.writeInt(100);
raf.close();
}
public static void writeFile()throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王五".getBytes());
raf.writeInt(99);
raf.close();
}
DataInputStream和DataOutputStream
用于操作基本数据类型的数据的流对象
public static void readUTFDemo()throws IOException{
DataInputStream dis = new DataInputStream(new FileInputStream("utfdata.txt"));
String s = dis.readUTF();
sop(s);
dis.close();
}
//writeUTF只能用对应的类读出来
public static void writeUTFDemo()throws IOException{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdata.txt"));
dos.writeUTF("你好");
dos.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();
sop("num="+num);
sop("b="+b);
sop("d="+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(1231.11);
dos.close();
}
操作字节数组
ByteArrayInputStream:在构造的时候,需要接受数据源。是一个字节数组
ByteArrayOutputStream:在构造的时候,不用定义数据目的。因为对象中已经内部封装了可变长度的字节数组,该数组就是数据目的地
因为这两个流对象都操作地数组,并没有使用系统资源,所以不用进行close
用流的读写思想来操作数组
public static void main(String[] args) {
//数据源
ByteArrayInputStream bis = new ByteArrayInputStream("ASDVASDD".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("1.txt"));
}
操作字符数组:
CharArrayReader与CharArrayWrite
操作字符串:
StringReader与StringWriter
以上3种流代表内存为源和目的
编码表:
技计算机只能识别二进制,为了方便应用让他可以识别各个国家的文字 。
将各个国家的文字用数字来表示,并一一对应形成一张表,称为编码表
ASCII:美国。一个字节的7位表示
ISO8859-1:欧洲。一个字节的8位表示
GBK:中国。两个字节
Unicode:国际标准码,所有文字用两个字节表示
UTF-8:最多用三个字节表示一个字符
转换流的编码应用:
可以将字符以指定编码格式存储
可以对文本数据指定编码格式来解读
指定编码表的动作由构造函数完成
public static void readText()throws IOException{
//浣犲y
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"GBK");
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("utf.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
}
编码:字符串编程字节数组 str.getBytes(charsetName);
解码:字节数组变成字符串 new String(byte[],charsetName);
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");
//若tuf-8解码并编码,出现错误
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解码。