jsp 如何通过cmd判断 文件是否存在

发布网友 发布时间:2022-04-20 04:46

我来回答

1个回答

热心网友 时间:2023-09-03 21:49

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) { ; }
}
}
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com