2025.2.24学习内容
day27视频
一、计算机的磁盘里面的数据主要分为两类型的
1、文件夹(目录):用来管理文件的
2、文件
二、文件的分类:
文件分类:.doc .txt .excel .ppt .pdf .jpg .mp3/4 .exe .java .html .class .dll .md .xml .properties .yml
存储数据的集合,文本文件,二进制文件
三、File类的常用方法
方法名称 | 说明 |
---|---|
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此对象表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length() | 返回文件的长度,单位为字节**,** 如果文件不存在,则返回 0L |
mkdir | 创建文件夹 |
eg:
注意:当一个目录下有文件存在是先需要删除文件才能再删除目录.
四、IO流
通过流来读写文件
流是一组有序的数据序列
以先进先出的方式发送信息都通道
java流的分类
按流向区分:
1、输出流:OutputStream和Writer作为基类
2、输入流:InputStream和Reader作为基类
输入流的操作就是读
输出流的操作就是写
输入输出流是相对于计算机内存来说的
按处理数据单元划分
1、字节流:字节输入流InputStream基类 字节输出流OutputStream基类
2、字符流:字符输入流Reader基类 字符输出流Writer基类
字节流是8位通用字节流,字符流水16为Unicode字符流。
字节流可以处理二进制文件,包括图片、视频、音频、.class等
字符流可以处理文本文件
五、字节输入流
//方式一
FileInputStream fis = new FileInputStream("d:\\hello\\abc.txt");
//方式二
File file = new File("d:\\hello\\abc.txt");
FileInputStream fis1 = new FileInputStream(file);
read读取方式
//方式一
FileInputStream fis = new FileInputStream("d:\\hello\\abc.txt");
int read1 = fis.read();
System.out.println((char) read1);
int read2 = fis.read();
System.out.println((char) read2);
int read3 = fis.read();
System.out.println((char) read3);
fis.close();
进阶:
//方式一
FileInputStream fis = new FileInputStream("d:\\hello\\abc.txt");
int data=0;
// byte [] bytes=new byte[1024];
StringBuffer str=new StringBuffer();
while ((data=fis.read())!=-1){
//System.out.println((char) data);
str.append((char)data);
}
String s = str.toString();
System.out.println("读取到的结果为:"+s);
fis.close();
-
int read()
:从输入流中读取一个字节的数据,并返回读取到的字节值(0 - 255),若读到流末尾返回 -1。//方式二 FileInputStream fis = new FileInputStream("d:\\hello\\abc.txt"); //建立缓冲区 byte [] bytes=new byte[1024]; int read = fis.read(bytes); //int read代表的是读取到的字节个数 // for (int i = 0; i < bytes.length; i++) { // System.out.println(bytes[i]); // } String str=new String(bytes); //使用byte数组构建了一个字符串对象 System.out.println(str); fis.close();
//方式二(优化) FileInputStream fis = new FileInputStream("d:\\hello\\abc.txt"); byte[] bytes=new byte[1024]; //abc def StringBuffer sb=new StringBuffer(); while (fis.read(bytes,0,3)!=-1){ String str=new String(bytes); sb.append(str); } String s = sb.toString(); System.out.println("内容:"+s); fis.close(); //也可以使用read(bytes)
-
int read(byte[] b)
:从输入流中将数据读入指定的字节数组b
中,返回实际读取的字节数,若已到流末尾返回 -1。 -
int read(byte[] b, int off, int len)
:从输入流中读取最多len
个字节的数据,存储到字节数组b
从索引off
开始的位置,==返回实际读取的字节数,==若已到流末尾返回 -1 -
read方法,无参数用int来接收,得到的是字节内容,如果使用参数,得到的是每次读取的字节个数
int available():方法返回的是文件输入流中可读取的字节数。
void.close(); 这段代码的功能是关闭文件输入流 ,释放与该流相关的系统资源。如果不关闭流,可能会导致资源泄露,影响程序性能或引发异常。
注意:当使用int read(byte[] b)
时,如果字节数组长度比较小,内容比较多时,那下一次读取数字时会覆盖前面的数据.
六、字节输出流
File file=new File("d:/a.txt");
FileOutputStream fos=new FileOutputStream(file,true);//true 支持追加内容
String str="hello,java! hello,io";
byte[] bytes = str.getBytes(); //把字符串转换成一个byte数组
fos.write(bytes,5,10);//从字符串的索引的第5位开始,输出10位字符
System.out.println("输出成功");
fos.close();
持追加内容
String str="hello,java! hello,io";
byte[] bytes = str.getBytes(); //把字符串转换成一个byte数组
fos.write(bytes,5,10);//从字符串的索引的第5位开始,输出10位字符
System.out.println("输出成功");
fos.close();