本文整理了Java中java.io.File.createNewFile()
方法的一些代码示例,展示了File.createNewFile()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。File.createNewFile()
方法的具体详情如下:
包路径:java.io.File
类名称:File
方法名:createNewFile
[英]Creates a new, empty file on the file system according to the path information stored in this file. This method returns true if it creates a file, false if the file already existed. Note that it returns false even if the file is not a file (because it's a directory, say).
This method is not generally useful. For creating temporary files, use #createTempFile instead. For reading/writing files, use FileInputStream, FileOutputStream, or RandomAccessFile, all of which can create files.
Note that this method does not throw IOException if the file already exists, even if it's not a regular file. Callers should always check the return value, and may additionally want to call #isFile.
[中]根据存储在此文件中的路径信息在文件系统上创建新的空文件。如果此方法创建文件,则返回true;如果文件已存在,则返回false。请注意,即使文件不是文件,它也会返回false(例如,因为它是一个目录)。
这种方法通常不有用。要创建临时文件,请改用#createTempFile。要读取/写入文件,请使用FileInputStream、FileOutputStream或RandomAccessFile,所有这些都可以创建文件。
请注意,如果文件已经存在,即使不是常规文件,此方法也不会引发IOException。调用者应始终检查返回值,并且可能还需要调用#isFile。
代码示例来源:origin: stackoverflow.com
String path = "C:" + File.separator + "hello" + File.separator + "hi.txt";
// Use relative path for Unix systems
File f = new File(path);
f.getParentFile().mkdirs();
f.createNewFile();
代码示例来源:origin: stackoverflow.com
File yourFile = new File("score.txt");
yourFile.createNewFile(); // if file already exists will do nothing
FileOutputStream oFile = new FileOutputStream(yourFile, false);
代码示例来源:origin: Blankj/AndroidUtilCode
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;
}
}
代码示例来源:origin: alibaba/nacos
public static void updateTerm(long term) throws Exception {
File file = new File(META_FILE_NAME);
if (!file.exists() && !file.getParentFile().mkdirs() && !file.createNewFile()) {
throw new IllegalStateException("failed to create meta file");
}
try (FileOutputStream outStream = new FileOutputStream(file)) {
// write meta
meta.setProperty("term", String.valueOf(term));
meta.store(outStream, null);
}
}
代码示例来源:origin: graphhopper/graphhopper
public static void main(String[] args) throws IOException {
// trying FileLock mechanics in different processes
File file = new File("tmp.lock");
file.createNewFile();
FileChannel channel = new RandomAccessFile(file, "r").getChannel();
boolean shared = true;
FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
System.out.println("locked " + lock1);
System.in.read();
System.out.println("release " + lock1);
lock1.release();
}
代码示例来源:origin: apache/storm
public static File createFile(String fileName) throws IOException {
File file = null;
file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
writeToFile(file, getRandomWordSet());
return file;
}
代码示例来源:origin: stackoverflow.com
public static void copyFile( File from, File to ) throws IOException {
if ( !to.exists() ) { to.createNewFile(); }
try (
FileChannel in = new FileInputStream( from ).getChannel();
FileChannel out = new FileOutputStream( to ).getChannel() ) {
out.transferFrom( in, 0, in.size() );
}
}
代码示例来源:origin: ltsopensource/light-task-scheduler
public static File createFileIfNotExist(File file) {
if (!file.exists()) {
// 创建父目录
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("create file[" + file.getAbsolutePath() + "] failed!", e);
}
}
return file;
}
代码示例来源:origin: google/guava
@CanIgnoreReturnValue
private File newFile(String name) throws IOException {
File file = new File(rootDir, name);
file.createNewFile();
return file;
}
}
代码示例来源:origin: hibernate/hibernate-orm
private void writeOutEnhancedClass(byte[] enhancedBytecode, File file) {
try {
if ( file.delete() ) {
if ( !file.createNewFile() ) {
logger.error( "Unable to recreate class file [" + file.getName() + "]" );
FileOutputStream outputStream = new FileOutputStream( file, false );
try {
outputStream.write( enhancedBytecode );
outputStream.close();
代码示例来源:origin: stackoverflow.com
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
代码示例来源:origin: scouter-project/scouter
public static void main(String[] args) throws IOException {
String path = getJarLocation(FileUtil.class);
System.out.println(path);
new File(path, "test.txt").createNewFile();
System.out.println(new File(path).canWrite());
System.out.println(new File(path).getAbsolutePath());
}
代码示例来源:origin: apache/zookeeper
public static void createInitializeFile(File dir) throws IOException {
File initFile = new File(dir, "initialize");
if (!initFile.exists()) {
Assert.assertTrue(initFile.createNewFile());
}
}
代码示例来源:origin: gocd/gocd
public static void createFilesByPath(File baseDir, String... files) throws IOException {
for (String file : files) {
if (file.endsWith("/")) {
File file1 = new File(baseDir, file);
file1.mkdirs();
} else {
File file1 = new File(baseDir, file);
file1.getParentFile().mkdirs();
file1.createNewFile();
}
}
}
代码示例来源:origin: google/data-transfer-project
void writeInputStream(String filename, InputStream inputStream) throws IOException {
File file = new File(TEMP_DIR + filename);
file.createNewFile();
try (OutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}
代码示例来源:origin: neo4j/neo4j
public static void writeToFile( File target, String text, boolean append ) throws IOException
{
if ( !target.exists() )
{
Files.createDirectories( target.getParentFile().toPath() );
//noinspection ResultOfMethodCallIgnored
target.createNewFile();
}
try ( Writer out = new OutputStreamWriter( new FileOutputStream( target, append ), StandardCharsets.UTF_8 ) )
{
out.write( text );
}
}
代码示例来源:origin: ltsopensource/light-task-scheduler
public static File createFileIfNotExist(File file) {
if (!file.exists()) {
// 创建父目录
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("create file[" + file.getAbsolutePath() + "] failed!", e);
}
}
return file;
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testWithANonMatchingFile() throws IOException, InterruptedException {
watcher.watch();
// Initial deployment
assertWaitUntil(() -> deploy.get() == 1);
File file = new File(root, "foo.nope");
file.createNewFile();
Thread.sleep(500);
assertThat(undeploy.get()).isEqualTo(0);
assertThat(deploy.get()).isEqualTo(1);
}
代码示例来源:origin: google/ExoPlayer
@Test
public void testDelete() throws Exception {
assertThat(file.createNewFile()).isTrue();
atomicFile.delete();
assertThat(file.exists()).isFalse();
}
代码示例来源:origin: stackoverflow.com
//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();
//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
内容来源于网络,如有侵权,请联系作者删除!