java_IO流
English
文件
一、创建文件的三种方式
package file_01;
import org.junit.jupiter.api.Test;
import javax.xml.soap.Text;
import java.io.File;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
* 一、 文件流 java程序 <-----输入流-------文件(磁盘)
* java程序 ------输出流------>文件(磁盘)
*
* 二、 创建文件的三种操作:
*
*/
public class file_create_demo_01 {
public static void main(String[] args) {
}
@Test
// 第一种创建方式
public void FileCreate01(){
File file = new File("Z:\\zh.txt");
try {
file.createNewFile();
System.out.println("创建成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
// 第二种创建方式
@Test
public void FileCreate02(){
File file=new File("Z:\\");
File file1=new File(file,"zh01.txt");
try {
file1.createNewFile();
System.out.println("创建成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
// 第三种创建方式
@Test
public void FileCreate03(){
String name="Z:\\";
String name1="zh02.text";
File file=new File(name,name1);
try {
file.createNewFile();
System.out.println("创建成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、获取文件的信息
package file_01;
import org.junit.jupiter.api.Test;
import java.io.File;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 获取文件的信息
*/
public class file_getInfo {
public static void main(String[] args) {
}
@Test
public void getInfo(){
// 创建文件对象
File file =new File("z:\\zh.txt");
// 一、获取文件的名字
System.out.println(file.getName());
// 二、获取文件的路径
System.out.println(file.getAbsolutePath());
// 三、获取文件的父目录
System.out.println(file.getParent());
// 四、获取文件的大小
System.out.println(file.length());
// 五、判断文件是否存在
System.out.println(file.exists());
// 六、判断是不是一个文件
System.out.println(file.isFile());
// 七、判断是不是一个目录
System.out.println(file.isDirectory());
}
}
三、创建文件目录
package file_01;
import org.junit.jupiter.api.Test;
import java.io.File;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class 创建目录 {
public static void main(String[] args) {
}
// 一、判断 "z:\\zh.txt" 是否存在 存在就删除
@Test
public void m(){
String filePath="z:\\zh.txt";
File file = new File(filePath);
if (file.exists()){
System.out.println(file.delete());
}else{
System.out.println("文件不存在");
}
}
// 二、判断 "z:\\zh\zz\hh" 目录是否存在 如果存在就提示存在 否则就删除
@Test
public void m1() {
File file = new File("z:\\zh\\zz\\hh");
if (file.exists()){
System.out.println("存在....");
}else{
// 注意 : 创建一级目录的话使用 file.mkdir z:\zh
// 创建多级目录的话使用 file.mkdirs z:\zh\zz\hh
if (file.mkdirs()){
System.out.println("创建目录成功...");
}else{
System.out.println("创建失败...");
}
}
}
}
IO流原理及流的分类
FileInputStream(文件字节输入流)
package IO流;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* 成为想成为的人
*/
public class FileInputStream_demo {
public static void main(String[] args) throws IOException {
String filepath="z:\\hello.txt";
byte[] buf=new byte[8];
int count;
FileInputStream fis = new FileInputStream(filepath);
while ((count=fis.read(buf))!=-1){
System.out.print(new String(buf,0,count));
}
// 关闭文件
fis.close();
}
}
FileOutputStrean (文件字节输出流)
package IO流;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* 成为想成为的人
*/
public class FileOutputStream_demo {
public static void main(String[] args) {
String filepath="z:\\hello.txt";
FileOutputStream fos=null;
try {
// 如果在后面加了 true 表示在文件后面追加 如果没加则会删除原来的文件
fos = new FileOutputStream(filepath,true);
fos.write("ZhouHao".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileReader(字符文件输入流)
package IO流;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* 成为想成为的人
*/
public class FileReader_demo {
public static void main(String[] args) throws IOException {
String filepath="z:\\hello.txt";
int count;
char[] ch=new char[8];
FileReader fr = new FileReader(filepath);
while ((count=fr.read(ch))!=-1){
System.out.print(new String(ch,0,count));
}
fr.close();
}
}
FileWriter(字符文件输出流)
package File_Reader;
import org.junit.jupiter.api.Test;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class FileWriter_demo {
public static void main(String[] args) {
}
@Test
public void FileWriter(){
String writePath ="z:\\hello.txt";
FileWriter fileWriter=null;
try {
// 不加true 是一个覆盖的操作
// 加了 true 就是在后面追加
fileWriter =new FileWriter(writePath,true);
fileWriter.write("周浩");
fileWriter.write("周浩",0,2); //指定字符串的长度 注意数组不要越界
fileWriter.write(65); //写入一个字符
fileWriter.write(new char[]{65,66});
fileWriter.write(new char[]{65,66},0,2); //指定char[] 数组的长度
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 对于 FileWriter 必须要关闭 close 这个文件 或者是 flush 刷新这个文件 否则不会写入文件中
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
节点流和处理流
一、概况
1、节点流:节点流可以从一个特定的数据源读写数据 eg FileReader 、FileWriter
2、处理流: 也叫(包装流)是“连接” 在已存在的流(节点流 或 处理流 ) 之上,为程序提供更为强大的读写功能 eg BufferedReader 、BufferedWriter
BufferedReader
package 包装流Reaner_Writer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 包装流的使用 包装输入流
*/
public class BufferedReader_01 {
public static void main(String[] args) throws IOException {
String FilePath="z:\\hello.txt";
BufferedReader bufferedReader=new BufferedReader(new FileReader(FilePath));
String temp;
// readLine 按行读取 当为null 读取完毕
while ((temp=bufferedReader.readLine())!=null){
System.out.println(temp);
}
// 记得关闭文件
bufferedReader.close();
}
}
BufferedWriter
{
public static void main(String[] args) throws IOException {
String FilePath="z:\\hello.txt";
// 不加 就是覆盖 添加true就是追加
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(FilePath,true));
bufferedWriter.write("hello,word..");
// 添加一个表示跟系统相关的换行符
bufferedWriter.newLine();
bufferedWriter.write("hello,word..");
// 关闭
bufferedWriter.close();
}
}
BufferedInputStream
package 包装流InputStream_OutputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 演示输入流
*/
public class BufferedInputStream_01 {
public static void main(String[] args) throws IOException {
String path="z:\\hello.txt";
// 按照byte 字节来读
byte[] by=new byte[1024];
int count;
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(path));
while ((count=bufferedInputStream.read(by))!=-1){
System.out.println(new String(by,0, count));
}
bufferedInputStream.close();
}
}
BufferedOutputStream
package 包装流InputStream_OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: BufferedOutputStream 的使用
*/
public class BufferedOutputStream_demo {
public static void main(String[] args) throws IOException {
String path="z:\\helloWord.txt";
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(path,true));
bos.write(65);
bos.write(66);
// 关闭
bos.close();
}
}
转化流
inputStreamReader
package 转换流;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 将一个 字节流转成一个字符流
*/
public class inputStreamReader_demo {
public static void main(String[] args) throws IOException {
String path="z:\\zhouhao.txt";
// 给他指定一个 字符集 就不会出现乱码的问题了
InputStreamReader re = new InputStreamReader(new FileInputStream(path),"gbk");
BufferedReader bufferedReader=new BufferedReader(re);
String str=bufferedReader.readLine();
System.out.println(str);
bufferedReader.close();
re.close();
}
}
OutputStreamWriter
package 转换流;
import java.io.*;
import java.nio.charset.Charset;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 处理编码 乱码的问题
*/
public class OutputStreamWriter_demo {
public static void main(String[] args) throws IOException {
String path="z:\\hello.txt";
// 指定专门的字集符
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(path), "utf-8");
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
bufferedWriter.write("周浩好帅");
bufferedWriter.newLine();
bufferedWriter.close();
}
}
ObjectInput
package 序列化_反序列化;
import java.io.*;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析: 反序列化演示 Input
*/
public class ObjectInput_demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String path="z:\\zhouhao.txt";
ObjectInput oi=new ObjectInputStream(new FileInputStream(path));
// 取出数据的时候 顺序不能乱了 否则会报异常
System.out.println(oi.readInt());
System.out.println(oi.readBoolean());
System.out.println(oi.readChar());
System.out.println(oi.readUTF());
Object dog=oi.readObject(); //获取一直dog
System.out.println(dog);
// 如果要使用 dog的私有的方法的话 要向下转型 所以要放在能
//
//
// 引用到这个类才行
System.out.println(dog.getClass());
oi.close();
}
}
ObjectOutput
package 序列化_反序列化;
import java.io.*;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class ObjectOutput_demo {
public static void main(String[] args) throws IOException {
String path="z:\\zhouhao.txt";
ObjectOutput oo=new ObjectOutputStream(new FileOutputStream(path));
oo.writeInt(100); //会自动装箱实现 Serializable接口
oo.writeBoolean(true);
oo.writeChar('a');
oo.writeUTF("周浩好帅"); //序列化 一个string
oo.writeObject(new Dog("张疆疆",18));
// 关闭
oo.close();
}
}
//自定义类如果要实现 序列化 的话 就需要实现 Serializable接口
class Dog implements Serializable {
String name;
int age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
注意:对象处理流使用的细节
1、读写的顺序要一致
2、要求实现 序列化和反序列化对象 ,需要实现Serializable接口
3、序列化的类中建议添加SerialVersopnUID,为了提高版本的兼容性
4、序列化对象时,默认将里面所有的属进行序列化,但除了Static 和 transient 修饰的成员
5、序列化对象时,要求里面属性的类型也实现序列化接口
6、序列化具备可继承性,也就是如果某类已经实现了序列化,则他的所有子类也已经默认实现了序列化
打印流
1、打印流,只有输出流,没有输入流
PrintStream
package 打印流;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class PrintStream_{
public static void main(String[] args) throws FileNotFoundException {
// 打印流只有输出流,没有输入流 父类的话是 字节流
String path="z:\\hello.txt";
// 改变了文件的打印位置 ,
PrintStream p=new PrintStream(path);
p.println("周浩好帅啊~~~");
// 关闭
p.close();
// ------------------------------------------------
PrintStream p1=System.out;
// 默认打印的情况下 标准输出 显示器
p1.println("周浩");
// 改变打印的位置
System.setOut(new PrintStream(path));
// 此时 这个输出 就会输出到 path这个文件下面
System.out.println("zhouhao~~");
p1.close();
}
}
Printwriter
package 打印流;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class PrintWriter_ {
public static void main(String[] args) throws IOException {
// 父类的话是 Writer 字符流
PrintWriter printWriter=new PrintWriter(new FileWriter("z:\\hello.txt"));
printWriter.println("周浩好帅啊!!!!!");
// 当关闭了之后 ,文件才会真正的答应到文件中
printWriter.close();
}
}
Properties类
一、properties 常用的方法
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author ZhouHao
* @version 1.0
* 成为想成为的人
*/
//properties 用于读取配置文件
public class properties_method_demo {
public static void main(String[] args) throws IOException {
// 1、创建properties 对象
Properties properties = new Properties();
// 2、load 加载指定配置文件
properties.load(new FileReader("z:\\hello.properties"));
// 3、把他显示在控制台
properties.list(System.out);
// 4、根据 key 获取相应的值
String user = properties.getProperty("user");
System.out.println(user);
}
}
二、配置文件的创建
package ProPerties_demo;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
/**
* @author ZhouHao
* @version 1.0
* @ 思路分析:
*/
public class properties_02 {
public static void main(String[] args) throws IOException {
Properties properties=new Properties();
// 如果是中文的话 就会保存 unicode 码值
properties.setProperty("name","周浩");
// 如果key相同就是修改 没有就是创建
properties.setProperty("name","周浩好帅");
properties.setProperty("sex","man");
// 将文件存入进去即可 null 为配置文件的注解
properties.store(new FileWriter("Z:\\mysql~~.properties"),null);
}
}