------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
import java.io.IOException;
import java.io.RandomAccessFile;
/*
RandomAccessFile(random随机的意思)
此类实例支持对随机访问文件的读取和写入(只能操作文件)
该类不是IO体系中的子类,而是直接继承自Object类,但它是IO包中的对象,因为它具备读和写的功能
内部封装了一个数组,而且通过指针对数组中的元素进行操作,可以通过getFilePointer获取指针位置
可以通过seek改变指针的位置,完成读写操作的原理是内部封装了字节输入流和输出流
通过构造函数可以看出,该类只能操作文件
RandomAccessFile(file r/rw/rws/rwd)
r:只读 rw:读写 rws:读写并把更新的内容或元数据同步写入底层存储设备 rwd:读写并把更新的内容同步写入底层存储设备
readInt(),writeInt()
RandomAccessFile(file r)不会创建file
RandomAccessFile(file rw)没有file就创建,有就在file上续写
*/
public class SuijiCaoZuoLiu
{
public static void main(String[] args) throws IOException
{
RandomAccessFile raf=new RandomAccessFile("C:\\Users\\ming\\Desktop\\新建文件夹\\21\\21\\test.txt","rw");
raf.write("zhangsan".getBytes());
raf.writeInt(258);
raf.write("李四".getBytes());
raf.writeInt(97);
raf.seek(8*3);//从3*8脚标开始写
raf.write("wangwu".getBytes());
raf.writeInt(97);
RandomAccessFile raf1=new RandomAccessFile("C:\\Users\\ming\\Desktop\\新建文件夹\\21\\21\\test.txt","r");
byte[] b=new byte[4];
raf1.read(b);//读4个字节
raf1.readInt();//读4个字节
raf1.seek(8);//从8脚标位开始取
raf1.skipBytes(8);//向后跳8个字节开始(只能往后跳)
raf.close();
raf1.close();
}//多线程下载要用到这个对象
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/*
DateInputStream DateOutputStream
可以用于操作基本数据类型的流对象
ByteArrayInputStream ByteArrayOutputStream
可以用于操作字节数组的流对象,构造函数如下:
ByteArrayInputStream(byte[] b)
ByteArrayInputStream(byte[] b,int offset,int length)
ByteArrayOutputStream()
ByteArrayOutputStream(int size)有writeTo(OutputStream o)方法涉及到异常
该对象没有涉及到底层资源,所以不用关闭流,也不会出现异常,
该对象不用定义数据目的地,因为其内部封装了可变长度的字节数组,可以作为目的地
*/
public class JiBenShuJuLiuDuiXiang
{
public static void main(String[] args) //throws IOException
{
//DataOutputStream dos=new DateOutputStream(new FileOutputStream(""));
ByteArrayInputStream bais=new ByteArrayInputStream("dfgjkhlj".getBytes());
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int num=0;
while((num=bais.read())!=-1)
{
baos.write(num);
}
System.out.println(baos.toString());
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/*
ASCII:美国码表,用一个字节的最低7位表示
ISO8859-1:拉丁码表,欧洲码表,用一个字节8位表示(开头为1以和ASCII区分)
GB2312:中国码表(2个8位表示,两个字节的高位都是1)含8000多常用字
GBK:扩容后的中国码表,含20000多字
Unicode:国际标准码,融合了多种文字(2个字节表示一个)
UTF-8:最多用三个字节来表示一个,
读的时候第一个字节0开头读一个字节;
第一个字节110开头,第二个字节10开头,读两个字节;
第一个字节1110开头,第二个字节10开头,第三个字节10开头,读三个字节
编码:字符串到字节数组 String-->String.getBytes(),String.getBytes(charsetName);
解码:字节数组到字符串 byte[]--> new String(byte[] b),new String(byte[] b,charsetName);
“联通”两个字在文本文档里面无法显示
存的时候默然GBK编码,打开读的时候,因为码符合UTF-8的码,所以读的时候用的是UTF-8,所以会出乱码
字符流是一次读两个字节就查表转换成字符,字节流不同
*/
public class ZiFuBianMa
{
public static void main(String[] args)throws IOException
{
//String s="你好";
//byte[] c=s.getBytes();
//System.out.println(Arrays.toString(c));
StudentTools ss=new StudentTools();
Comparator<Student> com=Collections.reverseOrder();
ss.shuChu(ss.getStudents(com));
}
}
/*
练习:有5个学生,每个学生有三门课程,从键盘输入数据(包括姓名和三门课成绩)
输入格式如:张三,30,40,50计算出总成绩
并把学生的信息和计算出的总分高低顺序存放在硬盘文件中
步骤:
1.描述学生对象
2.建立输入流对象关联键盘
3.建立set集合存储学生对象
4.建立输出流关联硬盘
*/
class Student implements Comparable<Student>
{
String name;
int cn,ma,en,sum;
Student(String name,int cn,int ma,int en)
{
this.name=name;
this.cn=cn;
this.ma=ma;
this.en=en;
sum=cn+ma+en;
}
public int hashCode()
{
return name.hashCode()+sum*15;
}
public boolean equals(Object o)
{
if(!(o instanceof Student))
throw new ClassCastException("类型不符");
Student s=(Student)o;
return this.name.equals(s.name)&&this.sum==s.sum;
}
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 int getSum()
{
return sum;
}
public String toString()
{
return name+":"+cn+","+ma+","+en+","+sum+"";
}
}
class StudentTools
{
public Set<Student> getStudents()throws IOException
{
return getStudents(null);
}
public Set<Student> getStudents(Comparator com)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=null;
Set<Student> set=null;
if(com==null)
set=new TreeSet<Student>();
else
set=new TreeSet<Student>(com);
while((line=br.readLine())!=null)
{
if(line.equals("over"))
break;
String[] str=line.split(",");
Student stu=new Student(str[0],Integer.parseInt(str[1]),
Integer.parseInt(str[2]),
Integer.parseInt(str[3]));
set.add(stu);
}
br.close();
return set;
}
public void shuChu(Set<Student> set)throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("C:\\Users\\ming\\Desktop\\新建文件夹\\21\\21\\test.txt"));
for(Student s:set)
{
bw.write(s.toString());
bw.newLine();
}
bw.close();
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/*
properties是hashtable的子类,也就是说它具备map集合的特点,而且它里面存储的键值对
都是字符串。
properties是集合中和IO技术相结合的集合容器,该对象的特点是可以用于键值对形式的配置文件。
*/
public class PropertiesDuiXiang
{
public static void main(String[] args)throws IOException
{
//setGet();
}
public static void setGet()
{
Properties p=new Properties();
p.setProperty("zhangsan","25");
p.setProperty("lisi","28");
String value=p.getProperty("zhangsan");
Set<String> s=p.stringPropertyNames();
for(String str:s)
{
System.out.println(str+":"+p.getProperty(str));
}
}
public static void demo1()throws IOException
{
BufferedReader br=new BufferedReader(new FileReader(""));
Properties p=new Properties();
String line=null;
while((line=br.readLine())!=null)
{
String[] s=line.split("=");
p.setProperty(s[0], s[1]);
}
br.close();
}
public static void demo2()throws IOException
{
FileInputStream fis=new FileInputStream("");
//BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(""));
FileOutputStream fos=new FileOutputStream("");
Properties p=new Properties();
p.load(fis);//Properties直接接受输入流中的数据
//p.setProperty("","")修改数据
//p.list(System.out);Properties中的数据直接发到输出流
p.store(fos, "明天");//将Properties的数据直接发送到输出流关联的文件(store存储,收藏的意思),"明天"是注释信息
fos.close();
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/*
ObjectInputStream(对象) ObjectOutputStream(对象)
对象必须实现Serializable(标准类)接口,注意:静态成员不能被序列化
如果有非静态成员也不需要序列化则需要在该成员前加transient修饰
*/
public class IOCaoZuoDuiXiang
{
public static void main(String[] args)throws Exception
{
object();
}
public static void object()throws Exception
{
ObjectOutputStream os=
new ObjectOutputStream(new FileOutputStream("C:\\Users\\ming\\Desktop\\新建文件夹\\21\\21\\123.txt"));
ObjectInputStream ois=
new ObjectInputStream(new FileInputStream("C:\\Users\\ming\\Desktop\\新建文件夹\\21\\21\\123.txt"));
os.writeObject(new person("zhangsan",25));
person p=(person)ois.readObject();//读取出来的是对象
System.out.println(p);
os.close();
ois.close();
}
}
class person implements Serializable
{
private String name;
private int age;
person(String name,int age)
{
this.name=name;
this.age=age;
}
public void set(String name)
{
this.name=name;
}
public void set(int age)
{
this.age=age;
}
public String toString()
{
return (name+":"+age);
}
}