JAVA文件操作
1.1创建文件
File类可以表示一个文件,还可以表示一个目录(Directory),所以我们可以在程序中用File 类的对象可以表示一个文件 或者 目录
当创建了 File 对象之后,我们可以利用该对象来对文件或者目录进行书属性修改:例如:文件的名称,修改日期的日期等等
File 类的对象 还不能直接对文件进行读写操作,只能修改文件的属性
public class FileDemo {
public static void main(String[] args) {
//相对路径,如果没有前面的src,就在当前目录创建文件
File f1=new File("src\\FileDemo.txt");
if(f1.exists()) {
System.out.println("文件已经存在");
}else {
try {
f1.createNewFile();
System.out.println("文件创建成功");
} catch (Exception e) {
}
}
System.out.println("文件已经存在:"+f1.exists());
System.out.println("文件的名字:"+f1.getName());
System.out.println("文件的路径:"+f1.getPath());
System.out.println("文件的绝对路径:"+f1.getAbsolutePath());
System.out.println("是目录吗:"+f1.isDirectory());
System.out.println("文件大小:"+f1.length());
}
}
1.2 Stream流(字节流)
流是指一连串流动的数据信号,通过FiFO(先进先出) 的方式接收和发送数据
-
数据流又分为输入流和输出流
-
输入输出流又分为字节流和字符流
字节流:以字节为基本单位 , 在 java.io包中,大部分操作继承InputStream(输入字节流)类和OutputStream(输出字节流)类 字符流:两个字节为基本单位,专门处理字符串和文本,对于字符流进行操作的类主要是Reader(读取流)类和 Writer(写入流)类。
1.2.1 FileInputStream输入流
- 继承于InputStream类,这是一个文件输入流,进行文件读操作的最基本的类
- 作用是将文件中的数据输入到内存中,我们可以用它来读文件操作
- 由于字节流的缘故,因此无法读取中文字符
向文件中添加数据
fileInputStream代码示例
public class InputStreamDemo {
public static void main(String[] args) {
try {
File file = new File("src/FileDemo.txt");
FileInputStream input = new FileInputStream(file);
int read;
System.out.print("文件中的字符串为:");
while((read = input.read())!=-1){
System.out.print((char) read);
}
} catch (IOException e) {
throw new RuntimeException();
}
}
}
运行结果
1.2.2 FileOutPutStream 出流
- FileOutputStream类称为文件输出流,继承于OutputStream类,是文件的基本读写的一个类
- 它的作用和上面读文件恰恰相反,将内存中的数据输出到文件中,所以我们可以用这个类来进行写文件的操作
FileOutputStream代码示例
public class OutputStreamDemo {
public static void main(String[] args) {
File file = new File("src/FileDemo.txt");
try {
// TODO: FIleOutPutStream中的参数说明,参数一为必须参数,参数二是默认为false--为覆盖模式,将会覆盖文件内容,反之为追加
FileOutputStream Output = new FileOutputStream(file,true);
Output.write("Hello World".getBytes());
FileInputStream fileInputStream = new FileInputStream(file);
int read;
System.out.print("文件内容为:");
while((read = fileInputStream.read())!=-1){
System.out.print((char)read);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
运行结果
1.3 Stream流(字符流)
字符串的读取,以及解决中文字符无法读入的问题
1.3.1 BufferedReader输入流
BufferedReader代码示例
public class bufferWriteDemo {
public static void main(String[] args) {
File file = new File("src/FileDemo.txt");
try {
BufferedReader Reader = new BufferedReader(new FileReader(file));
String str;
System.out.println("读取文件内容:");
// TODO:readline()是一行一行的读取
while((str = Reader.readLine()) != null){
System.out.println(str);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
运行结果
1.3.2 FileWrite输出流
代码示例
public class FileWriteDemo {
public static void main(String[] args) {
File file = new File("src/FileDemo.txt");
try {
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.write("hello world 仓颉");
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
运行结果
总结
1.因为上次笔试题有文件操作,而自己很久没有写过文件操作了,而没有完全A出来,狠狠打自己脸🆒
2.FileInputStream类 和 FileOutputStream类总是成对出现的,一个用作输入流,另一个自然是输出流
结果
[外链图片转存中…(img-krsqfAV2-1720870103694)]
总结
1.因为上次笔试题有文件操作,而自己很久没有写过文件操作了,而没有完全A出来,狠狠打自己脸🆒
2.FileInputStream类 和 FileOutputStream类总是成对出现的,一个用作输入流,另一个自然是输出流
我们在输入流中使用 byte(字节)数组 来存储我们的数据,因此我们不必考虑数据格式,所以说这两种操作的效率会比较高