第三章、输入和输出处理
一、File类的常用方法
System.out.println("文件绝对路径: " + file.getAbsolutePath());
System.out.println("文件相对路径: " + file.getPath());
System.out.println("文件大小(字节): " + file.length());
// 获取详细属性
System.out.println("判断文件或目录是否存在"+file.exists());
System.out.println("是否为目录: " + file.isDirectory());
System.out.println("是否为隐藏文件: " + file.isHidden());
System.out.println("是否可读: " + file.canRead());
System.out.println("是否可写: " + file.canWrite());
System.out.println("是否是文件: "+file.isFile());
二、读写文件
输入输出流是相对于计算机内存来说的
字节流的基类是InputStream和OutputStream
一、字节输入流 InputStream
InputStream类常用方法
字节流
int read( ) //读取一个字节的数据
int read(byte[] b)//读取的数据放入数组中
int read(byte[] b,int off,int len)// 读取0ff-len长度的字节 放入数组中
void close( )// 关闭文件输入流
int available():检查输入流中是否还有可读的字节
int read(): 从输入流中读取一个字节的数据并返回。如果已经到达文件末尾,则返回-1。
int read(byte[] b): 从输入流中读取一定数量的字节数据并将其存储到字节数组b中。返回实际读取的字节数,如果已经到达文件末尾,则返回-1。
int read(byte[] b, int off, int len): 从输入流中读取最多len个字节的数据并将其存储到字节数组b中,从偏移量off开始。返回实际读取的字节数,如果已经到达文件末尾,则返回-1。
void close(): 关闭此输入流并释放与该流关联的所有系统资源。在关闭流之后,不能再对其进行读写操作。
int available(): 返回可以从输入流中读取(或跳过)而不会阻塞的估计字节数。这个方法主要用于非阻塞I/O操作
子类FileInputStream常用的构造方法
1、 FileInputStream(File file)
File file = new File("example.txt"); // 指定文件路径
FileInputStream fis1 = new FileInputStream(file);
2、FileInputStream(String name)
String filePath = "example.txt"; // 指定文件路径
FileInputStream fis2 = new FileInputStream(filePath);
二、字节输出流OutputStream
void write(int c)
void write(byte[] buf)
void write(byte[] b,int off,int len)//跟输入是一样的作用
void close()//关闭流
void flush():强制把缓冲区的数据写到输出流中
FileOutputStream fos =new FileOutputStream("C:\\Users\\yu\\myPrime.txt")
fos.write(buffer, 0, bytesRead);//(0-最后一共字符)写入
子类FileOutputStream常用的构造方法
FileOutputStream (File file)
FileOutputStream(String name)
//前两个跟inputstream同理
FileOutputStream(String name,boolean append)
append的因为意思是附加或者增补,意思就是是否添加在末尾
FileOutputStream(String name, boolean append): 通过给定的文件路径字符串和一个布尔值来创建一个FileOutputStream实例。如果append参数为true,那么新写入的数据将被追加到文件末尾,而不是覆盖原有内容;如果append参数为false(默认值),则新写入的数据将覆盖原有内容
FileOutputStream fos = new FileOutputStream("example.txt", true);
str.getBytes() 是一个Java方法,用于将字符串转换为字节数组。在这个例子中,str 是一个字符串变量,其值为 "好好学习Java"。调用 str.getBytes() 会返回一个字节数组,其中包含了字符串中每个字符的字节表示形式。
三、字符流
字符流的基类则是Reader和Writer
一、Reader 类
常用方法
int read( )
int read(char[] c) //读取字符
read(char[] c,int off,int len)//读取的字符放在char 数组内 ,从off-len
void close( ) //关闭流,释放内存
1.子类InputStreamReader
常用的构造方法
InputStreamReader(InputStream in): 通过给定的InputStream对象创建一个InputStreamReader实例,使用默认的字符集进行解码。默认字符集通常是根据系统属性或JVM设置确定的,例如UTF-8。
InputStream inputStream = new FileInputStream("example.txt");
InputStreamReader reader = new InputStreamReader(inputStream);
InputStreamReader(InputStream in, String charsetName): 通过给定的InputStream对象和一个字符集名称创建一个InputStreamReader实例,使用指定的字符集进行解码。字符集名称可以是标准的字符集名称(如"UTF-8"、"ISO-8859-1"等)或者自定义的字符集名称。
InputStream inputStream = new FileInputStream("example.txt");
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
2.FileReader类是InputStreamReader的子类
FileReader(File file)
FileReader(String name)
FileReader只能用于读取文本文件(如.txt、.java等),而不能用于读取二进制文件(如.jpg、.png等)。如果需要读取二进制文件,可以使用
FileInputStream 字节输入类。
FileReader读取字符文件
Reader fr= new FileReader("D:\\myDoc\\简介.txt");//父类new 子类 多态
fr.read(); //读
fr.close(); //关闭
3.BufferedReader类
高字符流读取文本文件的效率
BufferedReader类是Reader类的子类
BufferedReader类带有缓冲区
按行读取内容的readLine()方法
步骤
Reader fr=new FileReader("C:\\myTest.txt ");
BufferedReader br=new BufferedReader(fr);
br.readLine();
br.close();
fr.close();
读取包含中文的文本文件时,可能出现中文乱码
FileInputStream fis=new FileInputStream("c:\\myDoc\\hello.txt");
//使用InputStreamReader并设置编码格式
InputStreamReader fr=new InputStreamReader(fis,"UTF-8");
BufferedReader br=new BufferedReader(fr);
二、Writer类
write(String str)
write(String str,int off,int len)
void close()
void flush()
1.子类OutputStreamWriter
常用的构造方法
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out,String charsetName)
// 创建一个 FileOutputStream 对象
FileOutputStream fos = new FileOutputStream("output.txt");
// 创建一个使用默认字符集的 OutputStreamWriter 对象
OutputStreamWriter osw1 = new OutputStreamWriter(fos);
// 创建一个使用指定字符集的 OutputStreamWriter 对象
OutputStreamWriter osw2 = new OutputStreamWriter(fos, "UTF-16");
2.FileWriter类是OutputStreamWriter的子类
FileWriter (File file)
FileWriter (String name)
该类只能按照本地平台的字符编码来写数据,用户不能指定其他的字符编码类型
Writer fw= new FileWriter("D:\\myDoc\\简介.txt");
fw.write();
fw.close();
3.BufferedWriter
提高字符流写文本文件的效率
BufferedWriter类是Writer类的子类
BufferedWriter类带有缓冲区
/要写入的文件
FileWriter fw=new FileWriter("C:\\myTest.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("hello");//写入的内容
bw.flush();
fw.close();
四、读写二进制文件
1.DataInputStream类
FileInputStream的子类
与FileInputStream类结合使用读取二进制文件
FileInputStream fis=new FileInputStream("C:\\HelloWorld.class");
DataInputStream dis=new DataInputStream(fis);
dis.read ();
2.DataOutputStream类
FileOutputStream的子类
与FileOutputStream类结合使用写二进制文件
FileOutputStream outFile=new FileOutputStream("C:\\temp.class");
DataOutputStream out=newDataOutputStream(outFile);
out.write();
五、序列化与反序列化
1.序列化(写入文件)
就是将相关内容读取出来,写到一个文件中,方便以后随时使用
FileOutputStream fileOut = new FileOutputStream("D:\\我的青春谁做主.txt");
//写入这个文件
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
2.反序列化(从文件中读出)
就是将在文件内的内容读取出来
// 反序列化对象,读取内容
FileInputStream fileIn = new FileInputStream("D:\\我的青春谁做主.txt");//读取内容的文件
ObjectInputStream in = new ObjectInputStream(fileIn);
Person persons = (Person) in.readObject();//对象转换
in.close();
fileIn.close();
System.out.println("Deserialized person: " + persons);//打印结果
序列化对象,读取内容
FileInputStream fileIn = new FileInputStream(“D:\我的青春谁做主.txt”);//读取内容的文件
ObjectInputStream in = new ObjectInputStream(fileIn);
Person persons = (Person) in.readObject();//对象转换
in.close();
fileIn.close();
System.out.println("Deserialized person: " + persons);//打印结果