Java基础
JavaIO流
什么是流?
内存与存储设备之间传输数据的通道
流的分类
-
按方向分类
- 输入流:将设备中的内容读入到内存中
- 输出流:将内存中的内容写入到设备中
-
按单位分类
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
-
按功能分类
- 节点流:具有实际传输数据的读写功能
- 过滤流:在节点流的基础之上增强功能
字节流的使用
/**
* FileInputStream的使用
* 文件字节输入流
* @author xue
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
//创建FileInputStream对象
FileInputStream file = new FileInputStream("D:\\Learn\\JAVA\\note\\day01.md");
//读取文件
// int data = 0;
// while ((data=file.read())!=-1){
// System.out.println((char)data);
// }
byte[] chars = new byte[1024];
int count = 0;
while ((count=file.read(chars))!=-1){
System.out.println(new String(chars,0,count));
}
//关闭
file.close();
}
}
/**
* FileOutputStream
* 文件字节输入流的使用
* @author xue
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
//创建FileOutputStream对象
FileOutputStream file = new FileOutputStream("D:\\Learn\\JAVA\\note\\day01.txt",true);
//写入数据
// file.write();
// file.write('d');
// file.write('d');
// file.write('d');
// file.write('d');
String str = "helloworld";
file.write(str.getBytes());
//关闭
file.close();
System.out.println("end");
}
}
使用基本字节流实现文件的复制
/**
* 实现文件的复制
* @author xue
*/
public class Demo3 {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("D:\\Learn\\JAVA\\note\\java_day01.md");
FileOutputStream fos = new FileOutputStream("D:\\Learn\\JAVA\\java_day01.md");
byte[] bytes = new byte[2048];
int count = 0;
while ((count=file.read(bytes))!=-1) {
fos.write(bytes,0,count);
}
file.close();
fos.close();
System.out.println("end");
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}
/**
* BufferedInputStream的使用
* 字节缓冲输入流
* @author xue
*/
public class Demo4 {
public static void main(String[] args) throws IOException {
//创建基本文件输入流传入字节缓冲输入流中
FileInputStream fis = new FileInputStream("D:\\Learn\\JAVA\\note\\aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//读取文件
// int data = 0;
// byte[] bytes = new byte[1024];
// while ((data=bis.read(bytes))!= -1){
// System.out.println((new String(bytes,0,data)));
//
// }
int data = 0;
while ((data=bis.read())!=-1){
System.out.println((char)data);
}
bis.close();
}
}
/**
* BufferedOutputStream
* 字节缓冲输出流
* @author xue
*/
public class Demo5 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\Learn\\JAVA\\note\\java.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("和交换到覅和".getBytes());
bos.flush();//此类方法会先将数据写入缓冲区中,刷新才会写入文件中
bos.close();//缓冲流close方法也会调用flush方法
}
}
对象流
- 增强了缓冲区功能、读写基本数据类型的字符串的功能
- 使用流传输对象的过程叫做序列化、反序列化
序列化与反序列化
/**
* ObjectOnputStream
* 实现序列化
* 需要序列化的类需要使用Serializable这一个标记接口
* 序列化版本号 可以确保序列化和反序列化的是同一个类
* 使用transient修饰符可以使某一个属性不能序列化
* 静态属性不能被序列化
*
* @author
*/
public class Demo6 {
public static void main(String[] args) throws Exception {
//创建对象流
FileOutputStream fos = new FileOutputStream("d:\\student.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//序列化
//实现多个对象序列化 1、直接new多个实例对象写入 2、将对个对象存如集合中 将集合序列化
Student stu1 = new Student("zhangsan", 23);
oos.writeObject(stu1);
//关闭
oos.close();
System.out.println("end");
}
}
/**
* ObjectInputStream
* 实现反序列化
* @author xue
*/
public class Demo7 {
public static void main(String[] args) throws Exception{
//创建对象流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\student.bin"));
//读取文件、反序列化
Student stu = (Student) ois.readObject();
//关闭
ois.close();
System.out.println("end");
System.out.println(stu);
}
}
- 需要序列化的类需要使用Serializable这一个标记接口
- 序列化版本号 可以确保序列化和反序列化的是同一个类
- 使用transient修饰符可以使某一个属性不能序列化
- 静态属性不能被序列化
字符流
-
FileReader 读取多个字符,读取内容存入数组
-
FileWriter 一次写入多个字符,将数组中的所有字符写入输出流
/** * FileReader * @author xue */ public class Demo8 { public static void main(String[] args) throws IOException { //创建FileReader对象 文件输入流 FileReader fr = new FileReader("D:\\Learn\\JAVA\\note\\aaa.txt"); //读取内容 int data = 0; while ((data=fr.read())!=-1) { System.out.print((char) data); } //也可以创建一个字符缓冲区读取 char[] chars = new char[1024]; int count = 0; while ((count=fr.read(chars))!=-1) { System.out.println(new String(chars,0,count)); } //关闭 fr.close(); } } /** * FileWriter * @author xue */ public class Demo9 { public static void main(String[] args) throws IOException { //创建FileWriter对象 FileWriter fw = new FileWriter("d:\\aa.txt",true); //写入 for (int i = 0;i<5;i++) { fw.write("Go语言,你也不错\n"); fw.flush(); } //关闭 fw.close(); System.out.println("end"); } }
打印流 PrintWriter
/**
* PrintWriter
* 输入什么文档就显示什么
* 像数组boolean 字符 原装写入
* @author xue
*/
public class Demo13 {
public static void main(String[] args) throws FileNotFoundException {
//创建打印流对象
PrintWriter pw = new PrintWriter("D:\\print.txt");
//打印
pw.print("dsfsdf");
pw.println("dsafasd");//会换行哦 println
pw.println(true);
pw.print(234);
pw.print(97.234);
//关闭
pw.close();
System.out.println("end");
}
}
标签:java,18,System,day17,file,println,new,序列化,public
From: https://www.cnblogs.com/onlyxue/p/16819045.html