Some cleanup
This commit is contained in:
parent
9934ff1df5
commit
4d513b407a
1 changed files with 24 additions and 12 deletions
|
|
@ -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;
|
||||||
|
|
||||||
|
|
@ -54,14 +55,14 @@ public class FileUtil {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue