- 列出一个文件夹下所有文件名
- 创建新文件
- 生成txt文件,写入指定字符串
- 使用BufferedWriter
- 使用PrintWriter
- 按行读文件
- 重命名
- 移动文件
- 依赖
- 移动文件夹,相当于覆盖
- 移动文件夹,某个文件夹,移动到另一个文件夹内
- 移动文件,把文件移动到某个文件夹下
- 移动到文件夹,自动识别操作文件还是文件夹
列出一个文件夹下所有文件名
public void listFileOnDir(String fileDirPath) {
File fileDir = new File(fileDirPath);
String[] filesOnDir = fileDir.list();
if (filesOnDir != null) {
log.info("listFileOnDir, filesOnDir:{}", filesOnDir);
for (String filePath : filesOnDir) {
try {
String fileFullPath = fileDir + "/" + filePath;
File singleFile = new File(fileFullPath);
log.info("listFileOnDir, singleFile: {}", singleFile);
} catch (Exception e) {
log.error("listFileOnDir error", e);
}
}
}
}
创建新文件
public void createFile(String path, String filename) throws IOException {
File file=new File(path+"/"+filename);
if(!file.exists())
file.createNewFile();
}
生成txt文件,写入指定字符串
使用BufferedWriter
public void writeContentToText(String fileContent, String filePath, String fileName) throws IOException {
// 生成的文件路径
String fullPathName = filePath + "/" + fileName;
File file = new File(fullPathName);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
// write 解决中文乱码问题
// FileWriter fw = new FileWriter(file, true);
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fileContent);
bw.flush();
bw.close();
fw.close();
}
使用PrintWriter
public void writeContentToTextOther(String fileContent, String fileFullPath, String fileName) {
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File sendFile = new File(fileFullPath);
if (!sendFile.exists()) {
sendFile.createNewFile();// 创建目标文件
}
fos = new FileOutputStream(sendFile);
pw = new PrintWriter(fos);
pw.write(fileContent.toCharArray());
pw.flush();
fos.close();
} catch (IOException e) {
log.error("writer file error", e);
} finally {
if (pw != null) {
pw.close();
}
}
}
按行读文件
public void readFileOnLine(String fullFilePath) {
try {
File file = new File(fullFilePath);
InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader bf = new BufferedReader(inputReader);
// 按行读取字符串
String lineContent;
while ((lineContent = bf.readLine()) != null) {
// TODO:按行解析读取的文件,测试是不是不同编码的文件都能解析
log.info("readFileOnLine, lineContent:{}", lineContent);
}
bf.close();
inputReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream读取出文件是字节流,InputStreamReader可以将字节流转换为字符流
new InputStreamReader时可指定编码:
File file = new File(fullFilePath);
InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), "utf-8");
重命名
public void renameFile(String oldFullFilePath, String newFullFilePath) {
File startFile = new File(oldFullFilePath);
File endFile = new File(newFullFilePath);
if (startFile.renameTo(endFile)) {
log.info("文件移动成功!目标路径: {}", endFile.getAbsolutePath());
} else {
log.info("文件移动失败!起始路径: {}", startFile.getAbsolutePath());
}
}
移动文件
移动文件一系列操作主要用 org.apache.commons.io下的包
依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
public void moveFile(File singleFile, String changeDir, String filePathOnDir) {
String newGenFilePath = changeDir + "/" + filePathOnDir;
File backedGeneratedCardFile = new File(newGenFilePath);
try {
FileUtils.moveFile(singleFile, backedGeneratedCardFile);
} catch (Exception e) {
log.error("moveFile error", e);
}
}
移动文件夹,相当于覆盖
public void moveDirectory(String oldDirPath, String newDirPath) {
File oldDir = new File(oldDirPath);
File newDir = new File(newDirPath);
try {
FileUtils.moveDirectory(oldDir, newDir);
} catch (Exception e) {
log.error("moveDirectory error", e);
}
}
移动文件夹,某个文件夹,移动到另一个文件夹内
public void moveDirToDirectory(String oldDirPath, String newDirPath) {
File oldDir = new File(oldDirPath);
File newDir = new File(newDirPath);
try {
FileUtils.moveDirectoryToDirectory(oldDir, newDir, true);
} catch (Exception e) {
log.error("moveDirToDirectory error", e);
}
}
移动文件,把文件移动到某个文件夹下
public void moveFileToDirectory(String srcFilePath, String newDirPath) {
File oldFile = new File(srcFilePath);
File newDir = new File(newDirPath);
try {
FileUtils.moveFileToDirectory(oldFile, newDir, true);
} catch (Exception e) {
log.error("moveFileToDirectory error", e);
}
}
移动到文件夹,自动识别操作文件还是文件夹
public void moveToDirectory(String srcPath, String newDirPath) {
File srcInfo = new File(srcPath);
File newDir = new File(newDirPath);
try {
FileUtils.moveToDirectory(srcInfo, newDir, true);
} catch (Exception e) {
log.error("moveFileToDirectory error", e);
}
}