本文整理汇总了Java中java.io.File.createNewFile方法的典型用法代码示例。如果您正苦于以下问题:Java File.createNewFile方法的具体用法?Java File.createNewFile怎么用?Java File.createNewFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.File
的用法示例。
在下文中一共展示了File.createNewFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveBitmapToSDCard
import java.io.File; //导入方法依赖的package包/类
/**
* 保存图片到手机存储
*
* @param context 上下文
* @param bitmap bitmap对象
* @param title 文件名
* @param dirName 文件夹名称
* @param shouldRefreshGallery 是否刷新图库
* @return 返回保存成功后的绝对路径
* @throws Exception IO操作异常
*/
public static String saveBitmapToSDCard(Context context, Bitmap bitmap,
String title, String dirName,
boolean shouldRefreshGallery) throws Exception {
File dir = new File(Environment.getExternalStorageDirectory(), dirName);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, title + ".jpg");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
if (bitmap == null) {
throw new Exception("bitmap is null");
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
if (shouldRefreshGallery) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + dirName + file.getAbsolutePath())));
}
return file.getAbsolutePath();
}
示例2: getDexClassLoader
import java.io.File; //导入方法依赖的package包/类
public static DexClassLoader getDexClassLoader( Context context ){
File cacheFile = FileUtils.getCacheDir(context.getApplicationContext());
String internalPath = cacheFile.getAbsolutePath() + File.separator + "dynamic_dex.jar";
File desFile = new File(internalPath);
try {
if (!desFile.exists()) {
desFile.createNewFile();
FileUtils.copyFiles( context , "dynamic_dex.jar", desFile);
}
} catch (IOException e) {
e.printStackTrace();
}
//下面开始加载dex class
DexClassLoader dexClassLoader = new DexClassLoader(internalPath, cacheFile.getAbsolutePath(), null,context.getClassLoader() );
return dexClassLoader ;
}
示例3: createFile
import java.io.File; //导入方法依赖的package包/类
public static String createFile(final String fileFullName) {
final File f = new File(fileFullName);
if (!f.exists()) {
f.mkdirs();
try {
if (!f.createNewFile()) {
f.delete();
f.createNewFile();
}
} catch (Exception e) {
Log.e(TAG, "createFile", e);
return "";
}
} else {
return "";
}
return fileFullName;
}
示例4: touch
import java.io.File; //导入方法依赖的package包/类
/**
* 修改文件的最后访问时间。
* 如果文件不存在则创建该文件。
* <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这些信息输出是否保留还在考虑中。</b>
* @param file 需要修改最后访问时间的文件。
* @since 1.0
*/
public static void touch(File file) {
long currentTime = System.currentTimeMillis();
if (!file.exists()) {
System.err.println("file not found:" + file.getName());
System.err.println("Create a new file:" + file.getName());
try {
if (file.createNewFile()) {
System.out.println("Succeeded!");
}
else {
System.err.println("Create file failed!");
}
}
catch (IOException e) {
System.err.println("Create file failed!");
e.printStackTrace();
}
}
boolean result = file.setLastModified(currentTime);
if (!result) {
System.err.println("touch failed: " + file.getName());
}
}
示例5: testGetOpenSupport
import java.io.File; //导入方法依赖的package包/类
/**
* Test of getOpenSupport method, of class MultiBundleStructure.
*/
@Test
public void testGetOpenSupport() throws Exception{
System.out.println("getOpenSupport");
String fileName1 = "foo.properties";
String fileName2 = "foo_ru.properties";
File propFile = new File(getWorkDir(), fileName1);
propFile.createNewFile();
File propFile2 = new File(getWorkDir(), fileName2);
propFile2.createNewFile();
DataObject propDO1 = DataObject.find(FileUtil.toFileObject(propFile));
DataObject propDO2 = DataObject.find(FileUtil.toFileObject(propFile));
DataObject.find(FileUtil.toFileObject(propFile2));
assertTrue(propDO1 instanceof PropertiesDataObject);
PropertiesDataObject dataObject = (PropertiesDataObject) propDO1;
MultiBundleStructure instance = (MultiBundleStructure) dataObject.getBundleStructure();
MultiBundleStructure instance2 = (MultiBundleStructure) ((PropertiesDataObject)propDO2).getBundleStructure();
//instances should be the same
assertEquals(instance, instance2);
instance.updateEntries();
PropertiesOpen result = instance.getOpenSupport();
assertNotNull(result);
}
示例6: backupPositive_deleteWrongBackupFile
import java.io.File; //导入方法依赖的package包/类
@Test
public void backupPositive_deleteWrongBackupFile() throws EnvironmentCleanupException, IOException {
String originalFileNameChanged = originalFileName + "_changed";
new File(originalFileNameChanged).delete();
String backupFileNameChanged = backupFileName + "_changed";
File backupFileChanged = new File(backupFolder + backupFileNameChanged);
backupFileChanged.createNewFile();
FileEnvironmentUnit fileEnvUnit = new FileEnvironmentUnit(originalFileNameChanged,
backupFolder,
backupFileNameChanged);
assertTrue(backupFileChanged.exists());
fileEnvUnit.backup();
assertFalse(backupFileChanged.exists());
}
示例7: getConfigGenerateFile
import java.io.File; //导入方法依赖的package包/类
protected File getConfigGenerateFile(String configPath, String fileName) {
File dir = new File(generateCodeRootPath + File.separator + configPath);
if ( !dir.exists() ) {
if ( !dir.mkdirs() )
throw new GenerateException("Oop~ generateFile mkdirs: " + dir.getAbsolutePath() + " fail.");
}
File folder = new File(dir, fileName);
if ( !folder.exists() ) {
try {
if ( !folder.createNewFile() )
throw new GenerateException("Oop~ generateFile createNewFile: " + fileName + " fail.");
} catch ( IOException e ) {
e.printStackTrace();
}
}
return folder;
}
示例8: loadTemplateDetails
import java.io.File; //导入方法依赖的package包/类
public void loadTemplateDetails(int templateId) {
template = getway.selectedTemplate(templateId);
drugs = getway.getSelectedTemplateDrugs(templateId);
try {
file = new File("Hello.html");
file.createNewFile();
out = new PrintStream(file);
out.print(maker.makeTemplate(template,drugs));
engine = webView.getEngine();
engine.load(file.toURI().toString());
// Desktop.getDesktop().browse(URI.create("Hello.html"));
System.out.println(file.getAbsolutePath());
} catch (IOException ex) {
Logger.getLogger(ViewTemplateController.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例9: testGetEntryCount2
import java.io.File; //导入方法依赖的package包/类
/**
* Test of getEntryCount method, of class MultiBundleStructure.
*/
@Test
public void testGetEntryCount2() throws Exception{
System.out.println("getEntryCount2");
String fileName1 = "foo.properties";
File propFile = new File(getWorkDir(), fileName1);
propFile.createNewFile();
String fileName2 = "foo_debug.properties";
File propFile2 = new File(getWorkDir(), fileName2);
propFile2.createNewFile();
DataObject propDO = DataObject.find(FileUtil.toFileObject(propFile));
assertTrue(propDO instanceof PropertiesDataObject);
PropertiesDataObject dataObject = (PropertiesDataObject) propDO;
MultiBundleStructure instance = new MultiBundleStructure(dataObject);
instance.updateEntries();
int expResult = 1;
int result = instance.getEntryCount();
assertEquals(expResult, result);
}
示例10: createOrExistsFile
import java.io.File; //导入方法依赖的package包/类
private static boolean createOrExistsFile(final File file) {
if (file == null) return false;
if (file.exists()) return file.isFile();
if (!createOrExistsDir(file.getParentFile())) return false;
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
示例11: createFile
import java.io.File; //导入方法依赖的package包/类
public DocumentFile createFile(String mimeType, String displayName) {
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
if (extension != null) {
displayName = displayName + "." + extension;
}
File target = new File(this.mFile, displayName);
try {
target.createNewFile();
return new RawDocumentFile(this, target);
} catch (IOException e) {
Log.w("DocumentFile", "Failed to createFile: " + e);
return null;
}
}
示例12: createNewFile
import java.io.File; //导入方法依赖的package包/类
/**
* Create a new file, if the file exists, delete and create again.
*
* @param targetFile file.
* @return True: success, or false: failure.
*/
public static boolean createNewFile(File targetFile) {
if (targetFile.exists()) delFileOrFolder(targetFile);
try {
return targetFile.createNewFile();
} catch (IOException e) {
return false;
}
}
示例13: Region
import java.io.File; //导入方法依赖的package包/类
public Region(File file) {
this.file = file;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException exception) {
}
}
config = YamlConfiguration.loadConfiguration(file);
id = NumberUtil.parseInt(file.getName().replace(YAML, new String()));
}
示例14: testCancel
import java.io.File; //导入方法依赖的package包/类
public void testCancel () throws Exception {
final File file = new File(workDir, "file");
file.createNewFile();
final File file2 = new File(workDir, "file2");
file2.createNewFile();
final Monitor m = new Monitor();
final GitClient client = getClient(workDir);
final Exception[] exs = new Exception[1];
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
client.addNotificationListener(m);
client.add(new File[] { file, file2 },m);
} catch (GitException ex) {
exs[0] = ex;
}
}
});
m.cont = false;
t1.start();
m.waitAtBarrier();
m.cancel();
m.cont = true;
t1.join();
assertTrue(m.isCanceled());
assertEquals(1, m.count);
assertEquals(null, exs[0]);
}
示例15: setUp
import java.io.File; //导入方法依赖的package包/类
@Override
protected void setUp() throws Exception {
clearWorkDir();
File odir = new File(getWorkDir(), "orig");
odir.mkdir();
orig = new File(odir, "test.js");
orig.createNewFile();
}