How to create file if not exists in Java

1 Answer

0 votes
import java.io.File;
import java.io.FileOutputStream;

public class MyClass {
    public static void main(String args[]) throws Exception {  
        File file = new File("date.txt");
        
        if (!file.exists()) {
            file.createNewFile();
        }
        
        FileOutputStream fos = new FileOutputStream(file);
    }
}
 
 
 
 
 
/*
run:
 

 
*/

 



answered Oct 9, 2023 by avibootz

Related questions

1 answer 111 views
1 answer 262 views
1 answer 150 views
150 views asked Nov 17, 2016 by avibootz
1 answer 155 views
...