I:Inout( 输入流:读取数据到内存)
O:Output(输出流:将数据输出到文件)
逐个数据按顺序处理,类似水流进行流动,称为IO流
Flie
File(文件的意思(文件和文件夹)): 对计算机来说文件就是指定盘符下的文件或者文件夹
File其中方法:
public File(String pathname) pathname:文件的路径;
创建文件:public boolean createNewFile() 创建文件,如果文件存在就不创建,返回false
创建文件夹:public boolean mkdir() 创建文件夹,如果文件夹存在,就创建,返回false,只能创建单个文件夹;
创建多层文件夹:public boolean mkdirs()
判断是否是文件夹:public boolean isDirectory()
判断是否是文件:public boolean isFile()
判断文件或目录是否存在 public boolean exists()
返回文件的长度,单位为字节 lenght
File file = new File("d:\\文件.txt");//在D盘创建一个txt类型的文件
boolean mkdir = file.mkdir();//创建一个文件夹
long length = file.length();//返回文件长度,单位为字节
System.out.println(file.exists());//判断文件/文件夹是否存在
字节流
字节输入流(FileInputStream):
字节输出流(FinleOutputStream):
字符流
字符流: 字符流的底层还是字节流, 字符流是专门处理文本的
字符输入流: FileReader
字符输出流: FileWriter
文件拷贝案例实现
import java.io.*;
public class Test {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
long start = System.currentTimeMillis();
try {
fis = new FileInputStream("D:\\lzb.txt");
fos = new FileOutputStream("D:\\test\\lzb.txt");
byte[] a = new byte[1024 * 512];
int len = fis.read(a);
while (len != -1) {
fos.write(a, 0, len);
len = fis.read(a);
}
long end = System.currentTimeMillis();
System.out.println("拷贝完毕" + (end - start) + "ms");
} catch (FileNotFoundException b) {
throw new RuntimeException(b);
} catch (IOException b) {
throw new RuntimeException(b);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException b) {
throw new RuntimeException(b);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException b) {
throw new RuntimeException(b);
}
}
}
}
}