81,091
社区成员
发帖
与我相关
我的任务
分享
private void scan(File f) {
if (f.isDirectory()) {
buf.append(String.format("%s{\n%s\"name\" : \"%s\",\n%s\"children\" : [\n", space(kai), space(++kai), f.getName(), space(kai)));
kai++;
for (File _f : f.listFiles()) scan(_f);
buf.delete(buf.length() - 2, buf.length());
buf.append(String.format("\n%s]\n%s},\n", space(--kai), space(--kai)));
} else
buf.append(String.format("%s{\n%s\"name\" : \"%s\"\n%s},\n", space(kai), space(++kai), f.getName(), space(--kai)));
}
public
class Demo10
{
public static void main(String[] args)
{
Demo10 d = new Demo10();
d.scan("E:\\workspace\\Demo");
d.println();
}
public void scan(String path) {
File f = new File(path);
if (f.isDirectory())
{
scan(new File(path));
buf.delete(buf.length() - 2, buf.length());
}
else System.out.format("{\"name\" : \"%s\"}", f.getName());
}
StringBuilder buf;
int kai = 0;
public Demo10() {
buf = new StringBuilder();
}
public void println() {
System.out.println(buf);
}
private void scan(File f) {
if (f.isDirectory())
{
buf.append(space(kai)).append("{\n").append(space(++kai)).append("\"name\" : \"").append(f.getName()).append("\",\n").append(space(kai)).append("\"children\" : [\n");
kai++;
Arrays.asList(f.listFiles()).forEach(this::scan);
buf.delete(buf.length() - 2, buf.length());
buf.append("\n").append(space(--kai)).append("]\n").append(space(--kai)).append("},\n");
}
else buf.append(space(kai)).append("{\n").append(space(++kai)).append("\"name\" : \"").append(f.getName()).append("\"\n").append(space(--kai)).append("},\n");
}
public String space(int kai) {
if (kai <= 0)
{
return "";
}
char[] cs = new char[kai<<1];
Arrays.fill(cs, ' ');
return new String(cs, 0, cs.length);
}
}
输出大概是这样.
{
"name" : "Demo",
"children" : [
{
"name" : "src",
"children" : [
{
"name" : "com",
"children" : [
{
"name" : "cua",
"children" : [
{
"name" : "Demo10.java"
},
{
"name" : "WinningBean.java"
}
]
}
]
},
{
"name" : "log4j.properties"
},
{
"name" : "test file.txt"
}
]
}
]
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadFromFile {
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName, String writefileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
appendMethodB(writefileName, tempString + "\r\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 递归讀取目录下的所有文件及子目录下所有文件
*/
public static void findDir(String patn, String writefileName) {
File dir = new File(patn);
if (dir.isDirectory()) {
System.out.println("获取文件夹下的集合");
String[] children = dir.list();
// 递归读取目录中的子目录下
for (int i = 0; i < children.length; i++) {
String chidPaath = patn + '/' + children[i];
File child = new File(chidPaath);
if (child.isDirectory()) {
System.out.println("文件夹");
findDir(chidPaath, writefileName);
} else {
System.out.println("文件-----讀取" + child.getName());
readFileByLines(chidPaath, writefileName);
}
}
} else {
readFileByLines(patn, writefileName);
}
}
/**
* B方法追加文件:使用FileWriter
*/
public static void appendMethodB(String fileName, String content) {
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "E:\\天表";
String writeFile = "D:\\JavaCode1.text";
File file = new File(writeFile);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ReadFromFile.findDir(path, writeFile);
}
}