Added File copy method
This commit is contained in:
parent
25d70c8075
commit
d3f3c02f78
4 changed files with 330 additions and 313 deletions
0
Zutil.jar
Normal file → Executable file
0
Zutil.jar
Normal file → Executable file
0
src/zutil/db/DBConnection.java
Normal file → Executable file
0
src/zutil/db/DBConnection.java
Normal file → Executable file
5
src/zutil/io/IOUtil.java
Normal file → Executable file
5
src/zutil/io/IOUtil.java
Normal file → Executable file
|
|
@ -55,7 +55,7 @@ public class IOUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads and returns all the content of a stream as a String.
|
* Reads and returns all the content of a stream as a String.
|
||||||
* This function will close the inout stream at the end.
|
* This function will close the input stream at the end.
|
||||||
*
|
*
|
||||||
* @param stream
|
* @param stream
|
||||||
* @return a String with the content of the stream
|
* @return a String with the content of the stream
|
||||||
|
|
@ -73,7 +73,8 @@ public class IOUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies all data from the input stream to the output stream
|
* Copies all data from the input stream to the output stream.
|
||||||
|
* The input stream will not be closed after method has returned.
|
||||||
*/
|
*/
|
||||||
public static void copyStream(InputStream in, OutputStream out) throws IOException {
|
public static void copyStream(InputStream in, OutputStream out) throws IOException {
|
||||||
byte[] buff = new byte[256];
|
byte[] buff = new byte[256];
|
||||||
|
|
|
||||||
|
|
@ -38,329 +38,345 @@ import java.util.regex.Matcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* File path utilities
|
* File path utilities
|
||||||
*
|
*
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a String with a relative path from the given path
|
* Returns a String with a relative path from the given path
|
||||||
*
|
*
|
||||||
* @param file is the file to get a relative path from
|
* @param file is the file to get a relative path from
|
||||||
* @param path is the path
|
* @param path is the path
|
||||||
* @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());
|
absolute.length());
|
||||||
return relative;
|
return relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the File object for the given file.
|
* Returns the File object for the given file.
|
||||||
* Can not point to files in JAR files.
|
* Can not point to files in JAR files.
|
||||||
*
|
*
|
||||||
* @param path is the path to the file (no / if not absolute path)
|
* @param path is the path to the file (no / if not absolute path)
|
||||||
* @return A File object for the file
|
* @return A File object for the file
|
||||||
*/
|
*/
|
||||||
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!=null && file.exists()){
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
URL url = findURL(path);
|
URL url = findURL(path);
|
||||||
if(url != null)
|
if(url != null)
|
||||||
return new File(url.toURI());
|
return new File(url.toURI());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the URL to the given file
|
* Copy the contents of a source file to another file.
|
||||||
*
|
* NOTE: the method will replace the destination file if it exists.
|
||||||
* @param path is the path to the file (no / if not absolute path)
|
*
|
||||||
* @return A URL object for the file
|
* @param source
|
||||||
* @throws URISyntaxException
|
* @param destination
|
||||||
*/
|
*/
|
||||||
public static URL findURL(String path){
|
public static void copy(String source, String destination) throws IOException{
|
||||||
return Thread.currentThread().getContextClassLoader().getResource(path);
|
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
|
||||||
}
|
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination));){
|
||||||
|
|
||||||
/**
|
IOUtil.copyStream(in, out);
|
||||||
* Returns a InputStream from the path
|
}
|
||||||
*
|
}
|
||||||
* @param path is the path to the file (no / if not absolute path)
|
|
||||||
* @return A InputStream object for the file
|
/**
|
||||||
*/
|
* Returns the URL to the given file
|
||||||
public static InputStream getInputStream(String path){
|
*
|
||||||
try {
|
* @param path is the path to the file (no / if not absolute path)
|
||||||
File file = new File(path);
|
* @return A URL object for the file
|
||||||
if(file!=null && file.exists()){
|
* @throws URISyntaxException
|
||||||
return new BufferedInputStream( new FileInputStream( file ) );
|
*/
|
||||||
}
|
public static URL findURL(String path){
|
||||||
return Thread.currentThread().getContextClassLoader()
|
return Thread.currentThread().getContextClassLoader().getResource(path);
|
||||||
.getResourceAsStream(path);
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
/**
|
||||||
}
|
* Returns a InputStream from the path
|
||||||
return null;
|
*
|
||||||
}
|
* @param path is the path to the file (no / if not absolute path)
|
||||||
|
* @return A InputStream object for the file
|
||||||
/**
|
*/
|
||||||
* Reads and returns the content of a file as a String.
|
public static InputStream getInputStream(String path){
|
||||||
*
|
try {
|
||||||
* @param file
|
File file = new File(path);
|
||||||
* @return the file content
|
if(file!=null && file.exists()){
|
||||||
*/
|
return new BufferedInputStream( new FileInputStream( file ) );
|
||||||
public static String getContent(File file) throws IOException{
|
}
|
||||||
|
return Thread.currentThread().getContextClassLoader()
|
||||||
|
.getResourceAsStream(path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads and returns the content of a file as a String.
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @return the file content
|
||||||
|
*/
|
||||||
|
public static String getContent(File file) throws IOException{
|
||||||
return new String(getByteContent(file));
|
return new String(getByteContent(file));
|
||||||
}
|
}
|
||||||
public static byte[] getByteContent(File file) throws IOException {
|
public static byte[] getByteContent(File file) throws IOException {
|
||||||
InputStream in = new FileInputStream(file);
|
InputStream in = new FileInputStream(file);
|
||||||
byte[] data = IOUtil.getContent(in);
|
byte[] data = IOUtil.getContent(in);
|
||||||
in.close();
|
in.close();
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads and returns the content of a file as a String.
|
* Reads and returns the content of a file as a String.
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return the file content
|
* @return the file content
|
||||||
*/
|
*/
|
||||||
public static String getContent(URL url) throws IOException{
|
public static String getContent(URL url) throws IOException{
|
||||||
InputStream in = url.openStream();
|
InputStream in = url.openStream();
|
||||||
String data = new String(IOUtil.getContent(in));
|
String data = new String(IOUtil.getContent(in));
|
||||||
in.close();
|
in.close();
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the contents of a file with the specified data.
|
* Replaces the contents of a file with the specified data.
|
||||||
*
|
*
|
||||||
* @param file the file to write the data to
|
* @param file the file to write the data to
|
||||||
* @param data the data to write to the file
|
* @param data the data to write to the file
|
||||||
*/
|
*/
|
||||||
public static void setContent(File file, byte[] data) throws IOException{
|
public static void setContent(File file, byte[] data) throws IOException{
|
||||||
OutputStream out = new FileOutputStream(file);
|
OutputStream out = new FileOutputStream(file);
|
||||||
out.write(data);
|
out.write(data);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Cache for the search functions
|
|
||||||
*/
|
|
||||||
private static HashMap<SearchItem,List<File>> search_cache = new HashMap<SearchItem,List<File>>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An Cache Item class to identify different cached items
|
* Cache for the search functions
|
||||||
* @author Ziver
|
*/
|
||||||
*/
|
private static HashMap<SearchItem,List<File>> search_cache = new HashMap<SearchItem,List<File>>();
|
||||||
private static class SearchItem{
|
|
||||||
private File dir;
|
|
||||||
private boolean folders;
|
|
||||||
private int recurse;
|
|
||||||
|
|
||||||
protected SearchItem(File dir, boolean folders, int recurse){
|
|
||||||
this.dir = dir;
|
|
||||||
this.folders = folders;
|
|
||||||
this.recurse = recurse;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object o){
|
|
||||||
if(o!=null && o instanceof SearchItem){
|
|
||||||
SearchItem si = (SearchItem)o;
|
|
||||||
return dir.equals(si.dir) && folders == si.folders && recurse == si.recurse;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
public int hashCode(){
|
|
||||||
int hash = 133;
|
|
||||||
hash = 23 * hash + dir.hashCode();
|
|
||||||
hash = 23 * hash + (folders ? 1 : 0);
|
|
||||||
hash = 23 * hash + recurse;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Same as search(File dir) but it caches the result
|
|
||||||
* to be used next time this function is called with
|
|
||||||
* the same parameters.
|
|
||||||
*/
|
|
||||||
public static List<File> cachedSearch(File dir){
|
|
||||||
return cachedSearch(dir, new LinkedList<File>(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Same as search(File dir, List<File> fileList, boolean recursive)
|
|
||||||
* but is caches the result to be used next time this function is
|
|
||||||
* called with the same parameters.
|
|
||||||
*/
|
|
||||||
public static List<File> cachedSearch(File dir, List<File> fileList, boolean recursive){
|
|
||||||
return cachedSearch(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Same as search(File dir, List<File> fileList, boolean folders, int recurse)
|
|
||||||
* but is caches the result to be used next time this function is called
|
|
||||||
* with the same parameters.
|
|
||||||
*/
|
|
||||||
public static List<File> cachedSearch(File dir, List<File> fileList, boolean folders, int recurse){
|
|
||||||
SearchItem si = new SearchItem(dir, folders, recurse);
|
|
||||||
if( search_cache.containsKey(si) ){
|
|
||||||
fileList.addAll( search_cache.get(si) );
|
|
||||||
return fileList;
|
|
||||||
}
|
|
||||||
search(dir, fileList, folders, recurse);
|
|
||||||
search_cache.put(si, fileList);
|
|
||||||
return fileList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a List with all the files in a folder and sub folders
|
|
||||||
*
|
|
||||||
* @param dir is the directory to search in
|
|
||||||
* @return The List with the files
|
|
||||||
*/
|
|
||||||
public static List<File> search(File dir){
|
|
||||||
return search(dir, new LinkedList<File>(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a ArrayList with all the files in a folder and sub folders
|
|
||||||
*
|
|
||||||
* @param dir is the directory to search in
|
|
||||||
* @param fileList is the List to add the files to
|
|
||||||
* @param recursive is if the method should search the sub directories to.
|
|
||||||
* @return A List with the files
|
|
||||||
*/
|
|
||||||
public static List<File> search(File dir, List<File> fileList, boolean recursive){
|
|
||||||
return search(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a ArrayList with all the files in a folder and sub folders
|
|
||||||
*
|
|
||||||
* @param dir is the directory to search in
|
|
||||||
* @param fileList is the List to add the files to
|
|
||||||
* @param folders is if the method should add the folders to the List
|
|
||||||
* @param recurse is how many times it should recurse into folders
|
|
||||||
* @return A List with the files and/or folders
|
|
||||||
*/
|
|
||||||
public static List<File> search(File dir, List<File> fileList, boolean folders, int recurse){
|
|
||||||
if(recurse<0)
|
|
||||||
return fileList;
|
|
||||||
--recurse;
|
|
||||||
if(folders){
|
|
||||||
fileList.add( dir );
|
|
||||||
}
|
|
||||||
|
|
||||||
File file;
|
|
||||||
String[] temp = dir.list();
|
|
||||||
if(temp != null){
|
|
||||||
for(int i=0; i<temp.length ;i++){
|
|
||||||
file = new File(dir.getPath()+File.separator+temp[i]);
|
|
||||||
if(file.isDirectory()){
|
|
||||||
logger.finer("Found Folder: "+file);
|
|
||||||
search(new File(dir.getPath()+File.separator+temp[i]+File.separator), fileList, folders, recurse);
|
|
||||||
}
|
|
||||||
else if(file.isFile()){
|
|
||||||
logger.finer("Found File: "+file);
|
|
||||||
fileList.add(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the extension(without the dot) of the file. e.g. "png" "avi"
|
|
||||||
*
|
|
||||||
* @param file is the file
|
|
||||||
* @return The extension
|
|
||||||
*/
|
|
||||||
public static String getFileExtension(File file){
|
|
||||||
return getFileExtension(file.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the extension(without the dot) of the file. e.g. "png" "avi"
|
|
||||||
*
|
|
||||||
* @param file is the file
|
|
||||||
* @return The extension
|
|
||||||
*/
|
|
||||||
public static String getFileExtension(String file){
|
|
||||||
if( file == null || file.lastIndexOf(".") == -1 )
|
|
||||||
return "";
|
|
||||||
return file.substring(file.lastIndexOf(".")+1, file.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces the current extension on the file withe the given one.
|
/**
|
||||||
*
|
* An Cache Item class to identify different cached items
|
||||||
* @param file is the name of the file
|
* @author Ziver
|
||||||
* @param ext is the new extension, without the dot
|
*/
|
||||||
* @return
|
private static class SearchItem{
|
||||||
*/
|
private File dir;
|
||||||
public static String changeExtension(String file, String ext) {
|
private boolean folders;
|
||||||
if( file == null )
|
private int recurse;
|
||||||
return null;
|
|
||||||
if( file.lastIndexOf(".") == -1 )
|
protected SearchItem(File dir, boolean folders, int recurse){
|
||||||
return file+"."+ext;
|
this.dir = dir;
|
||||||
return file.substring(0, file.lastIndexOf(".")+1)+ext;
|
this.folders = folders;
|
||||||
}
|
this.recurse = recurse;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* This function will replace some data between two boundaries.
|
public boolean equals(Object o){
|
||||||
* If the boundary is not found it will be added to the end of the file.
|
if(o!=null && o instanceof SearchItem){
|
||||||
*
|
SearchItem si = (SearchItem)o;
|
||||||
* @param file is the file to modify
|
return dir.equals(si.dir) && folders == si.folders && recurse == si.recurse;
|
||||||
* @param boundary is the start and end boundary to put the data between, this is a full line boundary.
|
}
|
||||||
* @param data is the data that will be written to the file
|
return false;
|
||||||
*/
|
}
|
||||||
public static void writeBetweenBoundary(File file, String boundary, String data) throws IOException{
|
public int hashCode(){
|
||||||
BufferedReader in = new BufferedReader(new FileReader(file));
|
int hash = 133;
|
||||||
StringBuilder output = new StringBuilder();
|
hash = 23 * hash + dir.hashCode();
|
||||||
|
hash = 23 * hash + (folders ? 1 : 0);
|
||||||
String line;
|
hash = 23 * hash + recurse;
|
||||||
while((line = in.readLine()) != null){
|
return 0;
|
||||||
// Found starting boundary
|
}
|
||||||
if(line.equals(boundary)){
|
}
|
||||||
while((line = in.readLine()) != null)
|
|
||||||
// Find ending boundary
|
/**
|
||||||
if(line.equals(boundary)) break;
|
* Same as search(File dir) but it caches the result
|
||||||
// EOF and no ending boundary found
|
* to be used next time this function is called with
|
||||||
if(line == null){
|
* the same parameters.
|
||||||
in.close();
|
*/
|
||||||
throw new EOFException("No ending boundary found");
|
public static List<File> cachedSearch(File dir){
|
||||||
}
|
return cachedSearch(dir, new LinkedList<File>(), true);
|
||||||
// Write the new data
|
}
|
||||||
output.append(boundary).append('\n');
|
|
||||||
output.append(data).append('\n');
|
/**
|
||||||
output.append(boundary).append('\n');
|
* Same as search(File dir, List<File> fileList, boolean recursive)
|
||||||
}
|
* but is caches the result to be used next time this function is
|
||||||
else
|
* called with the same parameters.
|
||||||
output.append(line).append('\n');
|
*/
|
||||||
}
|
public static List<File> cachedSearch(File dir, List<File> fileList, boolean recursive){
|
||||||
in.close();
|
return cachedSearch(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
||||||
|
}
|
||||||
// Save changes
|
|
||||||
FileWriter out = new FileWriter(file);
|
/**
|
||||||
out.write(output.toString());
|
* Same as search(File dir, List<File> fileList, boolean folders, int recurse)
|
||||||
out.close();
|
* but is caches the result to be used next time this function is called
|
||||||
}
|
* with the same parameters.
|
||||||
|
*/
|
||||||
|
public static List<File> cachedSearch(File dir, List<File> fileList, boolean folders, int recurse){
|
||||||
|
SearchItem si = new SearchItem(dir, folders, recurse);
|
||||||
|
if( search_cache.containsKey(si) ){
|
||||||
|
fileList.addAll( search_cache.get(si) );
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
search(dir, fileList, folders, recurse);
|
||||||
|
search_cache.put(si, fileList);
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a List with all the files in a folder and sub folders
|
||||||
|
*
|
||||||
|
* @param dir is the directory to search in
|
||||||
|
* @return The List with the files
|
||||||
|
*/
|
||||||
|
public static List<File> search(File dir){
|
||||||
|
return search(dir, new LinkedList<File>(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a ArrayList with all the files in a folder and sub folders
|
||||||
|
*
|
||||||
|
* @param dir is the directory to search in
|
||||||
|
* @param fileList is the List to add the files to
|
||||||
|
* @param recursive is if the method should search the sub directories to.
|
||||||
|
* @return A List with the files
|
||||||
|
*/
|
||||||
|
public static List<File> search(File dir, List<File> fileList, boolean recursive){
|
||||||
|
return search(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a ArrayList with all the files in a folder and sub folders
|
||||||
|
*
|
||||||
|
* @param dir is the directory to search in
|
||||||
|
* @param fileList is the List to add the files to
|
||||||
|
* @param folders is if the method should add the folders to the List
|
||||||
|
* @param recurse is how many times it should recurse into folders
|
||||||
|
* @return A List with the files and/or folders
|
||||||
|
*/
|
||||||
|
public static List<File> search(File dir, List<File> fileList, boolean folders, int recurse){
|
||||||
|
if(recurse<0)
|
||||||
|
return fileList;
|
||||||
|
--recurse;
|
||||||
|
if(folders){
|
||||||
|
fileList.add( dir );
|
||||||
|
}
|
||||||
|
|
||||||
|
File file;
|
||||||
|
String[] temp = dir.list();
|
||||||
|
if(temp != null){
|
||||||
|
for(int i=0; i<temp.length ;i++){
|
||||||
|
file = new File(dir.getPath()+File.separator+temp[i]);
|
||||||
|
if(file.isDirectory()){
|
||||||
|
logger.finer("Found Folder: "+file);
|
||||||
|
search(new File(dir.getPath()+File.separator+temp[i]+File.separator), fileList, folders, recurse);
|
||||||
|
}
|
||||||
|
else if(file.isFile()){
|
||||||
|
logger.finer("Found File: "+file);
|
||||||
|
fileList.add(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the extension(without the dot) of the file. e.g. "png" "avi"
|
||||||
|
*
|
||||||
|
* @param file is the file
|
||||||
|
* @return The extension
|
||||||
|
*/
|
||||||
|
public static String getFileExtension(File file){
|
||||||
|
return getFileExtension(file.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the extension(without the dot) of the file. e.g. "png" "avi"
|
||||||
|
*
|
||||||
|
* @param file is the file
|
||||||
|
* @return The extension
|
||||||
|
*/
|
||||||
|
public static String getFileExtension(String file){
|
||||||
|
if( file == null || file.lastIndexOf(".") == -1 )
|
||||||
|
return "";
|
||||||
|
return file.substring(file.lastIndexOf(".")+1, file.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the current extension on the file withe the given one.
|
||||||
|
*
|
||||||
|
* @param file is the name of the file
|
||||||
|
* @param ext is the new extension, without the dot
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String changeExtension(String file, String ext) {
|
||||||
|
if( file == null )
|
||||||
|
return null;
|
||||||
|
if( file.lastIndexOf(".") == -1 )
|
||||||
|
return file+"."+ext;
|
||||||
|
return file.substring(0, file.lastIndexOf(".")+1)+ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function will replace some data between two boundaries.
|
||||||
|
* If the boundary is not found it will be added to the end of the file.
|
||||||
|
*
|
||||||
|
* @param file is the file to modify
|
||||||
|
* @param boundary is the start and end boundary to put the data between, this is a full line boundary.
|
||||||
|
* @param data is the data that will be written to the file
|
||||||
|
*/
|
||||||
|
public static void writeBetweenBoundary(File file, String boundary, String data) throws IOException{
|
||||||
|
BufferedReader in = new BufferedReader(new FileReader(file));
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while((line = in.readLine()) != null){
|
||||||
|
// Found starting boundary
|
||||||
|
if(line.equals(boundary)){
|
||||||
|
while((line = in.readLine()) != null)
|
||||||
|
// Find ending boundary
|
||||||
|
if(line.equals(boundary)) break;
|
||||||
|
// EOF and no ending boundary found
|
||||||
|
if(line == null){
|
||||||
|
in.close();
|
||||||
|
throw new EOFException("No ending boundary found");
|
||||||
|
}
|
||||||
|
// Write the new data
|
||||||
|
output.append(boundary).append('\n');
|
||||||
|
output.append(data).append('\n');
|
||||||
|
output.append(boundary).append('\n');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
output.append(line).append('\n');
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
// Save changes
|
||||||
|
FileWriter out = new FileWriter(file);
|
||||||
|
out.write(output.toString());
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue