Javaday22 杂七杂八流
一 数据输入输出流的概述和使用
数据输入流 :DateInputStream
数据输出流:DateOutputStream
特点: 可以写基本数据类型,可以读取基本数据类型
public static void main(String[] args) throws IOException {
//数据输入输出流的特点,就是能够读写基本数据类型
// DataInputStream
// DataOutputStream
//writeData();
//怎么写的就怎么读,顺序不要乱
DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
boolean b = in.readBoolean();
double v = in.readDouble();
int i = in.readInt();
String s = in.readUTF();
System.out.println(b);
System.out.println(v);
System.out.println(i);
System.out.println(s);
in.close();
}
private static void writeData() throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt"));
out.writeBoolean(true);
out.writeDouble(3.14);
out.writeInt(100);
out.writeUTF("你好世界");
out.close();
}
}
二 内存操作流的概述和使用
1.内存操作流的概述
a 操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭
b 操作字符数组
CharArrayWrite
CharArrayReader
c 操作字符串
StringWriter
StringReader
public class 内存操作流 {
public static void main(String[] args) throws IOException {
byte[] allBytes = out.toByteArray();
String s = new String(allBytes);
System.out.println(s);
ByteArrayInputStream in= new ByteArrayInputStream(allBytes);
byte[] bytes=new byte[1024*8];
int len = in.read(bytes);
String s1 = new String(bytes, 0, len);
System.out.println(s1);
}
}
public static void main(String[] args) throws IOException {
// CharArrayWrite
//CharArrayReader
CharArrayWriter writer = new CharArrayWriter();
writer.write("abc");
writer.write("abc");
writer.write("abc");
writer.write("abc");
writer.write("abc");
//取出缓冲区中的数据
char[] chars = writer.toCharArray();
System.out.println(String.valueOf(chars));
System.out.println(new String(chars));
System.out.println(writer.toString());
三 打印流的概述和特点以及作为Weiter的子类使用
1.打印流的特点
打印流只能操作目的地,不能操作数据源(不能进行读取数据)
可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
通过以下构造创建对象 能够启动自动刷新 然后调用println、printf 或 format 方法中的一个方法的时候,会完 成自动刷新
public PrintWriter(OutputStream out, boolean autoFlush) 启动 自动刷新
public PrintWriter(Writer out, boolean autoFlush) 启动自动刷新
这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径)
public class 字节打印流 {
public static void main(String[] args) throws IOException {
//打印流:只操作目的地,不关联源文件
// PrintStream 字节打印流
PrintStream printStream = new PrintStream(new FileOutputStream("b.txt"));
printStream.write("字节打印流".getBytes());
printStream.print(true);
printStream.println(100);
printStream.close();
PrintStream out = System.out;
out.write("abc".getBytes());
out.println(3.14);
System.out.println("abc");
}
}
public class 字符打印流 {
public static void main(String[] args) throws IOException {
//参数2:true 自动刷新
//如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作
PrintWriter writer = new PrintWriter(new FileOutputStream("c.txt"),true);
//writer.write("字符打印流");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.flush();
writer.close();
}
}
public class 打印流复制文本文件 {
public static void main(String[] args) throws IOException {
//A:
//案例演示:
//打印流复制文本文件
BufferedReader reader = new BufferedReader(new FileReader("内存操作流.java"));
PrintWriter printWriter = new PrintWriter(new FileOutputStream("aa.java"),true);
String line=null;
while ((line=reader.readLine())!=null){
printWriter.println(line);
}
reader.close();
printWriter.close();
System.out.println("----------------------------------");
Scanner scanner = new Scanner(new File("内存操作流.java"));
PrintWriter printWriter2 = new PrintWriter(new FileOutputStream("bb.java"), true);
while (scanner.hasNextLine()){
String s = scanner.nextLine();
printWriter2.println(s);
}
scanner.close();
printWriter2.close();
}
}
四 两种方式实现键盘录入
public class 键盘录入的第二种方式 {
public static void main(String[] args) throws IOException {
//Scanner scanner = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("请输入数据");
String s = reader.readLine();
//自定义一个结束标记
if("886".equals(s)){
break;
}
System.out.println(s);
}
}
}
五 随机访问流概述和写出数据
一 随机访问流概述
- RandomAccessFile概述 最大特点 能读能写
- RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功 能。
- 支持对随机访问文件的读取和写入。
- 我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针
public class MyTest {
public static void main(String[] args) throws IOException {
// RandomAccessFile 随机访问流,此流的特点,能读能写,有一个文件指针,能够记录文件读写的位置
// 此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;
//rw 模式,可读可以写
// writeData();
//你怎么写的,就怎么读取 顺序不要乱
RandomAccessFile ra = new RandomAccessFile(new File("e.txt"), "rw");
boolean b = ra.readBoolean();
//获取文件指针的位置
long filePointer = ra.getFilePointer();
System.out.println(filePointer);//1
double v = ra.readDouble();
filePointer = ra.getFilePointer();
System.out.println(filePointer);//9
int i = ra.readInt();
filePointer = ra.getFilePointer();
System.out.println(filePointer);//13
char c = ra.readChar();
filePointer = ra.getFilePointer();
System.out.println(filePointer);//15
String s = ra.readUTF();
filePointer = ra.getFilePointer();
System.out.println(filePointer);//
System.out.println(b);
System.out.println(v);
System.out.println(i);
System.out.println(s);
//设置指针的位置
ra.seek(15);
String s1 = ra.readUTF();
System.out.println(s1);
}
private static void writeData() throws IOException {
RandomAccessFile ra = new RandomAccessFile(new File("e.txt"), "rw");
ra.writeBoolean(true);
ra.writeDouble(3.14);
ra.writeInt(100);
ra.writeChar('a');
ra.writeUTF("字符串");
ra.close();
}
}
public class MyTest2 {
public static void main(String[] args) throws IOException {
RandomAccessFile ra = new RandomAccessFile("许巍 - 曾经的你.mp3", "rw");
File file = new File("曾经的你.mp3");
RandomAccessFile ra2 = new RandomAccessFile(file, "rw");
if(file.exists()){
String s = new BufferedReader(new FileReader("临时.txt")).readLine();
int i = Integer.parseInt(s);
ra.seek(i);
ra2.seek(i);
}else{
ra.seek(0);
ra2.seek(0);
}
try {
int len = 0;
byte[] bytes = new byte[1];
int index=1;
while ((len = ra.read(bytes)) != -1) {
ra2.write(bytes,0,len);
}
}catch (Exception e){
//一旦遇到异常,我得记录当前指针的位置
long filePointer = ra.getFilePointer();
System.out.println(filePointer);
PrintWriter printWriter = new PrintWriter("临时.txt");
printWriter.println(filePointer);
printWriter.flush();
}
}
}
六 序列化流和反序列化流的概述和使用
序列化流的概述
所谓序列化就是把对象通过流的方式储存到文件中(此对象要重写serializable接口才能被序列化)
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流:objectoutputStream
反序列化 :objectinputsSream
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口
一个对象可以被序列化的前提是这个对象对应的类必须实现serialiable接口
public static void main(String[] args) throws IOException, CloneNotSupportedException, ClassNotFoundException {
readObj();
}
private static void readObj() throws IOException, ClassNotFoundException {
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("student.txt"));
Object obj = objin.readObject();
Student student= (Student) obj;
System.out.println(student.getName());
System.out.println(student.getAge());
}
private static void writeObj() throws IOException {
Student student = new Student("zhangsan",23);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));
out.writeObject(student);
out.close();
}
}
public class Student implements Serializable,Cloneable {
private static final long serialVersionUID = -7602640005373026150L;
//public static final long serialVersionUID = 42L;
private String name;
//transient 某个成员变量不想序列化可以使用transient来修饰一下
public transient int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
如何解决序列化的时候黄色警告线的问题
- 我一个类可以被序列化的前提是需要这个类实现Serializable接口,就需要给这个类添加一个标记.
- 在完成序列化以后,序列化文件中还存在一个标记,然后在进行反序列化的时候,
会验证这个标记和序列化前的标记是否一致,如果一致就正常进行反序列化 - 不一致就报错了. 而现在这个类做了修改,将相当于更改了标记,而导致这两个标记不一致,就报错了.
- 解决问题: 只要让这个两个标记一致,就不会报错了
- alt+enter, 生成出来
private static final long serialVersionUID = -7602640005373026150L;
七 properties的概述和作为map集合的使用
properties类表示一个持久的属性集,可以保存在流中或从流中加载
属性列表中每一个键及其对应值都是一个字符串
properties的父类是Hashtable,属于双列集合,这个集合的键和值都是字符串proper不能指定泛型
public static void main(String[] args) {
// Properties 属性集合,经常用它来读写配置文件 属于双列集合
Properties properties = new Properties();//他规定了键值 是String类型
//properties.put("aaa","bbb");
//Object aaa = properties.get("aaa");
//System.out.println(aaa);
//用它特有的方法,来存储键值
properties.setProperty("陈羽凡","白百合");
String value = properties.getProperty("陈羽凡");
System.out.println(value);
//参数2,默认值,如果键没有找到对应的值,就返回默认值
String property = properties.getProperty("陈羽凡", "李小璐");
System.out.println(property);