Created
July 4, 2020 18:35
-
-
Save kryvoboker/dc59817ca47448dccc3ed9460bbdc3dd to your computer and use it in GitHub Desktop.
IO (потоки ввода - вывода)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class CopyFiles { | |
public static void copyFile (File in, File out) throws IOException { | |
byte[] buffer = new byte[1024 * 1024]; | |
int readByte = 0; | |
try (FileInputStream fis = new FileInputStream(in); | |
FileOutputStream fos = new FileOutputStream(out)) { | |
for (; (readByte = fis.read(buffer)) > 0;) { | |
fos.write(buffer, 0, readByte); | |
} | |
} catch (IOException e) { | |
throw e; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
MyFileFilter mf = new MyFileFilter("doc", "docx"); | |
File folderOne = new File("E://Документація"); | |
File folderTwo = new File("Files"); | |
MyFileFilter.createFile(folderOne, folderTwo, mf); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileFilter; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class MyFileFilter implements FileFilter { | |
private String[] arr; | |
public MyFileFilter(String... arr) { | |
super(); | |
this.arr = arr; | |
} | |
private boolean check(String text) { | |
for (String stringExt : arr) { | |
if (stringExt.equals(text)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public boolean accept(File pathname) { | |
int pointerIndex = pathname.getName().lastIndexOf("."); | |
if (pointerIndex == -1) { | |
return false; | |
} | |
String text = pathname.getName().substring(pointerIndex + 1); | |
return check(text); | |
} | |
public static void createFile(File folderOne, File folderTwo, MyFileFilter mf) throws IOException { | |
folderTwo.mkdirs(); | |
File[] fileList = folderOne.listFiles(mf); | |
for (File fileIn : fileList) { | |
File outOne = new File(folderTwo.getName() + "/" + fileIn.getName()); | |
outOne.createNewFile(); | |
CopyFiles.copyFile(fileIn, outOne); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment