Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaIO;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.Random;
- public class IspitSeptemvri2015termin1 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- work("dekemvri","rez");
- }
- public static void rekurzijaBrisenje(File papka) {
- File[] lista = papka.listFiles();
- for (File f : lista) {
- if(f.isFile()){
- f.delete();
- }
- else if (f.isDirectory())
- {
- rekurzijaBrisenje(f);
- f.delete();
- }
- }
- }
- public static void work(String in_value, String out_value) throws IOException {
- File in = new File(in_value);
- if(!in.exists())
- return;
- if(!in.isDirectory())
- return;
- File out = new File(out_value);
- if (out.exists()) {
- if(out.isDirectory())
- rekurzijaBrisenje(out);
- else
- return;
- }
- else
- out.mkdirs();
- File rezultat = new File(out,"big.bin");
- if(!rezultat.exists())
- rezultat.createNewFile();
- File[] lista = in.listFiles();
- for (File f : lista) {
- long golemina = f.length();
- float goleminaKB = (float) golemina / 1024;
- if(f.isFile() && f.getName().endsWith(".txt")) {
- if (goleminaKB < 10) {
- copy(f,out);
- }
- else {
- System.out.println(f.getName());
- File tmp = getRandomFile(in,".bin");
- tmp.createNewFile();
- readFromSourceWriteToDestination(rezultat,tmp);
- rezultat.delete();
- rezultat.createNewFile();
- readFromSourceWriteToDestination(f, rezultat);
- readFromSourceAppendToDestination(tmp,rezultat);
- tmp.delete();
- }
- }
- else if (f.isDirectory())
- work(in.getAbsolutePath(),out.getAbsolutePath());
- }
- }
- private static void readFromSourceAppendToDestination(File source, File destination) throws IOException {
- // TODO Auto-generated method stub
- RandomAccessFile vlez = null;
- RandomAccessFile izlez = null;
- try {
- vlez = new RandomAccessFile(source, "r");
- izlez = new RandomAccessFile(destination, "rw");
- izlez.seek(izlez.length());
- int b = 0;
- while ((b=vlez.read()) != -1)
- izlez.write(b);
- }
- finally {
- if(vlez!=null)
- vlez.close();
- if(izlez!=null)
- izlez.close();
- }
- }
- 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 = 0;
- while ((b=vlez.read()) != -1)
- izlez.write(b);
- }
- finally {
- if(vlez!=null)
- vlez.close();
- if(izlez!=null)
- izlez.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;
- }
- public static void copy(File vleznaDatoteka, File destination) throws IOException {
- // TODO Auto-generated method stub
- File nov = new File(destination, vleznaDatoteka.getName());
- if(nov.exists()){
- System.out.println("datotekata " + vleznaDatoteka.getName() + " vekje postoi");
- } else {
- nov.createNewFile();
- BufferedReader vlez = null;
- BufferedWriter izlez = null;
- try {
- vlez = new BufferedReader(new FileReader(vleznaDatoteka));
- izlez = new BufferedWriter(new FileWriter(nov));
- String line;
- while((line = vlez.readLine()) != null)
- izlez.write(line + "\r\n");
- }
- finally {
- if(vlez!=null)
- vlez.close();
- if(izlez!=null)
- izlez.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement