java中的输入/输出针对的是内存进行的input/output。操作对象是文件file。
本篇文章主要介绍文件及字节流(流的最基本)。
File 文件/文件夹(目录)对象
不管是文件还是目录都用的是File对象
static void fileOperate() throws IOException {
File file = new File("f://d.txt");//用"/"转义
// 只是存在于内存中的一个对象而已,与实际磁盘上的文件无关。当做一些操作的时候才有关系。
// 路径只是这个对象的一个属性罢了。拿到文件对象的路径到磁盘上面去找这个文件进行操作
if (!file.exists()) {//判断文件是否存在
file.createNewFile();//创建文件
}
System.out.print(file.canRead());
System.out.print(file.getAbsolutePath());//此抽象绝对路径(即真实路径)
file.isDirectory();//是否为文件夹
file.isFile();//是否为文件
new Date( file.lastModified()) ;//最后修改时间
//file.delete();//删除此抽象路径名表示的文件(立即)
file.deleteOnExit();//程序运行结束(即虚拟机终止时)删除此象路径名表示的文件
System.out.print("执行完才删除");
try {
Thread.sleep(3000);//让程序停止3秒钟
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
文件与文件夹操作令基本相同,除个别:
static void directoryOperate() throws IOException {
File file = new File("f://a//b//c");// 此处file对象指的是c的这个目录,f://a//b是它的parent
if (!file.exists()) {// 判断文件夹是否存在
// file.mkdir();// 只创建一个文件夹,父文件夹如果不存在是不会创建的
file.mkdirs();
}
String str[] = file.list();// 目录中表示的目录和文件
File files[] = file.listFiles();// 目录中的文件
System.out.print(new Date(file.lastModified()));// 最后修改时间
// file.delete();//删除此抽象路径名表示的文件
file.deleteOnExit();// 程序运行结束(即虚拟机终止时)删除此象路径名表示的文件
}
利用迭带法列出文件夹下面的所有文件:
static void showsubFile(File dir) {
File files[] = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
//方法 出口 :是文件
System.out.println("\t"+file.getName());
} else {
System.out.println(file.getPath());
showsubFile(file);//迭带方法
}
}
对于文件操作,递归很重要,比如:
- 列出文件夹下所有文件和目录,
- 删除某一目录,
- 找出所有特定前缀/后缀的文件。
流:IO流 inputstream,outputstream
文件——>内存:输入,读
内存——>文件:输出,写
- 流的方向(根据内存):输入流,输出流
- 处理的内容:字节流,字符流
- 流的功能:节点流,转换流/包装流
节点流fileinputstream,fileoutputstream 最基本的,不管其他任何的流都要用到这个流
void streamOperatin() throws IOException {
File file = new File("f://d.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);// 文件输入流
int ch = fis.read();// 获得字节对应的ASC码
while(ch!=-1){
System.out.println(ch);
ch = fis.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {// 通常放资源释放的代码
try {
if (fis != null) {
fis.close();// 关闭流
fis = null;// 把流变成垃圾,等待释放内存
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- FileInputStream 的常用方法:
-
int
read()一个字节一个字节的读,返回的是读到的字节对应的ASC码。默认-1表示读完了
int
read(byte[] b)byte[]字节数组其到缓存作用,返回的是从缓存b中读到的字节个数。默认-1表示读完了
void streamOperatin() throws IOException {
File file = new File("f://d.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);// 文件输入流
byte[] buf = new byte[1024];// 字节数组,缓存作用
int lenth = -1;// 返回的是读到的字节个数,如果读不到返回的是-1;内容是读到buf缓冲区了
while ((lenth = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, lenth));// 打出读到的字符串
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {// 通常放资源释放的代码
try {
if (fis != null) {
fis.close();// 关闭流
fis = null;// 把流变成垃圾,等待释放内存
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件输出流fileoutputstream:从内存写入到文件
fileoutputstream的常用方法:
void | write(byte[] b) |
void | write(int b) |
void writefile() throws IOException {
File file = new File("f://d.txt");
FileOutputStream fos = new FileOutputStream(file);
byte[] b = "123".getBytes();
fos.write(b);//清空重写
fos.write(99);//写入int类型ASC码对应字符
fos.close();
}