Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Сите txt датотеки кои се поголеми од 10 KB од именикот in
- да се запишат во rezultat.txt (ако не постои да се креира)
- така што секоја датотека ќе биде запишана
- на почетокот на rezultat.txt.
- Значи на крај содржината на rezultat.txt треба да биде
- таква што последната датотека ќе биде запишана на почетокот,
- претпоследната под неа итн ...
- Ако има датотеки со содржина A, B, C,
- резултатот треба да е
- C B A
- */
- package ispitni;
- import java.io.*;
- import java.util.List;
- import java.util.Random;
- public class IspitJanuari2015 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- File papka = new File("dekemvri");
- File rezultat = new File(papka, "rezultat.txt");
- rezultat.createNewFile();
- func(papka, rezultat);
- }
- public static void func(File source, File rezultat) throws IOException {
- if (source.isDirectory()) {
- File[] lista = source.listFiles();
- for (File f : lista) {
- long golemina = f.length();
- float goleminaKB = (float) golemina / 1024;
- // samo za testiranje, treba da e 10
- if (goleminaKB > 0 && f.isFile()) {
- // System.out.println(f.getName());
- File tmp = getRandomFile(source, ".txt");
- readFromSourceWriteToDestination(rezultat, tmp);
- rezultat.delete();
- rezultat.createNewFile();
- readFromSourceWriteToDestination(f, rezultat);
- readFromSourceAppendToDestination(tmp, rezultat);
- tmp.delete();
- }
- }
- }
- }
- public static void readFromSourceWriteToDestination(File source, File destination) throws IOException {
- RandomAccessFile vlez = null;
- RandomAccessFile izlez = null;
- try {
- vlez = new RandomAccessFile(source, "r");
- izlez = new RandomAccessFile(destination, "rw");
- int b;
- while ((b = vlez.read()) != -1)
- izlez.write(b);
- } finally {
- if (izlez != null) {
- izlez.close();
- }
- if (vlez != null) {
- vlez.close();
- }
- }
- }
- public static void readFromSourceAppendToDestination(File source, File destination) throws IOException {
- RandomAccessFile vlez = null;
- RandomAccessFile izlez = null;
- try {
- vlez = new RandomAccessFile(source, "r");
- izlez = new RandomAccessFile(destination, "rw");
- izlez.seek(izlez.length());
- int b;
- while ((b = vlez.read()) != -1)
- izlez.write(b);
- } finally {
- if (izlez != null) {
- izlez.close();
- }
- if (vlez != null) {
- vlez.close();
- }
- }
- }
- public static File getRandomFile(File source, String ekstenzija) throws IOException {
- File[] lista = source.listFiles();
- String tmp = "tmp";
- int randomNumber = getRandomValue(1, 100);
- String fileName = tmp + randomNumber + ekstenzija;
- while (sourceContainsFileName(source, fileName)) {
- randomNumber = getRandomValue(1, 100);
- fileName = tmp + randomNumber + ekstenzija;
- }
- File tmpFile = new File(source, fileName);
- tmpFile.createNewFile();
- return tmpFile;
- }
- public static int getRandomValue(int min, int max)
- {
- Random r = new Random();
- int result = r.nextInt(max - min) + min;
- return result;
- }
- public static boolean sourceContainsFileName(File source, String fileName) {
- File[] lista = source.listFiles();
- for (File f : lista) {
- if (f.getName().equals(fileName))
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement