Some cleanup

This commit is contained in:
Ziver Koc 2020-11-10 21:46:29 +01:00
parent 9934ff1df5
commit 4d513b407a

View file

@ -33,6 +33,7 @@ import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -52,16 +53,16 @@ public class FileUtil {
* @return A String with a relative path * @return A String with a relative path
*/ */
public static String relativePath(File file, String path){ public static String relativePath(File file, String path){
if( file == null || path == null ) if(file == null || path == null)
return null; return null;
String absolute = file.getAbsolutePath(); String absolute = file.getAbsolutePath();
String tmpPath = path.replaceAll( String tmpPath = path.replaceAll(
"[/\\\\]", "[/\\\\]",
Matcher.quoteReplacement(File.separator)); Matcher.quoteReplacement(File.separator));
String relative = absolute.substring( String relative = absolute.substring(
absolute.indexOf(tmpPath)+path.length(), absolute.indexOf(tmpPath)+path.length());
absolute.length());
return relative; return relative;
} }
@ -77,14 +78,14 @@ public class FileUtil {
public static File find(String path){ public static File find(String path){
try { try {
File file = new File(path); File file = new File(path);
if(file!=null && file.exists()){ if(file.exists())
return file; return file;
}
URL url = findURL(path); URL url = findURL(path);
if(url != null) if(url != null && "file".equals(url.getProtocol()))
return new File(url.toURI()); return new File(url.toURI());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.log(Level.FINE, "Unable to find file: " + path, e);
} }
return null; return null;
} }
@ -113,7 +114,7 @@ public class FileUtil {
*/ */
public static File getNextFile(File file){ public static File getNextFile(File file){
for(int i = 1; i<10000; ++i){ for(int i = 1; i<10000; ++i){
File next = new File(file.getParentFile(), file.getName()+"."+ StringUtil.prefixInt(i, 4)); File next = new File(file.getParentFile(), file.getName() + "." + StringUtil.prefixInt(i, 4));
if(!next.exists()) if(!next.exists())
return next; return next;
} }
@ -139,17 +140,18 @@ public class FileUtil {
public static InputStream getInputStream(String path){ public static InputStream getInputStream(String path){
try { try {
File file = new File(path); File file = new File(path);
if(file!=null && file.exists()){ if(file.exists())
return new BufferedInputStream( new FileInputStream( file ) ); return new BufferedInputStream(new FileInputStream(file));
}
return Thread.currentThread().getContextClassLoader() return Thread.currentThread().getContextClassLoader()
.getResourceAsStream(path); .getResourceAsStream(path);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.log(Level.FINE, "Unable to find file: " + path, e);
} }
return null; return null;
} }
/** /**
* Reads and returns the content of a file as a String. * Reads and returns the content of a file as a String.
* This method can read files inside compressed archives also. * This method can read files inside compressed archives also.
@ -187,6 +189,16 @@ public class FileUtil {
return new String(IOUtil.readContent(url.openStream(), true)); return new String(IOUtil.readContent(url.openStream(), true));
} }
/**
* Replaces the contents of a file with the specified String.
*
* @param file the file to write the data to
* @param data the String to write to the file
*/
public static void setContent(File file, String data) throws IOException{
setContent(file, data.getBytes());
}
/** /**
* Replaces the contents of a file with the specified data. * Replaces the contents of a file with the specified data.
* *