网站开发员工结构,做网站 需要了解什么,多媒体制作公司,内衣网站建设推广文件(File)
狭义的文件: 指的是硬盘上的文件和目录 广义的文件: 泛指计算机中很多软硬件资源(操作系统中把很多硬件和软件资源抽象成了文件, 按照文件的方式同意管理)
本章内容只讨论狭义的文件
路径
绝对路径: 以c: , d: 盘符开头的路径相对路径: 以当前所在的目录为基准(…文件(File)
狭义的文件: 指的是硬盘上的文件和目录 广义的文件: 泛指计算机中很多软硬件资源(操作系统中把很多硬件和软件资源抽象成了文件, 按照文件的方式同意管理)
本章内容只讨论狭义的文件
路径
绝对路径: 以c: , d: 盘符开头的路径相对路径: 以当前所在的目录为基准(工作目录), 以. 或 … 开头(有时 . 可以省略), 找到指定的路径
那么我们的IDEA的工作路径在哪呢? IDEA 的工作路径默认就是在当前的项目所在目录
Java 对于文件的操作
针对文件系统操作(文件的创建, 删除, 重命名)针对文件内容的操作(文件的读和写)
Java 标准库中提供了File这个类 parent 表示当前文件所在目录 child 表示自身的文件名 File file new File(d:/text.txt);构建一个File 对象
import java.io.File;
import java.io.IOException;public class IODemo1 {public static void main(String[] args) throws IOException {File file new File(./test1.txt);// 获取文件名System.out.println(file.getName());// 获取父级路径System.out.println(file.getParent());// 获取完整路径System.out.println(file.getPath());// 获取绝对路径System.out.println(file.getAbsoluteFile());// 获取绝对路径的简化路径System.out.println(file.getCanonicalPath());}
}判断文件
import java.io.File;public class IODemo2 {public static void main(String[] args) {File file new File(d:/f1/f2/test1.txt);// 这个文件是否存在System.out.println(file.exists());// 这是不是一个文件System.out.println(file.isFile());// 这是一个目录吗System.out.println(file.isDirectory());}
}创建文件
import java.io.File;
import java.io.IOException;public class ThreadDemo3 {public static void main(String[] args) throws IOException {File file new File(./test.txt);//创建这个文件file.createNewFile();}
}删除文件
import java.io.File;public class IODemo4 {public static void main(String[] args) {File file new File(./test.txt);// 删除文件file.delete();}
}deleteOnExit import java.io.File;
import java.io.IOException;public class IODemo5 {public static void main(String[] args) throws IOException {File file new File(test.txt);System.out.println(file.exists());System.out.println(file.createNewFile());System.out.println(file.exists());//程序运行结束后删除file.deleteOnExit();System.out.println(file.exists());}
}目录的创建
import java.io.File;public class IODemo6 {public static void main(String[] args) {File dir new File(d:/f1/f2/f3/f3);dir.mkdir(); // 只能创建一级目录dir.mkdirs(); // 创建多级目录}
}文件的重命名
import java.io.File;public class IODemo7 {public static void main(String[] args) {File file new File(./aaa/bbb/test.txt);File file1 new File(./aaa/bbb/test2.txt);file.renameTo(file1);}
}Java 标准库的流对象
从类型上分成两个大类
字节流: 操作二进制数据的字符流: 操作文本数据 read 无参数版本:一次只读一个字节 read 有一个参数版本: 把读到的内容填充到参数的这个字节数组中, (此处的参数是一个输出型参数), 返回值是实际读取的字节数 read 有三个参数的版本: 和一个参数的差不多. 只不过是往数组的一部分区间里尽可能填充
思考: read 读取的是一个字节, 按理说应该返回一个 byte , 但是实际上是返回int, 这是为什么呢? 这里除了表示byte里的0~255 (-128 ~ 127) 这样的情况外, 还需要表示一个特殊情况, -1,这个标记表示读取文件结束了(读到末尾了). UTF-8 每个汉字三个字节
字节流读取:
import java.io.*;public class IODemo8 {// 使用字节流读取文件public static void main(String[] args) throws IOException {// 创建InputStream 对象的时候, 使用绝对路径或者相对路径都行, 也可以使用File对象InputStream inputStream new FileInputStream(d:/test.txt);// //进行读操作
// while (true) {
// int b inputStream.read();
// if(b -1) {
// // 读取完毕
// break;
// }
// //System.out.println( (byte)b);
// System.out.printf(%x,(byte)b);
// }while (true) {byte[] buffer new byte[1024];int len inputStream.read(buffer);System.out.println(len: len);if (len -1) {break;}// 此时读取的字符就被放到了byte[] 中了.for (int i 0; i len; i) {System.out.printf(%x\n, (byte)buffer[i]);}}inputStream.close();}
}buffer 存在的意义,是为了提高 IO 操作的效率, 单次 IO 操作, 是要访问硬盘的, 所以就会比较耗费时间, 缩短了 IO 操作的次数, 所以效率也就提高了 字节流写文件 也是类似的三个方法
public class IODemo9 {// 进行写文件public static void main(String[] args) throws IOException {OutputStream outputStream new FileOutputStream(d:/test.txt);outputStream.write(97);outputStream.write(98);outputStream.write(99);outputStream.write(100);outputStream.close();}
}更推荐的写法 这个写法虽然没有显示的写 close , 但是在 try 语句执行完的时候, 会自动执行到 close
public class IODemo {public static void main(String[] args) throws IOException {try (OutputStream outputStream new FileOutputStream(d:/test.txt)) {outputStream.write(97);outputStream.write(97);outputStream.write(97);outputStream.write(97);outputStream.write(97);outputStream.write(97);}}
}字符流读文件
package io;import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;public class IODemo10 {// 字符流的操作public static void main(String[] args) {try (Reader reader new FileReader(d:/test.txt)) {while (true) {int ch reader.read();if (ch -1) {break;}System.out.println( (char)ch);}} catch (IOException e) {throw new RuntimeException(e);}}
}字符流写文件
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;public class IODemo11 {// 字符流写文件public static void main(String[] args) {try (Writer writer new FileWriter(d:/test.txt)) {writer.write(hello world);} catch (IOException e) {throw new RuntimeException(e);}}
}下面我们实现一个删除文件的小程序
import java.io.File;
import java.util.Scanner;public class IODemo12 {private static Scanner scanner new Scanner(System.in);public static void main(String[] args) {// 让用户输入一个指定搜索的目录System.out.println(请输入一个要搜索的路径: );String basePath scanner.next();// 针对用户输入进行简单判定,File root new File(basePath);if (!root.isDirectory()) {// 路径不存在, 或者是一个普通文件, 此时无法进行搜索System.out.println(输入的路径有误);return;}System.out.println(请输入要删除的文件名: );// 此处使用 next, 不用 next.LineString nameToDelete scanner.next();// 针对指定路径进行扫描, 递归操作// 先从根目录出发(root)// 先看看这个目录是否有我们要删除的文件, 如果有就删除, 否则下一个// 如果是包含了一些目录, 那就进行递归scanDir(root,nameToDelete);}private static void scanDir(File root, String nameToDelete) {// 1. 先列出当前路径下包含和目录File[] files root.listFiles();if (files null) {// 当前目录没东西, 进行下一步递归return;}// 遍历当前列出的结果for (File f: files) {if (f.isDirectory()) {// 如果是目录就进一步递归scanDir(f,nameToDelete);} else {// 如果是文件就判断if (f.getName().contains(nameToDelete)) {System.out.println(确认要删除 f.getName() 吗?);String choice scanner.next();if (choice.equals(Y) || choice.equals(y)) {f.delete();System.out.println(删除成功);} else {System.out.println(删除取消);}}}}}
}文件的复制 就是把一个文件按照字节一次读取, 把结果写入另一个文件中
import java.io.*;
import java.util.Scanner;// 文件的拷贝
public class IODemo13 {public static void main(String[] args) throws RuntimeException {// 输入两个路径 一个源 一个目标路径 (从哪里拷贝到哪)Scanner scanner new Scanner(System.in);System.out.println(请输入要拷贝哪个文件: );String srePath scanner.next();System.out.println(请输入要拷贝到哪个地方: );String destPath scanner.next();File srcFile new File(srePath);if (!srcFile.isFile()) {System.out.println(您输入的源路径有误!);return;}File destFile new File(destPath);if (destFile.isFile()) {// 如果已经存在不能拷贝System.out.println(您输入的目标路径有误: );return;}// 进行拷贝操作try (InputStream inputStream new FileInputStream(srcFile);OutputStream outputStream new FileOutputStream(destFile)) {// 进行读文件操作while (true) {int b inputStream.read();if (b -1) {break;}outputStream.write(b);}} catch (IOException e) {e.printStackTrace();}}
}
文章转载自: http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn