文件读写
读文件(行)
private void readFile(File file) throws IOException {
FileInputStream stream = null;
stream = new FileInputStream(file);
DataInputStream sysin = new DataInputStream(stream);
String line = null;
while ((line = sysin.readLine()) != null) {
if (line.trim().equals("")) {
continue;
}
System.out.println(line);
}
}
写文件
public void write(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, false);
String data = "aa\nbb";
out.write(data.toString().getBytes("utf-8"));
ParsePclntResult obj = new ParsePclntResult();
}
public void write(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, false);
String data = "aa\nbb";
out.write(data.toString().getBytes("utf-8"));
ParsePclntResult obj = new ParsePclntResult();
}
递归删除指定文件夹下的文件(遍历文件)
private void cleanupRecursive(File file) {
try {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
cleanupRecursive(child);
}
if (file.listFiles().length == 0) {
file.delete();
}
} else {
if (file.getName().endsWith(".xls")) {
file.delete();
}
}
} catch (Exception e) {
System.out.println(e.getMessage() + "\nFile:" + file.getAbsolutePath());
}
}
private void cleanupRecursive(File file) {
try {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
cleanupRecursive(child);
}
if (file.listFiles().length == 0) {
file.delete();
}
} else {
if (file.getName().endsWith(".xls")) {
file.delete();
}
}
} catch (Exception e) {
System.out.println(e.getMessage() + "\nFile:" + file.getAbsolutePath());
}
}