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();
File yourFile = new File("score.txt"); yourFile.createNewFile(); // if file already exists will do nothing FileOutputStream oFile = new FileOutputStream(yourFile, false);
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; } }
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); } }
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(); }
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; }
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() ); } }
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; }
@CanIgnoreReturnValue private File newFile(String name) throws IOException { File file = new File(rootDir, name); file.createNewFile(); return file; } }
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();
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();
public static void createInitializeFile(File dir) throws IOException { File initFile = new File(dir, "initialize"); if (!initFile.exists()) { Assert.assertTrue(initFile.createNewFile()); } }
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(); } } }
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); } } }
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 ); } }
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; }
@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); }
@Test public void testDelete() throws Exception { assertThat(file.createNewFile()).isTrue(); atomicFile.delete(); assertThat(file.exists()).isFalse(); }
//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();