Java文件操作编程
2023-04-18 16:11:53 时间
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/220
package com.joshua317;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
renameFile();
}
/**
* 当前目录下,创建文件
*/
public static void createFile()
{
String dir = System.getProperty("user.dir");
String filePath = dir + "/info.log";
try {
File file = new File(filePath);
if (file.createNewFile()) {
System.out.println("创建文件成功!");
} else {
System.out.println("创建失败,该文件已经存在!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 当前目录下,写入文件
*/
public static void writeFile()
{
String dir = System.getProperty("user.dir");
String filePath = dir + "/info1.log";
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("joshua317
");
bufferedWriter.write("joshua318
");
bufferedWriter.close();
System.out.println("写入文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 当前目录下,追加文件内容
*/
public static void writeAppendFile()
{
String dir = System.getProperty("user.dir");
String filePath = dir + "/info2.log";
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));
bufferedWriter.write("joshua317
");
bufferedWriter.close();
System.out.println("追加文件文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将文件内容复制到另一个文件
*/
public static void copyFile()
{
String dir = System.getProperty("user.dir");
String filePath1 = dir + "/info2.log";
String filePath2 = dir + "/info3.log";
try {
InputStream in = new FileInputStream(new File(filePath1));
OutputStream out = new FileOutputStream(new File(filePath2));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
BufferedReader fileReader = new BufferedReader(new FileReader(filePath2));
String str;
while ((str = fileReader.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 当前目录下,读取文件内容
*/
public static void readFile()
{
String dir = System.getProperty("user.dir");
String filePath = dir + "/info2.log";
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 当前目录下,获取文件大小,1KB=1024字节
*/
public static void getFileSize() {
String dir = System.getProperty("user.dir");
String filePath = dir + "/info2.log";
File file = new File(filePath);
if (!file.isFile() || !file.exists()) {
System.out.println("文件不存在");
} else {
System.out.println(file.length());
}
}
/**
* 当前目录下,获取文件最后的修改时间
*/
public static void getLastModified() {
String dir = System.getProperty("user.dir");
String filePath = dir + "/info2.log";
File file = new File(filePath);
//文件最后的修改时间
long lastModified = file.lastModified();
//格式化打印日期
Date date = new Date(lastModified);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(date);
System.out.println(format);
}
/**
* 当前目录下,获取文件最后的修改时间
*/
public static void fileExists() {
String dir = System.getProperty("user.dir");
String filePath = dir + "/info4.log";
File file = new File(filePath);
System.out.println(file.exists());
}
/**
* 当前目录下,重命名文件
*/
public static void renameFile() {
String dir = System.getProperty("user.dir");
String filePathOld = dir + "/info2.log";
String filePathNew = dir + "/info5.log";
File fileOld = new File(filePathOld);
File fileNew = new File(filePathNew);
// 确保新的文件名不存在
try {
if (fileNew.exists()) {
throw new IOException("file exists");
}
if(fileOld.renameTo(fileNew)) {
System.out.println("已重命名");
} else {
System.out.println("重命名失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/220
相关文章
- Jease 2.6发布 Java开源内容框架
- JVM调优总结:反思
- JVM调优总结:调优方法
- JVM调优总结:新一代的垃圾回收算法
- JVM调优总结:典型配置举例
- JVM调优总结:分代垃圾回收详述
- JVM调优总结:垃圾回收面临的问题
- JVM调优总结:基本垃圾回收算法
- JVM调优总结:一些概念
- 用Java GUI编写的画板程序
- Java的动态绑定机制
- jOOQ 2.0.2发布 Java的ORM框架
- Java中带复选框的树的实现和应用
- Java网络编程菜鸟进阶:TCP和套接字入门
- 甲骨文与谷歌专利权之争定于今年三月开审
- Java调用C/C++编写的第三方dll动态链接库
- 集成开发环境 NetBeans IDE 7.1正式版发布
- kangle 2.7.5紧急发布 防hash碰撞攻击
- 东方通技术引领模式为国产软件“争权”
- UML中关联,组合与聚合等关系的辨析