如果此抽象路径名并不表示一个目录,则此方法将返回 null.否则,为目录中的每个文件或目录返回一个字符串数组。表示目录本身及其父目录的名称不包括在结果中。每个字符串是一个文件名,而不是一条完整路径。
SUN写这个方法的时候就规定了可以返回null,是防止路径不是目录。
而用files.length != 0,是在files不为null得条件下继续判断,表明此时目录下面至少有一个文件或者子目录存在。
JAVA里面&&判断是截断的,即就是如果files != null为假,就不会判断后面的了呢。
public static void copy(String from_name, String to_name)throws IOException
{
File from_file = new File(from_name) // Get File objects from Strings
File to_file = new File(to_name)
String parent = to_file.getParent() // The destination directory
if (parent == null) // If none, use the current directory
parent = System.getProperty("user.dir")
File dir = new File(parent) // Convert it to a file.
dir.mkdir()
// If we've gotten this far, then everything is okay.
// So we copy the file, a buffer of bytes at a time.
FileInputStream from = null // Stream to read from source
FileOutputStream to = null // Stream to write to destination
try {
from = new FileInputStream(from_file) // Create input stream
to = new FileOutputStream(to_file)// Create output stream
byte[] buffer = new byte[4096]// To hold file contents
int bytes_read// How many bytes in buffer
// Read a chunk of bytes into the buffer, then write them out,
// looping until we reach the end of the file (when read() returns
// -1). Note the combination of assignment and comparison in this
// while loop. This is a common I/O programming idiom.
while((bytes_read = from.read(buffer)) != -1) // Read until EOF
to.write(buffer, 0, bytes_read) // write
}
finally {
if (from != null) try { from.close()} catch (IOException e) { }
if (to != null) try { to.close()} catch (IOException e) { }
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)