------ > Java培训、 Android培训、 iOS培训、. Net培训、期待与您交流! -------
1. RandomAccessFile
该类不算是IO体系中的子类,而是直接继承自Object,但它是IO包中成员,因为它具备读和写功能。内部封装了一个数组,通过指针对数组元素进行操作。可以通过getFilePointer获取指针位置,同时可通过seek改变指针位置。
其实完成读写原理是内部封装了字节输入流和输出流,通过构造函数可看出,该类只能操作文件,操作文件还具有模式:只读r,读写rw等。如果模式为只读r,不会创建文件,只读取一个已存在文件,如果该文件不存在,出现异常,如果模式为rw,操作的文件不存在,会自动创建,如果存在不会覆盖。
write()方法:只写数据的低八位, writeInt()方法:写整形数据的32位。
示例代码:
import java.io.*;
class RandomAccessFileDemo
{
public static void main(String[] args) throws IOException
{
// writeFile();
// readFile();
writeFile_2();
}
public static void writeFile_2()throws IOException //在指定位置写入
{
RandomAccessFile raf=new RandomAccessFile("ran.txt","rw"); //该文件具有读写功能
raf.seek(8*4); //在指定位置写入
raf.write("赵六".getBytes());
raf.writeInt(37); //writeInt方法写四个字节
raf.close();
}
public static void readFile()throws IOException
{
RandomAccessFile raf=new RandomAccessFile("ran.txt","r");//只读
//调整对象中指针,可以前后调
raf.seek(8*1); //指针放到第8个字节
//跳过指定的字节数,只能往后跳
// raf.skipBytes(8);
byte[] buf =new byte[4]; //读4个字节,两个汉字
raf.read(buf); //读4个字节到buf中
String name = new String (buf); //字节数组转字符串
int age = raf.readInt();//读年龄
System.out.println("name="+name);
System.out.println("age="+age);
raf.close();
}
public static void writeFile()throws IOException
{
RandomAccessFile raf=new RandomAccessFile("ran.txt","rw"); //该文件具有读写功能
raf.write("李四".getBytes());
raf.writeInt(97); //writeInt方法写四个字节
raf.write("王五".getBytes());
raf.writeInt(25);
raf.close();
}
}
2. DateInputSream 与DateOutputStream
可以用于操作基本数据类型的数据的流对象,输入输出更简洁.示例代码:
import java.io.*;
class DataStreamDemo
{
public static void main(String[] args) throws IOException
{
//writeDate();
// readData();
// writeUTFDemo();
readUTFDemo();
}
public static void readUTFDemo() throws IOException //用UTF编码格式读
{
DataInputStream dis =new DataInputStream (new FileInputStream("dateUTF.txt"));
String s =dis.readUTF();
System.out.println(s);
}
public static void writeUTFDemo() throws IOException //用UTF编码格式写
{
DataOutputStream dos =new DataOutputStream(new FileOutputStream("dateUTF.txt"));
dos.writeUTF("你好");
dos.close();
}
public static void readData() throws IOException
{
DataInputStream dis =new DataInputStream (new FileInputStream("date.txt"));
//以前的读取需要装到数组里,把数组变成字符串再强转成整数
int num=dis.readInt(); //因为读取时按照字节大小读取,一定要按顺序读,
boolean b=dis.readBoolean();
double d=dis.readDouble();
System.out.println("int num="+num);
System.out.println("boolean b="+b);
System.out.println("double d="+d);
dis.close();
}
public static void writeDate() throws IOException
{
DataOutputStream dos =new DataOutputStream(new FileOutputStream("date.txt"));
dos.writeInt(234); //写整形数据32位,8个字节
dos.writeBoolean(true); //1个字节
dos.writeDouble(5368.567); //4个字节
dos.close();
}
}
3.用于操作字节数组的流对象
ByteArrayInputStream: 构造的时候,需要接收数据源,而且数据源是一个字节数组。
ByteArrayOutputStream: 构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组,这就是数据目的地。
因为这两个流对象都操作的数组,并没有使用系统资源所以不用关闭。
在流操作规律讲解时:
源设备: 键盘 System.in 硬盘 FileStream 内存:ArrayStream
目的设备: 控制台:System.out 硬盘 FileStream 内存 ArrayStream
用流的读写思想来操作数组。
import java.io.*;
class ByteArrayStream
{
public static void main(String[] args)
{
//数据源
ByteArrayInputStream bis=new ByteArrayInputStream("ABCDE".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());
}
}
4.编码和解码
编码:字符串变成字节数组。
String-->byte[] 用 str.getBytes(charsetName);
解码:字节数组变成字符串
byte[]--> String new String(byte[],charsetName);
常用的编码表有: ASCII 美国标准信息交换码,用一个字节的7位可以表示;
ISO-8859:拉丁码表,欧洲码表,用一个字节的8位表示;
GB2312:中国的中文编码表,一个汉字用两个字节表示,每个字节的最高位都是1;
GBK码表:中文编码表的升级,融合更多中文文字符号;
Unicode:国际标准码,融合多种文字,所有文字都用两个字节表示,Java使用Unicode表;
UTF-8:最多用三个字节表示一个字符,也能识别中文。
下面的方法是当解码和编码不使用同一个码表出现乱码时怎么把信息还原的方法:
import java.io.*;
import java.util.*;
class EncodeDemo
{
public static void main(String[] args) throws IOException
{
String s="你好";
byte[] b1=s.getBytes("GBK");//默认是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");//对s进行ISO8859-1编码
System.out.println(Arrays.toString(b2));
String s2=new String(b2,"GBK");
System.out.println("s2="+s2);
}
}