点餐网站模板 手机端,网站开发主要做哪些,做化工贸易要用那些网站推广,个人注什么域名的网站文件
文件有两个概念#xff0c;在广义来看就是操作系统上对硬件和软件资源抽象为文件。 在侠义上来看#xff0c;就是我们保存在硬盘上的文件
在这里我们讨论的是狭义的文件#xff0c;在外面的硬盘上的文件细分又可以分为二进制文件和文本文件#xff0c;文本文件可以通…文件
文件有两个概念在广义来看就是操作系统上对硬件和软件资源抽象为文件。 在侠义上来看就是我们保存在硬盘上的文件
在这里我们讨论的是狭义的文件在外面的硬盘上的文件细分又可以分为二进制文件和文本文件文本文件可以通过码表转换成现实生活中有意义的文字而二进制文件则是我们看不懂的文件。
我们可以通过记事本打开一个文件如果里面的乱码则说明这是二进制文件如果你能看懂说明这是文本文件。
其实文件本质上都是二进制文件上面的文件只是根据文件是否表达人类语言的意义来进行划分的在计算机里文件的存储都是以二进制的方式进行存储的。
绝对路径就是文件的具体位置信息没有省略 相对路径./text.txt 就在当前路劲下的 text.txt …/text.txt 表示当前路劲的上一个路径 windows 操作系统路径表示支持斜杠 / 和 反斜杠 \ 两种符号其他操作系统只能使用 斜杠 / 来对路径进行分割 我们使用 斜杠 / 来分割路径就可以了反正 windows 操作系统也能支持。 File
在Java 中标准库给我们提供了可以操作文件或者目录的类 File。 注意 File 不仅仅可以操作文件也可以操作目录。 下面是 File 的构造方法 这里如果你使用的是相对路径进行传参那这个相对路径的基准值就是程序存在在哪个文件夹下这个路径就是基准值因为基准值是根据程序运行而改变的。 File 提供的方法
import java.io.File;
import java.io.IOException;public class Demo1 {public static void main(String[] args) throws IOException {File file new File(./test.txt);System.out.println(file.getPath());System.out.println(file.getParent());System.out.println(file.getAbsolutePath());System.out.println(file.getCanonicalPath());System.out.println(file.exists());}
}首先要注意即使没有存在 file 这个文件File 在实例化的时候是没有问题的。 public static void main(String[] args) throws IOException {File file new File(./test.txt);System.out.println(file.createNewFile());}如果文件没有存在会成功创建并且返回 true如果文件已经存在就会创建失败并且返回false. public static void main(String[] args) throws IOException {File file new File(./test.txt);file.createNewFile();System.out.println(file.exists());System.out.println(file.delete());System.out.println(file.exists());}这个就是简单的删除没什么好说的。 public static void main(String[] args) throws IOException {File file new File(./test.txt);file.createNewFile();System.out.println(file.exists());file.deleteOnExit();System.out.println(file.exists());}deleteOnExit() 这个是等到进程结束才删除这个文件。 public static void main(String[] args) {File file new File(./text.txt);file.mkdir();}mkdir 是创建一级目录如果涉及到多级目录的创建是无法完成的多级目录的创建要使用 mkdirs() 方法。 public static void main(String[] args) {File file new File(./text.txt/2005/04/02);file.mkdirs();}list() 和 listFile() 都是获取当前目录下的所有文件只是返回值类型不同list() 是返回 String 类型listFile() 则是返回 File 类型。 public static void main(String[] args) {File file new File(D:/code/Java/java_study/J2024_11_2);String[] list file.list();System.out.println(Arrays.toString(list));File[] list2 file.listFiles();System.out.println(Arrays.toString(list2));}IO 流
字节流
字节流是以字节为单位的读取主要用于二进制文件的读写。
字符流在骄Java中有两个类需要掌握一个是 InputStream 另一个是 OutputStream 。
首先我们要知道这两个类都是抽象类是不能直接实例化的。
接着我们还要知道输入流是用来读取还是写入 输入输出的定义是我们站在CPU 的视角数据往 CPU 来就是输入数据从 CPU 离开就是 输出。 因此输入流 InputStream 就是用来读取数据的OutputStream 是用来写入数据的。
最后我们知道文件的打开是会消耗资源的当我们使用完文件之后我们需要关闭资源避免资源泄漏其实这也是因为操作系统在每次打开文件就会占用文件描述表的一个位置并且这个文件描述表是不会自动扩容的所以能打开的文件有限不能只打开不关闭文件这样文件描述表在某一个时刻就会被消耗殆尽导致后续再打开文件的操作失败引发 BUG。
我们关闭文件除了可以使用 close 方法之外我们可以直接在 try(打开文件的代码){}当出了 try 代码块就会自动关闭文件这是因为Java 的 IO 流的类 实现了 Closable 接口。 既然我们不能直接实例化 InputStream 和 OutputStream 的话我们可以使用 FileInputStream 和 FileOutputStream 来实例化因为它们分别继承了InputStream 和 OutputStream听名字大家应该知道这两个类其实就是用来操作文件的。 FileInputStream InputStream 提供的方法 FileInoutStream 的构造方法 代码演示 首先在 test.txt 文件中写上 hello java read() 演示 public static void main(String[] args) throws IOException {try(InputStream inputStream new FileInputStream(./test.txt);) {int n 0;while (n ! -1) {n inputStream.read();System.out.println(n);}}}read(byte[] b) 演示这个会首先填满 byte 数组并且返回一个数值表示读取了多少个字节。 这种也叫做输出型参数就是我们传入的引用参数到一个方法里面这个方法会直接修改我们传入的引用类型参数并且这个参数的修改也影响到方法外部的参数这种参数我们称之为输出型参数。 public static void main(String[] args) throws IOException {try(InputStream inputStream new FileInputStream(./test.txt);) {byte[] b new byte[1024];while(true) {int n inputStream.read(b);if(n -1) {break;}for (int i 0; i n; i) {System.out.println(b[i]);}}}}FileOutputStream OutputStream 提供的方法 FileOutputStream 的构造方法和 FileInputStream 的参数是一样的。 如果文件不存在的话FileOutputStream 会自动创建好文件然后进行写操作。 public static void main(String[] args) throws IOException {try(OutputStream outputStream new FileOutputStream(./test.txt);) {outputStream.write(65);outputStream.write(66);outputStream.write(67);}}public static void main(String[] args) throws IOException {try(OutputStream outputStream new FileOutputStream(./test.txt);) {byte[] b {97,98,99};outputStream.write(b);}}当你进行写操作的时候是直接覆盖之前的内容再进行写操作如果你想要进行的是追加写的话在构造方法中加上一个 true这样就是追加写。 public static void main(String[] args) throws IOException {try(OutputStream outputStream new FileOutputStream(./test.txt,true);) {byte[] b {97,98,99};outputStream.write(b);}}字符流
字符流是以字符为单位进行存储的主要用于文本文件的读写
字符流一样给我们提供了两个抽象类 Reader 和 Writer我们可以通过实例化 FileReader 和 FileWriter
我们先将文本内容改为 你好
Reader
Reader 提供的方法 read() 是读取一个字符read(char[] chuf) 是将字符数组填充读取到的字符read(CharBuffer target) 其中的CharBuffer 是对char[] 进行了封装。 read(char[] cbuf, int off, int len) 就是指定将读取到的字符填充到字符数组的哪个位置。 public static void main(String[] args) throws IOException {try(Reader reader new FileReader(./test.txt)) {while(true) {char[] b new char[1];int n reader.read(b);if(n -1) {break;}for (int i 0; i n; i) {System.out.println(b[i]);}}}}在Java中使用 utf8进行编码明明一个中文占3个字节为什么我用 char(两个字节) 就可以读取出来 字符流读取到的是文件中原始的数据也就是 两个汉字占 6 个字节utf8 编码字符流会根据文件的编码方式进行解析read() 一次就会读取到 3 个字节返回的时候会针对这 3 个字节进行转码也就是先将 这个 3 个字节对照 utf8 码表查询到 ‘你’ 这个汉字之后read 又将 “你” 这个汉字在 unicode 这个码表进行查询得到 unicode 编码这样就是两个字节最后将这两个字节的数值返回到 char 变量中。 public static void main(String[] args) throws IOException {try(Reader reader new FileReader(./test.txt)) {char[] arr new char[2];while(true) {int n reader.read(arr);if(n -1) {break;}for (int i 0; i n; i) {System.out.println(arr[i]);}}}}Writer
Writer 提供的方法 public static void main(String[] args) throws IOException {try(Writer writer new FileWriter(./test.txt)) {writer.write(hello world);}}你想要追加写也是一样加上 true 就可以了。 public static void main(String[] args) throws IOException {try(Writer writer new FileWriter(./test.txt,true)) {writer.write(hello world);}}实战演练
扫描指定目录并找到名称中包含指定字符的所有普通文件不包含目录并且后续询问用户是否要删除该文件
在遍历文件的时候我们只能遍历到一层的文件如果该文件夹里面还有文件的话我们需要进行递归才能获取到内部的文件。
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Demo1 {public static void main(String[] args) {System.out.println(请输入指定目录);Scanner scan new Scanner(System.in);String str scan.next();File file new File(str);if(!file.isDirectory()) {System.out.println(您输入的不是目录或者该目录不存在...);return;}System.out.println(请输入指定的字符);String token scan.next();ListFile list new ArrayList();scanFile(file, token);}private static void scanFile(File file, String token) {File[] ret file.listFiles();if(ret null) {return;}for (int i 0; i ret.length; i) {if(ret[i].isDirectory()) {scanFile(ret[i], token);} else {delFile(ret[i], token);}}}private static void delFile(File file, String token) {if(file.getName().contains(token)) {System.out.println(该文件包含你指定的字符内容 file.getAbsoluteFile());System.out.println(是否进行删除Y / N);Scanner scan new Scanner(System.in);String ret scan.next();if(ret.equals(Y)) {file.delete();}}}
} 进行普通文件的复制
import java.io.*;
import java.util.Scanner;public class Demo2 {public static void main(String[] args) throws IOException {System.out.println(请输入你要复制的文件路径);Scanner scan new Scanner(System.in);String str scan.next();File file new File(str);if(!file.isFile()) {System.out.println(你输入的不是文件或者该文件不存在...);return;}System.out.println(请输入你要复制的路径);String end scan.next();File file2 new File(end);if(!file2.getParentFile().isDirectory()) {System.out.println(要复制到的文件路径不存在...);return;}byte[] b new byte[1024];try(InputStream inputStream new FileInputStream(str);OutputStream outputStream new FileOutputStream(end);) {while(true) {int n inputStream.read(b);if(n -1) {break;}outputStream.write(b,0, n);}}System.out.println(复制成功);}
} 扫描指定目录并找到名称或者内容中包含指定字符的所有普通文件不包含目录
import java.io.*;
import java.util.Scanner;public class Demo3 {public static void main(String[] args) throws IOException {System.out.println(请输入你要指定的目录);Scanner scan new Scanner(System.in);String str scan.next();File file new File(str);if(!file.isDirectory()) {System.out.println(你输入的不是目录或者该目录不存在);return;}System.out.println(请输入你要指定的内容);String token scan.next();scanDir(file, token);}public static void scanDir(File rootFile, String token) throws IOException {File[] list rootFile.listFiles();for(File file: list) {if(file.isDirectory()) {scanDir(file, token);} else {checkFile(file, token);}}}private static void checkFile(File file, String token) throws IOException {if(file.getName().contains(token)) {System.out.println(文件名包含指定关键字 file.getAbsolutePath());return;}Reader reader new FileReader(file);StringBuilder check new StringBuilder();char[] b new char[1024];while(true) {int n reader.read(b);if(n -1) {break;}check.append(b);}if(check.toString().contains(token)) {System.out.println(文件内容包含指定关键字 file.getAbsoluteFile());}}
}
文章转载自: http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.tsycr.cn.gov.cn.tsycr.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn