13,100
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws IOException {
//String path = "G:" + File.separator + "test.txt";
String path = "c:/123.txt";
String s;
int count = 1;
int length;
SortedMap<Integer, String> map = new TreeMap<Integer, String>();
// 加载文件
File f = new File(path);
// 读取文件
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
try {
// 读入数据
s = reader.readLine();
while (s != null) {
length = s.length();
// 添加数据
map.put(length, s);
System.out.println("第" + count++ + "行" + s + "<<<长度" + length);
s = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("长度最短:" + map.get(map.firstKey()));
System.out.println("长度最长:" + map.get(map.lastKey()));
}
import java.io.File;
public class TestFlour {
public static void main(String[] args){
// String path = "G:" + File.separator + "down";
String path = "D:/eclipse";
// 加载目录
File file = new File(path);
list(file);
}
/**
* 遍历文件夹,显示文件属性
* @param file 文件夹或文件
*/
public static void list(File file){
// 测试此抽象路径名表示的文件或目录是否存在,不存在时结束
if(!file.exists()) {
return;
}
// 判断文件是否是目录
if(file.isDirectory()) {
// 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件
File[] files = file.listFiles();
// 循环遍历文件夹,
for(File f : files) {
list(f);
}
} else {
System.out.println("文件名: " + file.getName());
System.out.println("文件绝对路径:" + file.getAbsoluteFile());
System.out.println("文件大小:" + file.length());
System.out.println("访问时间:" + file.lastModified());
System.out.println("创建时间:");
System.out.println("");
}
}
}