Fórum pegar diretorio da aplicação. #569304
09/04/2009
0
Clebio Silva
Curtir tópico
+ 0Posts
09/04/2009
Alexandre Viriato
package src;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
public class MainTeste {
public static void main(String[] args) {
MainTeste main = new MainTeste();
System.out.println(main.getApplicationPath());
}
/**
* Retorna o caminho onde a aplicação está sendo executada
* @return caminho da aplicação
*/
public String getApplicationPath() {
String url = getClass().getResource(getClass().getSimpleName() + ".class").getPath();
File dir = new File(url).getParentFile();
String path = null;
if (dir.getPath().contains(".jar"))
path = findJarParentPath(dir);
else
path = dir.getPath();
try {
return URLDecoder.decode(path, "UTF-8");
}
catch (UnsupportedEncodingException e) {
return path.replace("%20", " ");
}
}
/**
* retorna o caminho quando a aplicao est dentro de um
* arquivo .jar
* @param jarFile
* @return
*/
private String findJarParentPath(File jarFile) {
while (jarFile.getPath().contains(".jar"))
jarFile = jarFile.getParentFile();
return jarFile.getPath().substring(6);
}
}
Gostei + 0
09/04/2009
Clebio Silva
String xml = xout.outputString(doc); fileWrite = new FileWriter(new File(appl+"/tabelas.xml")); fileWrite.write(xml); fileWrite.close();
Gostei + 0