文件
文件流
- 文件在程序中是以流的形式来操作的
- 流:数据在数据源(文件)和程序(内存)之间经历的路径
- 输入流:数据从数据源(文件)到程序(内存)的路径
- 输出流:数据从程序(内存)到数据源(文件)的路径
常用文件操作
创建文件对象相关构造器和方法
new File(String pathname)
new File(File parent, String child)
new File(String parent, String child)
createNewFile 创建新文件
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
}
@Test
public void create01(){
String filePath = "E:\\Java\\javaIO\\file\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create02(){
File parentFile = new File("E:\\Java\\javaIO\\file\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
public void create03(){
String parentPath = "E:\\Java\\javaIO\\file\\";
String fileName = "news3.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
获取文件的相关信息
getName getAbsolutePath getParent length exists isFile isDirectory
import org.junit.Test;
import java.io.File;
public class FileInformation {
public static void main(String[] args) {
}
@Test
public void info(){
File file = new File("E:\\Java\\javaIO\\file\\news1.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());
}
}
目录的操作和文件删除
mkdir创建一级目录 D:\file
mkdirs创建多级目录 D:\file\a\b\c
delete删除空目录或文件
import org.junit.Test;
import java.io.File;
public class Directory_ {
public static void main(String[] args) {
}
@Test
public void m1(){
String filePath = "E:\\Java\\javaIO\\file\\news1.txt";
File file = new File(filePath);
if(file.exists()) {
if(file.delete()) {
System.out.println(filePath + "删除成功");
} else {
System.out.println(filePath + "删除失败");
}
} else {
System.out.println("该文件不存在");
}
}
@Test
public void m2(){
String filePath = "E:\\Java\\javaIO\\file\\file01";
File file = new File(filePath);
if(file.exists()) {
if(file.delete()) {
System.out.println(filePath + "删除成功");
} else {
System.out.println(filePath + "删除失败");
}
} else {
System.out.println("该目录不存在");
}
}
@Test
public void m3(){
String directoryPath = "E:\\Java\\javaIO\\file\\file01";
File file = new File(directoryPath);
if(file.exists()) {
System.out.println(directoryPath + "已经存在");
} else {
if (file.mkdirs()) {
System.out.println(directoryPath + "创建成功");
} else {
System.out.println(directoryPath + "创建失败");
}
}
}
}
IO流原理及流的分类
javaIO流原理
- I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输,如读写文件,网络通讯等
- java程序中,对于数据的输入/输出操作以“流stream”的方式进行
- java.io包下提供了各种“流”类和接口,用于获取不同种类的数据,并提供方法输入或输出数据
- 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中
- 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中
流的分类
- 按操作数据单位不同分为: 字节流(8bit) 二进制文件,字符流(根据编码对应的字节不同,效率更高) 文本文件
- 按数据流的流向不同分为:输入流、输出流
- 按流的角色不同分为:节点流,处理流/包装流
抽象基类 |
字节流 |
字符流 |
输入流 |
InputStream |
Reader |
输出流 |
OutputStream |
Writer |
- java的IO类共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的
- 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀
IO流体系图-常用类
InputStream:字节输入流
- InputStream抽象类是所有类字节输入流的超类
- InputStream常用子类:
- FileInputStream:文件输入流
- BufferedInputStream:缓冲字节输入流
- ObjectInputStream:对象字节输入流
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStream_ {
public static void main(String[] args) {
}
@Test
public void readFile01() throws IOException {
String filePath = "E:\\Java\\javaIO\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02() throws IOException {
String filePath = "E:\\Java\\javaIO\\hello.txt";
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream:字节输出流
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStream01 {
public static void main(String[] args) {
}
@Test
public void writeFile(){
String filePath = "E:\\Java\\javaIO\\a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write('H');
String str = "hello,world!";
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
new FileOutputStream(filePath) 创建方式,当写入内容时,会覆盖原来的内容
new FileOutputStream(filePath, true) 创建方式,当写入内容时,是追加到文件后面
文件拷贝
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
String srcFilePath = "E:\\Java\\javaIO\\a.txt";
String destFilePath = "E:\\Java\\javaIO\\b.txt";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(srcFilePath);
fileInputStream = new FileInputStream(destFilePath);
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, readLen);
}
System.out.println("拷贝成功~");
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fileInputStream != null){
fileInputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}
FileReader
import org.junit.jupiter.api.Test;
import java.io.FileReader;
import java.io.IOException;
public class FileReader_ {
public static void main(String[] args) {
}
@Test
public void m1(){
String filePath = "E:\\Java\\javaIO\\a.txt";
FileReader fileReader = null;
int data = 0;
try{
fileReader = new FileReader(filePath);
while((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e){
e.printStackTrace();
} finally {
try{
if(fileReader != null){
fileReader.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
}
@Test
public void m2() {
String filePath = "E:\\Java\\javaIO\\a.txt";
FileReader fileReader = null;
int readLen = 0;
char[] buf = new char[8];
try {
fileReader = new FileReader(filePath);
while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter
- 也是字节流
- FileWriter使用后,必须要关闭(close)或者刷新(flush),否则写入不到指定的文件
import java.io.FileWriter;
import java.io.IOException;
public class FileWtriter_ {
public static void main(String[] args) {
String filePath = "E:\\Java\\javaIO\\note.txt";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(filePath);
fileWriter.write('H');
char[] chars = {'a','b'};
fileWriter.write(chars);
fileWriter.write("zxyccm".toCharArray(),0,3);
fileWriter.write("zxyccm");
fileWriter.write("zxyccm",0,3);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序结束~");
}
}