👉『关于作者』
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单。
👉『前提』
File类是java.io包中唯一代表磁盘文件本身的对象。
调用此类的 getParent() 方法可以获取抽象路径名的父 路径名,它由路径名前缀以及路径名名称序列中的每个名称(最后一个除外)组成。对于任何具有绝对抽象路径名的 File 对象,如果其绝对抽象路径名以某个目录的绝对路径名开头,那么该目录的绝对路径名是该 File 对象的祖先。例如,抽象路径名 “/usr” 表示的目录是路径名 “/usr/local/bin” 所表示目录的一个祖先。
👉『实践过程』
File 类的实例是不可变的;也就是说,一旦创建,File 对象表示的抽象路径名将永不改变。
File类定义了一些与平台无关的方法来操作文件,可以通过调用file类中的方法实现创建、删除、重命名文件等操作。file类的对象主要用来获取文件本身的一些信息,如文件所在的目录、文件的长度文件读写权限等。数据流可以将数据写入文件,文件也是数据流最常用的数据媒体
😜File类的常用方法
File类中包含了很多获得文件或文件夹属性的方法,使用起来比较方便,下面将常见的方法介绍如下:
createNewFile方法
public boolean createNewFile() throws IOException
该方法的作用是创建指定的文件。该方法只能用于创建文件,不能用于创建文件夹,且文件路径中包含的文件夹必须存在。
delect方法
public boolean delete()
该方法的作用是删除当前文件或文件夹。如果删除的是文件夹,则该文件夹必须为空。如果需要删除一个非空的文件夹,则需要首先删除该文件夹内部的每个文件和文件夹,然后在可以删除,这个需要书写一定的逻辑代码实现。
exists方法
public boolean exists()
该方法的作用是判断当前文件或文件夹是否存在。
getAbsolutePath方法
public String getAbsolutePath()
该方法的作用是获得当前文件或文件夹的绝对路径。例如c:\test\1.t则返回c:\test\1.t.
getName方法
public String getName()
该方法的作用是获得当前文件或文件夹的名称。例如c:\test\1.t,则返回1.t.
getParent方法
public String getParent()
该方法的作用是获得当前路径中的父路径。例如c:\test\1.t则返回c:\test.
isDirectory方法
public boolean isDirectory()
该方法的作用是判断当前File对象是否是目录。
isFile方法
public boolean isFile()
该方法的作用是判断当前File对象是否是文件。
length方法
public long length()
该方法的作用是返回文件存储时占用的字节数。该数值获得的是文件的实际大小,而不是文件在存储时占用的空间数。
list方法
public String[] list()// 并不包含其属性。
该方法的作用是返回当前文件夹下所有的文件名和文件夹名称。说明,该名称不是绝对路径。
listFiles方法
public File[] listFiles()
该方法的作用是返回当前文件夹下所有的文件对象。包含其属性。
mkdir方法
public boolean mkdir()
该方法的作用是创建当前文件文件夹,而不创建该路径中的其它文件夹。假设d盘下只有一个test文件夹,则创建d:\test\abc文件夹则成功,如果创建d:\a\b文件夹则创建失败,因为该路径中d:\a文件夹不存在。如果创建成功则返回true,否则返回false.
mkdirs方法
public boolean mkdirs()
该方法的作用是创建文件夹,如果当前路径中包含的父目录不存在时,也会自动根据需要创建。
renameTo方法
public boolean renameTo(File dest)
该方法的作用是修改文件名。在修改文件名时不能改变文件路径,如果该路径下已有该文件,则会修改失败。
setReadOnly方法
public boolean setReadOnly()
该方法的作用是设置当前文件或文件夹为只读。
I/O类使用
由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择。因为文件是一种常见的数据源,而且读写文件
也是程序员进行IO编程的一个基本能力。
😜FileWritter写入文件
FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函数的第二个参数,它会保留现有的内容,并追加新内容在文件的末尾。
- 替换所有现有的内容与新的内容。new FileWriter(file);
- 保留现有的内容和附加在该文件的末尾的新内容。new FileWriter(file,true);
一个文本文件,命名为“javaio-appendfile.txt”,并包含以下内容。
ABC Hello追加新内容 new FileWriter(file,true)
package com.yiibai.file;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class AppendToFileExample
{
public static void main( String[] args )
{
try{
String data = " This content will append to the end of the file";
File file =new File("javaio-appendfile.txt");
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
System.out.println("Done");
}catch(IOException e){
e.printStackTrace();
}
}
}
结果 现在,文本文件“javaio-appendfile.txt”内容更新如下:
ABC Hello This content will append to the end of the file
😜BufferedWriter写入文件
缓冲字符(BufferedWriter )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。
package com.yiibai.iofile;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
😜FileOutputStream写入文件
文件输出流是一种用于处理原始二进制数据的字节流类。为了将数据写入到文件中,必须将数据转换为字节,并保存到文件。请参阅下面的完整的例子。
package com.yiibai.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//更新的JDK7例如,使用新的“尝试资源关闭”的方法来轻松处理文件。
package com.yiibai.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
File file = new File("c:/newfile.txt");
String content = "This is the text content";
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
👉『其他』
📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢欢迎点赞👍收藏🌟留言📝