Department of Mathematics and Computer Science

www.univ-soukahras.dz/en/dept/mi

Module: Structures complexes et algorithmique

  1. Information
  2. Questions

create a file in java

2 votes

how to create a file and write to a file in java ?

and do I check if a file exists  ??

Asked on 13:45, Sunday 8 May 2016 By houssem djefaflia (95 points)
In Structures complexes et algorithmique


answers (4)




Answer (1)

1 votes

// instruction pour creé un fichier 
File
file = new File("path/nom_fichier.txt");
// pour verifier fchier creé ou nn if(file.createNewFile()) System.out.println("creé"); else System.out.println("erreur de creation")

Answered on 14:56, Sunday 8 May 2016 by Aterrai Zakaria (79 points)
In Structures complexes et algorithmique



Answer (2)

1 votes

//check if a file exists  

      File file = new File("path/nom_fichier.txt");
System.out.println(file.exists());

 

Answered on 15:00, Sunday 8 May 2016 by Aterrai Zakaria (79 points)
In Structures complexes et algorithmique



Answer (3)

2 votes

try{

     File file = new File("path/nom_fichier.txt");

     if(!file.exists()){

          file.createNewFile();

          // do some work you need

     }else{

         System.out.println("File already exists");

         // do some work you need

     }

}catch(IOException ex){

    // handling exception or

    ex.printStackTrace();

}

      

Answered on 16:28, Sunday 8 May 2016 by fateh bensadi (13 points)
In Structures complexes et algorithmique



Answer (4)

1 votes

c'est mieux d'utiliser la sérialisation d'objet en java puisque tu crie en fin un fichier d'objet .

c'est une class a implementer facilement .

Answered on 20:34, Sunday 8 May 2016 by khaled amirat (184 points)
In Structures complexes et algorithmique



Do you have an answer ?