package xiyin1979.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* 移动文本文件,按行读取后移动,如果不是文本文件,则移动后的文件不能打开或运行
* @author xiyin1979
*/
public class MovieFile {
public static void main(String[] args) {
MovieFile.moveFile("C:\\city.xml", "D:\\");
}
/**
* 将指定文件移动到目标文件夹,如果目标文件夹不存在,则先创建
* @param sourcePath 要移动的文件路径
* @param targetPath 目标文件夹路径
*/
private static void moveFile(String sourcePath, String targetPath) {
BufferedReader bufferRead = null;
BufferedWriter newFileBW = null;
FileOutputStream fileOutputStream = null;
boolean copyOK = false;
File sourceFile = new File(sourcePath);
try {
bufferRead = new BufferedReader(new InputStreamReader(
new FileInputStream(sourceFile), "UTF-8"));
File targetFolder = new File(targetPath);
if (!targetFolder.exists()) {
targetFolder.mkdirs();
}
File copyFile = new File(targetPath + File.separator+ sourceFile.getName());
fileOutputStream = new FileOutputStream(copyFile);
newFileBW = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
String line = "";
while ((line = bufferRead.readLine()) != null) {
newFileBW.write(line + "\r\n");
}
newFileBW.flush();
copyOK = true;
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (newFileBW != null) {
newFileBW.close();
}
if (bufferRead != null) {
bufferRead.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
if (copyOK) {
boolean deleteSuccess = sourceFile.delete();
System.out.println("日志文件:" + sourcePath + "成功移动到备份文件夹");
} else {
System.out.println("日志文件:" + sourcePath + "解析后,未成功移动到备份文件夹");
}
}
}
}
package xiyin1979.io;
import java.io.File;
/**
* 移动文件2
* @author yixin1979
*/
public class MovieFile2 {
public static void main(String[] args) {
// 文件原地址
File oldFile = new File("d:\\XDICT.rar");
// 文件新(目标)地址
String newPath = "c:\\";
// new一个新文件夹
File fnewpath = new File(newPath);
// 判断文件夹是否存在
if(!fnewpath.exists())
fnewpath.mkdirs();
// 将文件移到新文件里
File fnew = new File(newPath +oldFile.getName());
oldFile.renameTo(fnew);
}
}
以下内容摘自msdn
//如何复制文件和目录。
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
//如何移动文件和目录。
// Simple synchronous file move operations with no user interface.
public class SimpleFileMove
{
static void Main()
{
string sourceFile = @"C:\Users\Public\public\test.txt";
string destinationFile = @"C:\Users\Public\private\test.txt";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
// To move an entire directory. To programmatically modify or combine
// path strings, use the System.IO.Path class.
System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
}
}
//删除文件或目录
// Simple synchronous file deletion operations with no user interface.
// To run this sample, create the following files on your drive:
// C:\Users\Public\DeleteTest\test1.txt
// C:\Users\Public\DeleteTest\test2.txt
// C:\Users\Public\DeleteTest\SubDir\test2.txt
public class SimpleFileDelete
{
static void Main()
{
// Delete a file by using File class static method...
if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
{
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
try
{
System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
// ...or by using FileInfo instance method.
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\Users\Public\DeleteTest\test2.txt");
try
{
fi.Delete();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
// Delete a directory. Must be writable or empty.
try
{
System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
// Delete a directory and all subdirectories with Directory static method...
if(System.IO.Directory.Exists(@"C:\Users\Public\DeleteTest"))
{
try
{
System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
// ...or with DirectoryInfo instance method.
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\Users\Public\public");
// Delete this dir and all subdirs.
try
{
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
}