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 123 views
1 answer 279 views
1 answer 160 views
160 views asked Nov 17, 2016 by avibootz
1 answer 163 views
...