IO流
文件基础知识
文件在程序中是以流的形式才操作的
文件(磁盘)----> java程序(内存)文件读取数据到程序中叫做输入流
java程序(内存)---->文件(磁盘) 把java程序中的数据或内存,输出到磁盘中叫做输出流
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
创建文件
//方式1 new File(String pathname)根据路径构建一个File对象
//方式2 new File(File patent,String child)根据父类目录文件+子路径构建
//方式3 new File(String pathname)根据父类目录文件+子路径构建
//方式1 new File(String pathname)根据路径构建一个File对象
@Test
public void create01(){
String filePath = "d:\\news1.text";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("创建文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式2 new File(File patent,String child)根据父类目录文件+子路径构建
@Test
public void create03(){
File parentFile= new File("d:\\");
String fileName= "\\news3.text";
//这里的file对象,在java中只是一个对象
//只有执行createNewFile()方法才会在磁盘中创建该文件
File file = new File(parentFile,fileName);
try {
//
file.createNewFile();
System.out.println("创建文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式3 new File(String pathname)根据父类目录文件+子路径构建
@Test
public void create02(){
File parentFile= new File("d:\\");
String fileName= "\\news2.text";
//这里的file对象,在java中只是一个对象
//只有执行createNewFile()方法才会在磁盘中创建该文件
File file = new File(parentFile,fileName);
try {
//
file.createNewFile();
System.out.println("创建文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
获取文件信息
方法 | 返回类型 | 作用 |
---|---|---|
getName() | String | 获取文件名称 |
isDirectory() | boolean | 是不是一个目录 |
isFile() | boolean | 是不是一个文件 |
exists() | boolean | 文件是否存在 |
length() | long | 获取文件大小(字节) |
getParent() | String | 获取文件父级目录 |
getAbsolutePath() | String | 获取文件绝对路径 |
getPath() | String | 获取文件相对路径 |
目录的操作和文件的删除
方法/返回值 | 作用 |
---|---|
mkdir()/boolean | 创建一级目录 |
mkdirs()/boolean | 创建多级目录 |
delete() | 删除空目录或文件(只能删除空的,如果里面有文件必须先清空) |
IO流原理
Java IO流原理
1.I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输。
如读/写文件,网络通讯等。
2. Java程序中,对于数据的输入/输出操作以”流(stream)”的方式进行。
3. java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方
法输入或输出数据
aa
4.输入input:读取外部数据
(磁盘、光盘等存储设备的数据)到程序(内存)中。
5.输出output:将程序(内存)
数据输出到磁盘、光盘等存储设备中
流的分类
IO流四大分类
InputStream
OutputStream
FileReader
FileWriter
这四个都是抽象类
按操作数据单位不同分为:字节流(8bit)适合二进制文件,字符流(按字符)适合文本文件
按数据流向不同分为:输入流,输出流
按流的角色的不同分为:节点流,处理流也叫包装流
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
1.Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的
2.有这四个类派生出来的子类名称都是以其父类名作为子类名后缀
文件和流的关系
文件 就像是 外卖
流 就像是 外卖小哥
外卖通过外卖小哥运输====文件通过流来运输
FileInputStream
构造方法
Constructor and Description |
---|
FileInputStream(File file) 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File 对象 file 命名。 |
FileInputStream(FileDescriptor fdObj) 创建 FileInputStream 通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。 |
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name 命名。 |
方法
Modifier and Type | Method | Description |
---|---|---|
int |
available() |
返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 |
void |
close() |
关闭此文件输入流并释放与流相关联的任何系统资源。 |
protected void |
finalize() |
确保当这个文件输入流的 close 方法没有更多的引用时被调用。 |
FileChannel |
getChannel() |
返回与此文件输入流相关联的唯一的FileChannel 对象。 |
FileDescriptor |
getFD() |
返回表示与此 FileInputStream 正在使用的文件系统中实际文件的连接的 FileDescriptor 对象。 |
int |
read() |
从该输入流读取一个字节的数据。 |
int |
read(byte[] b) |
从该输入流读取最多 b.length 个字节的数据为字节数组。 |
int |
read(byte[] b, int off, int len) |
从该输入流读取最多 len 字节的数据为字节数组。 |
long |
skip(long n) |
跳过并从输入流中丢弃 n 字节的数据。 |
@Test
public void readFile01(){
String filePath = "d:\\Hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取 文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止。
//如果返回-1,表示读取完毕
while ((readData = fileInputStream.read()) !=-1){
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭流节省资源
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 使用read(byte[] b) 读取文件提高效率
*
* */
@Test
public void readFile02(){
String filePath = "d:\\Hello.txt";
//字节数组
byte[] bytes = new byte[8];//一起读8个字节
int readLen= 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取 文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用
//如果返回-1,表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((readLen = fileInputStream.read(bytes)) !=-1){
//把读到的字节转换成String类型
System.out.print(new String(bytes,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭流节省资源
fileInputStream.close();
} catch (IOException e) {
e.<