本文整理汇总了Java中java.io.File.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java File.getParent方法的具体用法?Java File.getParent怎么用?Java File.getParent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.File
的用法示例。
在下文中一共展示了File.getParent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import java.io.File; //导入方法依赖的package包/类
private void build(File tmpDir, File js) throws Exception {
File outApk = new File(js.getParent(), js.getName() + ".apk");
InputStream inApk = getAssets().open("template.apk");
ApkBuilder apkBuilder = new ApkBuilder(inApk, outApk, tmpDir.getPath())
.prepare();
apkBuilder.editManifest()
.setVersionCode(5000)
.setVersionName("1.2.3")
.setAppName("Hello")
.setPackageName("com.stardust.xxx")
.commit();
apkBuilder.replaceFile("assets/script.js", js.getPath())
.setArscPackageName("com.stardust.xxx")
.build()
.sign();
}
示例2: rename
import java.io.File; //导入方法依赖的package包/类
/**
* 重命名文件
*
* @param file 文件
* @param newName 新名称
* @return {@code true}: 重命名成功<br>{@code false}: 重命名失败
*/
public static boolean rename(final File file, final String newName) {
// 文件为空返回false
if (file == null) {
return false;
}
// 文件不存在返回false
if (!file.exists()) {
return false;
}
// 新的文件名为空返回false
if (isSpace(newName)) {
return false;
}
// 如果文件名没有改变返回true
if (newName.equals(file.getName())) {
return true;
}
File newFile = new File(file.getParent() + File.separator + newName);
// 如果重命名的文件已存在返回false
return !newFile.exists()
&& file.renameTo(newFile);
}
示例3: makeParentDirectories
import java.io.File; //导入方法依赖的package包/类
public void makeParentDirectories(File f) {
String parent = f.getParent();
if (parent != null) {
new File(parent).mkdirs();
} else {
// workaround for jdk 1.1 bug (returns null when there is a parent)
parent = f.getPath();
int index = parent.lastIndexOf('/');
if (index > 0) {
parent = parent.substring(0, index);
new File(parent).mkdirs();
}
}
}
示例4: doZipAlign
import java.io.File; //导入方法依赖的package包/类
public static synchronized File doZipAlign(final AndroidBuilder androidBuilder, Project project, final File apkFile) {
final File zipalignedFile = new File(apkFile.getParent(), apkFile.getName().replace(".apk", "-zipaligned.apk"));
project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec execSpec) {
String path = androidBuilder.getTargetInfo()
.getBuildTools().getPath(ZIP_ALIGN);
execSpec.executable(new File(path));
execSpec.args("-f", "4");
execSpec.args(apkFile);
execSpec.args(zipalignedFile);
}
});
return zipalignedFile;
}
示例5: getUsableDataBase
import java.io.File; //导入方法依赖的package包/类
/**
* Returns a usable database with the given name. If a database with this name
* already exists, it is returned. Otherwise created with the given SQL create
* query.
*/
protected static SQLiteDatabase getUsableDataBase(String dbName,
String sqlCreateQuery) {
File dbFile = getPathToDb(dbName);
File fileDirectory = new File(dbFile.getParent());
if (!fileDirectory.exists()) {
// Make sure the path for the file exists, before creating the
// database.
fileDirectory.mkdirs();
}
Log.d(TAG, "DB Path: " + dbFile.getAbsolutePath());
boolean initDb = !dbFile.exists();
try {
SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
if (initDb) {
result.execSQL(sqlCreateQuery);
}
return result;
} catch (SQLiteException ex) {
Log.w(TAG, "Could not open or image database.");
return null;
}
}
示例6: readMesasurementFilesFromSafe
import java.io.File; //导入方法依赖的package包/类
/**
* Return the mesaurements
*
* @return
*/
public void readMesasurementFilesFromSafe() {
XPathExpression<Element> expr = xFactory
.compile(
"/xfdu:XFDU/dataObjectSection/dataObject[@repID='s1Level1ProductSchema']/byteStream/fileLocation",
Filters.element(), null, xfdu);
List<Element> values = expr.evaluate(safe);
this.measurements=new File[values.size()];
File safefile = new File(safePath);
String measurementPath = safefile.getParent() + "/measurement";
for (int i = 0; i < values.size(); i++) {
Element e = values.get(i);
String href = e.getAttributeValue("href");
if (href.startsWith("./"))
href = href.substring(2);
measurements[i] = new File(measurementPath + "/" + href);
System.out.println(measurements[i]);
}
}
示例7: createFileParents
import java.io.File; //导入方法依赖的package包/类
public static boolean createFileParents(File file){
if(file.exists()){
return true;
}
File parent = new File(file.getParent());
if(parent.exists()){
return true;
}
try{
parent.mkdirs();
}catch(Exception e){
return false;
}
return true;
}
示例8: compileTestFile
import java.io.File; //导入方法依赖的package包/类
protected File compileTestFile(File f, String testClass, String... extraParams) {
List<String> options = new ArrayList<>();
options.addAll(Arrays.asList(extraParams));
options.add(f.getPath());
int rc = com.sun.tools.javac.Main.compile(options.toArray(new String[options.size()]));
if (rc != 0)
throw new Error("compilation failed. rc=" + rc);
String path = f.getParent() != null ? f.getParent() : "";
return new File(path, format("%s.class", testClass));
}
示例9: onActivityResult
import java.io.File; //导入方法依赖的package包/类
@Override
protected void onActivityResult(final int requestCode, final int resultCode, @NonNull final Intent returnIntent)
{
switch (requestCode)
{
case REQUEST_FILE_CODE:
if (resultCode == AppCompatActivity.RESULT_OK)
{
final Uri fileUri = returnIntent.getData();
if (fileUri == null)
{
break;
}
Toast.makeText(this, fileUri.toString(), Toast.LENGTH_SHORT).show();
final File file = new File(fileUri.getPath());
final String parent = file.getParent();
final File parentFile = new File(parent);
final Uri parentUri = Uri.fromFile(parentFile);
final String query = file.getName();
String base = parentUri.toString();
if (base != null && !base.endsWith("/"))
{
base += '/';
}
Settings.save(this, query, base);
updateButton();
// query
// query();
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, returnIntent);
}
示例10: writeTags
import java.io.File; //导入方法依赖的package包/类
/**
* Write tags to file
* @param file File to write
* @param tag NBT tag to write
* @throws IOException If writing failed
*/
public static void writeTags(File file, NBTTagCompound tag) throws IOException {
if (file.getParentFile() != null && !file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
throw new IOException("Can't create path: " + file.getParent());
}
}
try (FileOutputStream fos = new FileOutputStream(file)) {
CompressedStreamTools.writeCompressed(tag, fos);
}
}
示例11: setFile
import java.io.File; //导入方法依赖的package包/类
public void setFile(File imageFile) {
displayName = imageFile.getPath();
try {
dat = new RandomAccessFile(imageFile, "r");
directory = imageFile.getParent();
} catch (Exception ex) {
dispose();
logger.error("Cannot access the file ",ex);
}
}
示例12: invokeNbExecAndCreateCluster
import java.io.File; //导入方法依赖的package包/类
private void invokeNbExecAndCreateCluster(File workDir, StringBuffer sb, String... args) throws Exception {
URL u = Lookup.class.getProtectionDomain().getCodeSource().getLocation();
File f = Utilities.toFile(u.toURI());
assertTrue("file found: " + f, f.exists());
File nbexec = org.openide.util.Utilities.isWindows() ? new File(f.getParent(), "nbexec.exe") : new File(f.getParent(), "nbexec");
assertTrue("nbexec found: " + nbexec, nbexec.exists());
LOG.log(Level.INFO, "nbexec: {0}", nbexec);
URL tu = NewClustersRebootCallback.class.getProtectionDomain().getCodeSource().getLocation();
File testf = Utilities.toFile(tu.toURI());
assertTrue("file found: " + testf, testf.exists());
LinkedList<String> allArgs = new LinkedList<String>(Arrays.asList(args));
allArgs.addFirst("-J-Dnetbeans.mainclass=" + NewClustersRebootCallback.class.getName());
allArgs.addFirst(System.getProperty("java.home"));
allArgs.addFirst("--jdkhome");
allArgs.addFirst(getWorkDirPath());
allArgs.addFirst("--userdir");
allArgs.addFirst(testf.getPath());
allArgs.addFirst("-cp:p");
allArgs.addFirst("--nosplash");
if (!org.openide.util.Utilities.isWindows()) {
allArgs.addFirst(nbexec.getPath());
allArgs.addFirst("-x");
allArgs.addFirst("/bin/sh");
} else {
allArgs.addFirst(nbexec.getPath());
}
LOG.log(Level.INFO, "About to execute {0}@{1}", new Object[]{allArgs, workDir});
Process p = Runtime.getRuntime().exec(allArgs.toArray(new String[0]), new String[0], workDir);
LOG.log(Level.INFO, "Process created {0}", p);
int res = readOutput(sb, p);
LOG.log(Level.INFO, "Output read: {0}", res);
String output = sb.toString();
// System.out.println("nbexec output is: " + output);
assertEquals("Execution is ok: " + output, 0, res);
}
示例13: rename
import java.io.File; //导入方法依赖的package包/类
public static boolean rename(File file, String newName) {
// 文件为空返回false
if (file == null) return false;
// 文件不存在返回false
if (!file.exists()) return false;
// 新的文件名为空返回false
if (isSpace(newName)) return false;
// 如果文件名没有改变返回true
if (newName.equals(file.getName())) return true;
File newFile = new File(file.getParent() + File.separator + newName);
// 如果重命名的文件已存在返回false
return !newFile.exists()
&& file.renameTo(newFile);
}
示例14: run
import java.io.File; //导入方法依赖的package包/类
private void run(File workDir, String... args) throws Exception {
URL u = Lookup.class.getProtectionDomain().getCodeSource().getLocation();
File f = Utilities.toFile(u.toURI());
assertTrue("file found: " + f, f.exists());
File nbexec = Utilities.isWindows() ? new File(f.getParent(), "nbexec.exe") : new File(f.getParent(), "nbexec");
assertTrue("nbexec found: " + nbexec, nbexec.exists());
URL tu = MainCallback.class.getProtectionDomain().getCodeSource().getLocation();
File testf = Utilities.toFile(tu.toURI());
assertTrue("file found: " + testf, testf.exists());
LinkedList<String> allArgs = new LinkedList<String>(Arrays.asList(args));
allArgs.addFirst("-J-Dnetbeans.mainclass=" + MainCallback.class.getName());
allArgs.addFirst(System.getProperty("java.home"));
allArgs.addFirst("--jdkhome");
allArgs.addFirst(getWorkDirPath());
allArgs.addFirst("--userdir");
allArgs.addFirst(testf.getPath());
allArgs.addFirst("-cp:p");
if (!Utilities.isWindows()) {
allArgs.addFirst(nbexec.getPath());
allArgs.addFirst("-x");
allArgs.addFirst("/bin/sh");
} else {
allArgs.addFirst(nbexec.getPath());
}
StringBuffer sb = new StringBuffer();
Process p = Runtime.getRuntime().exec(allArgs.toArray(new String[allArgs.size()]), new String[0], workDir);
int res = readOutput(sb, p);
String output = sb.toString();
assertEquals("Execution is ok: " + output, 0, res);
}
示例15: createItemAttributes
import java.io.File; //导入方法依赖的package包/类
@Override
protected DirItemAttributes createItemAttributes(final File resource) {
DirItemAttributes dia = new DirItemAttributes();
dia.setDirname(resource.getName());
dia.setItemId(resource.getAbsolutePath());
dia.setItemName(resource.getName());
String path = resource.getParent();
if (path != null) {
dia.setPath(path);
}
dirItems.put(dia.getItemId(), dia);
return dia;
}