1、写入文件
write(String)
public class FirstString {
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outfilename")); //使用字符缓冲写入流,最重要的是底层写入流,也就是FileWriter/OutputWriter/StringWriter...
bufferedWriter.write("程序外写文件");
bufferedWriter.close();
System.out.println("文件创建成功!");
}
}
控制台
文件创建成功!
系统文件树
2、读取文件内容
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("outfilename")); //使用字符缓冲读取流,最重要的是底层读取流,也就是FileReader/InputReader/StringReader...
String str;
while((str = bufferedReader.readLine())!=null){
System.out.println("str = " + str);
}
}
输出结果
str = 程序外写文件
str = 第二行信息
3、删除文件
public static void main(String[] args) throws IOException {
File file = new File("outfilename"); //创建File对象
if (file.delete()){
System.out.println("删除成功!");
}
}
控制台
删除成功!
4、Copy文件内容至另一个文件
步骤流程:
InputStream in = new FileInputStream("outfilename"); //获取文件读取流
OutStream out = new FileOutputStream("copyfile"); //获取文件输入流
byte[] bytes = new byte[1024]; //设置缓冲字节区
int len;
while((len = in.read(bytes)) > 0){ //读入缓冲区
out.write(bytes,0,len);
//write(byte[],index,len);
}
in.close();
out.close();
BUfferReader br = new BufferReader(new FileReader("copyfile"));
String str;
while((str = br.readLine()) != null){
System.out.println(str);
}
输出结果
写入的东西,请不要再继续写了,我要copy了
5、文件追加数据
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("copyFile",true));
//使用的是Writer(path,flag),flag = true——追加尾部文本,flag=false——文件初始位置写入
bufferedWriter.write("我不是什么都会");
bufferedWriter.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader("copyFile"));
String str;
while ((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
bufferedReader.close();
}
输出结果
写入的东西,请不要再继续写了,我要copy了我不是什么都会
6、临时文件创建以及使用
public static void main(String[] args) throws IOException {
File temp = File.createTempFile("pattern",".tmp"); //创建临时文件,createTempFile(String filename,String suffix,File dictory);
temp.deleteOnExit(); //程序结束即删除
BufferedWriter out = new BufferedWriter(new FileWriter(temp,true));
out.write("这里是一个temp");
out.write("\ntempFile#2lines");
out.close();
System.out.println("临时文件创建成功");
out = new BufferedWriter(new FileWriter(temp,true));
out.write("\notherWord");
out.close();
BufferedReader in = new BufferedReader(new FileReader(temp));
String str;
while ((str = in.readLine()) != null){
System.out.println(str);
}
}
输出结果
临时文件创建成功
这里是一个temp
tempFile#2lines
otherWord
7、修改文件最后修改日期
lastModified()
setLastModified()
public static void main(String[] args) throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
File file = new File("./src/fileCollect/newFileText");
System.out.println(sdf.format(file.lastModified()));
System.out.println(file.setLastModified(System.currentTimeMillis()));
System.out.println(sdf.format(file.lastModified()));
}
2023-02-21 02:21:14
true
2023-02-21 02:25:23
8、获取文件大小
length()
public static void main(String[] args) throws IOException {
File file = new File("./src/fileCollect/newFileText");
System.out.println(file.length()+" Byte"); //file.length()获取的是字节数,我们在文件属性中也能看到大小为xx字节
}
209 Byte
9、文件重命名
renameTo(File)
public static void main(String[] args) throws IOException {
File file = new File("./src/fileCollect/newFileText");
file.renameTo(new File("./src/fileCollect/files"));
}
效果:
10、文件属性只读,可写属性设置
setReadOnly()
canWrite()
public static void main(String[] args) throws IOException {
File file = new File("./src/fileCollect/files");
System.out.println(file.setReadOnly());
System.out.println(file.canWrite());
}
效果图
11、创建文件
createNewFile()
public static void main(String[] args) throws IOException {
File file = new File("./src/fileCollect/createNewFile");
if (!file.exists()){
if (file.createNewFile()){
System.out.println("文件创建成功");
}
}else {
System.out.println("文件创建失败");
}
}