java - From File[] files to directory -
i have problem code:
public class files { public static void main(string[] args) throws ioexception { // filter files aaa.txt , bbb.txt another's file f = new file("d:\\dir"); // current directory file f1 = new file("d:\\dir1\\"); filenamefilter textfilter = new filenamefilter() { public boolean accept(file dir, string name) { if (name.startswith("a") && name.endswith(".txt")) { //system.out.println(name); return true; } else if (name.startswith("b") && name.endswith(".txt")) { //system.out.println(name); return true; } else { //system.out.println(name); return false; } } }; file[] files = f.listfiles(textfilter); (file file : files) { if (file.getname().startswith("a") ) { //here save file d:\\folder1\\ } } } }
how can save files specific name in example aaa.txt folder1 , bbb.txt folder 2. examples
from files class java 7:
use move(path source, path target, copyoption... options)
import static java.nio.file.standardcopyoption.*; import java.nio.file.filesystem; import java.nio.file.path; import java.nio.file.files; ... (file file : files) { if (file.getname().startswith("a") ) { //here save file d:\\folder1\\ // convert file path object use topath() method. path targetfilepath = filesystems.getdefault().getpath("d:\\folder1\\").resolve(file.getfilename()) files.move(file.topath(), targetfilepath , replace_existing); } }
Comments
Post a Comment