如何在java中备份文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22135156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to backup file in java?
提问by user3236502
In reality its just making a copy of a text.txt file. I know how to use file chooser to choose the file but that is as far as my knowledge really goes.
实际上它只是制作一个 text.txt 文件的副本。我知道如何使用文件选择器来选择文件,但据我所知,这是真的。
I can do this:
我可以做这个:
public BasicFile()
{
JFileChooser choose = new JFileChooser(".");
int status = choose.showOpenDialog(null);
try
{
if (status != JFileChooser.APPROVE_OPTION) throw new IOException();
f = choose.getSelectedFile();
if (!f.exists()) throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
display(1, e.toString(), "File not found ....");
}
catch(IOException e)
{
display(1, e.toString(), "Approve option was not selected");
}
}
采纳答案by java-love
Path object is perfect for copying files,
Try this code to copy a file,
Path 对象非常适合复制文件,
试试这个代码复制文件,
Path source = Paths.get("c:\blabla.txt");
Path target = Paths.get("c:\blabla2.txt");
try {
Files.copy(source, target);
} catch (IOException e1) {
e1.printStackTrace();
}
回答by MadProgrammer
Start by taking a look at Basic I/O, which explains the basics of Input/OutputStreams
and Reader
s and Writer
s, which are used to read/write bytes of data from a source to a destination.
首先看一下Basic I/O,它解释了Input/OutputStreams
and Reader
s 和Writer
s的基础知识,它们用于从源读取/写入数据字节到目标。
If you're using Java 7 or over, you should also take a look at Copying a File or Directorywhich is part of newer Files
and Paths
API, which you can find more information about at File I/O (Featuring NIO.2)
如果您使用的是Java 7或以上,你也应该看看复制文件或目录是较新的组成部分Files
和Paths
API,你可以找到有关的详细信息的文件I / O(NIO.2特色)
回答by Randula Koralage
If you have to backup a whole folder, you can use this code
如果必须备份整个文件夹,可以使用此代码
public class BackUpFolder {
public void copy(File sourceLocation, File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
copyDirectory(sourceLocation, targetLocation);
} else {
copyFile(sourceLocation, targetLocation);
}
}
private void copyDirectory(File source, File target) throws IOException {
if (!target.exists()) {
target.mkdir();
}
for (String f : source.list()) {
copy(new File(source, f), new File(target, f));
}
}
private void copyFile(File source, File target) throws IOException {
try (
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
public static void main(String[] args) {
try {
BackUpFolder backUpFolder = new BackUpFolder();
String location = "./src/edu/abc/locationFiles/daofile"; //File path you are getting from file chooser
String target = "./src"; //target place you want to patse
File locFile = new File(location);
File tarFile = new File(target);
backUpFolder.copyDirectory(locFile, tarFile);
} catch (IOException ex) {
Logger.getLogger(BackUpFolder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}