IO:
字节流:
InputStream
–FileInputStream
–BufferedInputStream(InputStream)
OutputStream
–FileOutputStream
–BufferedOutputStream(OutputStream)
字符流:
Reader
—FileReader
Writer
—FileWriter
转换流:
InputStreamReader(InputStream, charsetname)
OutputStreamWriter(OutputStream,charsetname)
读取数据步骤:
1.根据操作数据选择对应的输入流对象,并关联要读取的文件
2.定义数组,读取数据
3.关闭流
写入数据步骤:
1.根据操作数据选择对应的输出流对象,并关联要写入数据的文件
2.向文件中写入数据
3.关闭
注意:如果是字符流,要刷新
编码:
将字符数据转成字节数据的过程,即将看的懂的转成看不懂的
解码:
将字节数据转成字符数据的过程,即将看不懂的转成看的懂的
乱码:
编码和解码的时候,使用的编码表不一致,导致再解码的时候出现的错误的数据。这些数据被称为乱码。
BufferedReader:
String readLine()
BufferedWriter:
void newLine()
装饰设计模式:
在原有对象的基础上,增强原有对象的功能。
键盘录入:
System.in == InputStream
java.util.Scanner
功能流:
Properties:
java.util
可以存储key-value,但是key和value必须是字符串形式
可以将Properties中存储的数据保存到文件,也可以将文件中的数据存储到集合中
SequenceInputStream:
合并流,可以将文件按照顺序进行读取
序列化和反序列化:
序列化:写 ObjectOutputStream,按照字节的顺序写数据
反序列化:读 ObjectInputStream,按照字节的顺序读取数据
基本类型,字符串,数组,集合,包装类等对象都可以直接进行序列化
自定义对象序列化,需要实现Serializable接口,实质序列化的是对象的属性值
如果某个属性不想要进行序列化,使用static(不建议)和transient(建议)
与底层代码有关的一些对象不可以进行序列化,比如System,Thread
RandomAccessFile:随机访问流对象,既可以读数据,也可以写数据。
seek(position):可以跳到指定的位置,进行读写操作
getFilePointer():获取当前位置
PrintStream和PrintWrite:打印流
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import org.junit.Test;
/*
* 功能流Properties
*/
public class Demo08 {
@Test
public void test() throws FileNotFoundException, IOException {
// 创建Properties对象
Properties prop = new Properties();
// 给集合中保存数据
prop.put("鲁班", "ADC");
prop.put("诸葛亮", "法师");
// 将集合中数据保存到文件中
prop.store(new FileOutputStream("D:\\prop.properties"), "王者荣耀");
// 加载数据到prop集合中
prop.load(new FileInputStream("D:\\prop.properties"));
// 利用map集合的遍历输出,正常操作!
Set set = prop.keySet();
for (Object key : set) {
Object value = prop.get(key);
System.out.println(key + "...." + value);
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
/*
* 合并流SequenceInpuStream练习
*/
public class Demo09 {
public static void main(String [] args) throws IOException {
// FileInputStream f1 = new FileInputStream("a.txt");
// FileInputStream f2 = new FileInputStream("b.txt");
// SequenceInputStream sis = new SequenceInputStream(f1, f2);
// byte[] by = new byte[100];
// int len = 0;
// while((len = sis.read(by)) != -1){
// System.out.println(new String(by,0,len));
// }
// sis.close();
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(new FileInputStream("a.txt"));
list.add(new FileInputStream("b.txt"));
list.add(new FileInputStream("abc.java"));
Enumeration<FileInputStream> en = Collections.enumeration(list);
SequenceInputStream sis = new SequenceInputStream(en);
byte[] by = new byte[100];
int len = 0;
while((len = sis.read(by)) != -1){
System.out.print(new String(by,0,len));
}
sis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
//自定义的类要实现Serializable接口才能被序列化
class Student implements Serializable{
//在序列化的类添加版本号
private static final long serialVersionUID = 1L;
String name;
//如果对象中的某些成员变量在序列化的时候,不需要被序列化,就可以添加transient修饰成员变量。
transient int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name+"..."+age;
}
}
public class Demo01 {
@Test
public void test3() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
oos.writeObject(new Student("zhangsan2", 20));
// oos.writeObject(new Student("zhangsan3", 20));
// oos.writeObject(new Student("zhangsan4", 20));
oos.close();
}
@Test
public void test4() throws IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
Student s = (Student)ois.readObject();
System.out.println(s);
ois.close();
}
@Test
public void test2() throws ClassNotFoundException, IOException{
//创建反序列化对象,传入字节输出流对象,指定目录。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
//调用不同类型数据的方法读出数据
int num = ois.readInt();
float f = ois.readFloat();
String s = ois.readUTF();
int[] arr = (int[])ois.readObject();
ArrayList<Integer> list = (ArrayList<Integer>)ois.readObject();
System.out.println(num);
System.out.println(f);
System.out.println(s);
System.out.println(Arrays.toString(arr));
System.out.println(list);
ois.close();
}
@Test
public void test1() throws IOException{
//创建多个类型不同的数据
int num = 100;
float f = 12.3f;
String s = "helloworld";
int[] arr = {1,2,3};
ArrayList<Integer> list = new ArrayList<>();
list.add(123);
list.add(345);
//创建序列化流对象,并传入字节输出流对象,指定目录写入。
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeInt(num);
oos.writeFloat(f);
oos.writeUTF(s);
oos.writeObject(arr);
oos.writeObject(list);
oos.close();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
//打印流PrintWriter练习
public class Demo11 {
public static void main(String[] args) throws IOException {
//输出打印
// PrintStream ps = System.out;
// ps.write("abcd".getBytes());
// ps.println("abc");
// ps.print("abc");
PrintStream ps = new PrintStream("print.txt");
PrintStream ps1 = new PrintStream(new File("print1.txt"));
PrintStream ps2= new PrintStream(new FileOutputStream("print2.txt"));
ps.println("haha");
ps.print("hehe");
ps.close();
//创建指定的目录,写入。
PrintWriter pw = new PrintWriter("print3.txt");
pw.print("hhah");
pw.close();
}
}
package day19;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
public class Demo09 {
@Test
public void test1() throws IOException{
//向文件中写入数据
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
raf.seek(5);
raf.write("abcde".getBytes());
raf.close();
}
@Test
public void test2() throws IOException{
//向文件中写入数据
RandomAccessFile raf = new RandomAccessFile("raf.txt", "r");
raf.seek(2); //将指针移到指定的位置上
int ch = raf.read();
System.out.println(raf.getFilePointer()); //获取当前指针的位置
System.out.println((char)ch);
raf.close();
}
}
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
//随机访问流
public class Demo12 {
@Test
public void test1() throws IOException{
//向文件中写入数据
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
raf.seek(5);
raf.write("abcde".getBytes());
raf.close();
}
@Test
public void test2() throws IOException{
//向文件中写入数据
RandomAccessFile raf = new RandomAccessFile("raf.txt", "r");
raf.seek(5); //将指针移到指定的位置上
int ch = raf.read();
System.out.println(raf.getFilePointer()); //获取当前指针的位置
System.out.println((char)ch);
raf.close();
}
}
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
* 图形化界面GUI
*/
class Demo02{
public static void main(String [] args) {
//创建窗口
Frame f = new Frame("我的窗口");
//设置窗口的大小和位置
f.setBounds(300, 150, 400, 200);
//设置背景颜色
f.setBackground(Color.DARK_GRAY);
//给界面添加一个按钮
f.setLayout(new FlowLayout()); //设置布局为流式布局
Button btn = new Button("确定");
Button btn1 = new Button("取消");
//给窗口添加监听机制
f.addWindowListener(new MyWindow());
//创建用于书写用户名和密码文本的标签
Label name = new Label("用户名:");
Label pwd= new Label("密码:");
//创建用于输入用户名和密码的文本框
TextField tf_name = new TextField(12);
TextField tf_pwd = new TextField(12);
f.add(name);
f.add(tf_name);
f.add(pwd);
f.add(tf_pwd);
f.add(btn);
f.add(btn1);
f.setVisible(true);
}
}
//继承了窗口的适配器,重写窗口关闭方法。
class MyWindow extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}