File类,实现找出一个文件下的所有文件
代码如下:package myday20;
import java.io.File;
public class fileName {
public static void main(String[] args) throws Exception {
File f = new File("src/myday20");
test1(f);
System.out.println("------------------");
listMyFiles(f);
}
public static void test1(File file) {
if (file.isDirectory()) {
String[] s = file.list();
for (String string : s) {
System.out.println(string);
}
System.out.println("--------------------------");
int length = s.length;
File[] files = new File[length];
System.out.println(files.length);
System.out.println(files[0]);
// files[0] = new File(s[0]);
// System.out.println(files[0]);// 这里输出的为什么是文件名字而不是地址?
System.out.println("----------------------");
for (int i = 0; i < length - 1; i++) {
files[i] = new File(s[i]);
System.out.println(files[i]);
// System.out.println(files[i].canExecute());
// System.out.println(files[i].exists());
// System.out.println("------------------");
}
for (int i = 0; i < length - 1; i++) {
test1(files[i]);
}
} else {
System.out.println(file + " " + "不是目录");
}
}
public static void listMyFiles(File file) {
if (file.isDirectory()) {
File[] fs = file.listFiles();
for (File file2 : fs) {
System.out.println(file2);
System.out.println(file2.exists());
}
for (File file2 : fs) {
listMyFiles(file2);
}
}
}
}
我的包结构如图
我的问题是这个代码里的test1方法输出的结果为什么不对。test1的输出结果如下:
5
null
----------------------
directoryTest809756473462135397.txt
fileName.java
test
test.txt
directoryTest809756473462135397.txt 不是目录
fileName.java 不是目录
test 不是目录
test.txt 不是目录
这个test明明是个文件夹,按照我的本意,程序应该进入test文件夹继续读出test文件夹下有哪些文件才对。但这里却没有
listMyFiles的输出结果是正确的。输出结果如下:
src\myday20\directoryTest809756473462135397.txt
true
src\myday20\fileName.java
true
src\myday20\test
true
src\myday20\test.txt
true
src\myday20\text2.txt
true
src\myday20\test\red rose.mp3
true
src\myday20\test\test2
true
src\myday20\test\test2\测试.bmp
true
--------------------编程问答--------------------
package epssmsserver;--------------------编程问答-------------------- 我自己想了一下,但不知道这样解释对不对:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Create a folder "server" that is a database system environment, in which an
* authentication strategy is specified.
*
* @author gaowen
*/
public class EstablishDBEnvironment {
public static void main(String[] args) {
// Check if the folder "server" exists or not. If it does not exist, create it.
String path = System.getProperty("user.home") + System.getProperty("file.separator") + "server";
File folder = new File(path);
if (!folder.exists()) {
if (!folder.mkdir()) {
Logger.getLogger(EstablishDBEnvironment.class.getName()).log(Level.INFO, "create folder \"server\" failed");
return;
}
}
// check if the file "derby.properties" exists or not. If it exists, delete and then create it; else, create it directly.
File derbyProperties = new File(folder, "derby.properties");
if (derbyProperties.exists()) {
if (!derbyProperties.delete()) {
Logger.getLogger(EstablishDBEnvironment.class.getName()).log(Level.INFO, "\"derby.properties\" exists already and its deleting failed");
return;
}
}
try {
if (!derbyProperties.createNewFile()) {
Logger.getLogger(EstablishDBEnvironment.class.getName()).log(Level.INFO, "create file \"derby.properties\" failed");
return;
}
} catch (IOException ex) {
Logger.getLogger(EstablishDBEnvironment.class.getName()).log(Level.SEVERE, null, ex);
return;
}
// Write the derby properties to the "derby.properties" file.
try (FileWriter writer = new FileWriter(derbyProperties); BufferedWriter bw = new BufferedWriter(writer)) {
String[] contents = {"derby.connection.requireAuthentication=true", "derby.authentication.provider=BUILTIN", "derby.user.sa=12345"};
for (String line : contents) {
bw.write(line);
bw.newLine();
}
} catch (IOException ex) {
Logger.getLogger(EstablishDBEnvironment.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
--------------------编程问答-------------------- public void ReadF(String path){
File files = new File(path);
String[] flist = files.list();
if(files.isDirectory()){
for(int i=0;i<flist.length;i++){
//if(flist[i].indexOf(seaname.getText())!=-1 ){
File readfile = new File(path + "\\" + flist[i]);
System.out.println(readfile.getAbsolutePath());
//System.out.println(readfile.getPath().substring(readfile.getPath().lastIndexOf('\\'),readfile.getPath().lastIndexOf('.')));
if(!readfile.isDirectory()){
AnalysisStr(false,Modes,readfile.getName(),readfile.getPath());
}else if(readfile.isDirectory()){
AnalysisStr(true,Modes,readfile.getName(),readfile.getPath());
filei.setText(readfile.getPath()+"\r\n");
ReadF(path + "\\" + flist[i]);
/* if(readfile.getPath().indexOf(seaname.getText())!=-1){
rightpa.add(readfile.getPath());
}*/
}
}
}
}
递归函数,你看下吧,里面有我其他函数,你可以忽略,看主题结构, --------------------编程问答--------------------
public static void printFiles(File file){
if(file.isDirectory()){
String[] files=file.list();
for(int i=0;i<files.length;i++){
File newFile=new File(file.toString()+"\\"+files[i]);
printFiles(newFile);
}
}else {
System.out.println(file.getAbsolutePath());
}
}
补充:Java , Java SE