PageRenderTime 314ms CodeModel.GetById 151ms app.highlight 12ms RepoModel.GetById 149ms app.codeStats 0ms

/src/org/ooc/utils/FileUtils.java

http://github.com/nddrylliog/ooc
Java | 162 lines | 105 code | 21 blank | 36 comment | 19 complexity | fe57d2b16777d23f8c566b50a4360760 MD5 | raw file
  1package org.ooc.utils;
  2
  3import java.io.BufferedWriter;
  4import java.io.File;
  5import java.io.FileInputStream;
  6import java.io.FileOutputStream;
  7import java.io.FileReader;
  8import java.io.FileWriter;
  9import java.io.IOException;
 10import java.nio.channels.FileChannel;
 11import java.util.ArrayList;
 12import java.util.List;
 13import java.util.StringTokenizer;
 14
 15/**
 16 * A collection of file utilities that should have been in the
 17 * Java SDK.
 18 * 
 19 * @author Amos Wenger
 20 */
 21public class FileUtils {
 22
 23	/**
 24	 * Recursively deletes a file/folder and all its subcontent.
 25	 * In other words, "rm -rf"
 26	 * @param file
 27	 */
 28	public static void deleteRecursive(File file) {
 29	
 30		List<File> exploreQueue = new ArrayList<File>();
 31		List<File> exploreQueue2 = new ArrayList<File>();
 32		List<File> deleteQueue = new ArrayList<File>();
 33		exploreQueue.add(file);
 34		
 35		while(!exploreQueue.isEmpty()) {
 36			deleteQueue.addAll(0, exploreQueue);
 37			for(File toExplore: exploreQueue) {
 38				if(toExplore.isDirectory()) {
 39					for(File child: toExplore.listFiles()) {
 40						exploreQueue2.add(0, child);
 41					}
 42				}
 43			}
 44			exploreQueue.clear();
 45			exploreQueue.addAll(exploreQueue2);
 46			exploreQueue2.clear();
 47		}
 48		
 49		for(File toDelete: deleteQueue) {
 50			toDelete.delete();
 51		}
 52		
 53	}
 54
 55	/**
 56	 * Copy a source file to a specified destination path.
 57	 * @param sourceFile the source
 58	 * @param destFile the destination (will be created if doesn't exist, will
 59	 * be overwritten if it exists)
 60	 * @throws IOException we never know...
 61	 */
 62	public static void copy(File sourceFile, File destFile) throws IOException {
 63		
 64		if(!destFile.exists()) {
 65			destFile.createNewFile();
 66		}
 67	
 68		FileChannel source = null;
 69		FileChannel destination = null;
 70		try {
 71			source = new FileInputStream(sourceFile).getChannel();
 72			destination = new FileOutputStream(destFile).getChannel();
 73			destination.transferFrom(source, 0, source.size());
 74		}
 75		finally {
 76			if(source != null) {
 77				source.close();
 78			}
 79			if(destination != null) {
 80				destination.close();
 81			}
 82		}
 83		
 84	}
 85	
 86	/**
 87	 * Read a file contents and return it as a string
 88	 * @param file
 89	 * @return
 90	 * @throws IOException
 91	 */
 92	public static String read(File file) throws IOException {
 93	
 94		StringBuilder builder = new StringBuilder((int) file.length());
 95		FileReader reader = new FileReader(file);
 96		int n;
 97		char[] cbuf = new char[4096];
 98		while((n = reader.read(cbuf)) != -1) {
 99			builder.append(cbuf, 0, n);
100		}
101		reader.close();
102		return builder.toString();
103		
104	}
105	
106	/**
107	 * Write a string to a file
108	 * @param file
109	 * @param data
110	 * @throws IOException
111	 */
112	public static void write(File file, String data) throws IOException {
113	
114		file.getParentFile().mkdirs();
115		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
116		writer.write(data);
117		writer.close();
118	
119	}
120
121	/**
122	 * Resolve redundancies, ie. ".." and "."
123	 * @param file
124	 * @return
125	 */
126	public static File resolveRedundancies(File file) {
127
128		String path = file.getPath();
129		List<String> elems = new ArrayList<String>();
130		StringTokenizer st = new StringTokenizer(path, File.separator);
131		boolean startsWithSeparator = path.startsWith(File.separator);
132		while(st.hasMoreTokens()) {
133			String elem = st.nextToken();
134			if(elem.equals("..")) {
135				if(!elems.isEmpty()) {
136					elems.remove(elems.size() - 1);
137				} else {
138					elems.add(elem);
139				}
140			} else if(elem.equals(".")) {
141				// do nothing
142			} else {
143				elems.add(elem);
144			}
145		}
146		StringBuffer buffer = new StringBuffer(path.length());
147		if(startsWithSeparator) {
148			buffer.append(File.separator);
149		}
150		int size = elems.size();
151		int count = 0;
152		for(String elem: elems) {
153			buffer.append(elem);
154			if(++count < size) {
155				buffer.append(File.separator);
156			}
157		}
158		return new File(buffer.toString());
159		
160	}
161	
162}