RW6
RW6

Reputation: 59

How to check if file is already created before writing a new file?

I am using android studio just to see how to create a file, or if a file already exists use the existing file. My code so far is:

              public void saveFile(){

            try{

            FileOutputStream fOut = openFileOutput("current.xml", 
            Context.MODE_APPEND);
            //OutputStreamWriter outputWriter = new 
             OutputStreamWriter(fOut);

            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fOut, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));

            serializer.startTag(null, "records");
            serializer.startTag(null,"employee");
            serializer.startTag(null, "name");
            serializer.text("Ryan");
            serializer.endTag(null,"name");
            serializer.startTag(null,"surname");
            serializer.text("Derk");
            serializer.endTag(null,"surname");
            serializer.startTag(null,"salary");
            serializer.text("60000");
            serializer.endTag(null,"salary");
            serializer.endTag(null,"employee");
            serializer.endTag(null,"records");

            serializer.endDocument();
            serializer.flush();
            fOut.close();

            Toast.makeText(getApplicationContext(), "Save Successful", 
            Toast.LENGTH_LONG).show();

            }
            catch(Throwable t){

            Toast.makeText(getApplicationContext(), "Save Unsuccessful", 
            Toast.LENGTH_LONG).show();

        }


        }

         private static String getValue(String tag, Element element) {
         NodeList nodeList = 
         element.getElementsByTagName(tag).item(0).getChildNodes();
         Node node = nodeList.item(0);
         return node.getNodeValue();
        }

How can i check if the file is already created before saving to that file? And if it is not created create the file?

Upvotes: 0

Views: 1728

Answers (4)

Haythem ROUIS
Haythem ROUIS

Reputation: 357

If I correctly understand your problem:

  • You need to have your application Context android.content.Context to call the method openFileOutput.

  • Also, to check if the file exists, you can call Context.fileList() to get the list of files in the context and check if your files exist.

    String[] files = fileList();
    for (String file : files) {
        if (file.equals(myFileName)) {
            //file exits
        }
    

Upvotes: 1

RW6
RW6

Reputation: 59

For my case what i had done is added this method:

       public Boolean checkFile(){

    Boolean myBool2 = false;
    File file  = getBaseContext().getFileStreamPath("current.xml");

    if(file.exists()){

        Toast.makeText(getApplicationContext(), "FileFound", 
    Toast.LENGTH_LONG).show();

    }

    else{

        Toast.makeText(getApplicationContext(), "No file found", 
    Toast.LENGTH_LONG).show();
        myBool2 = true;
    }
        return myBool2;

}

And it turns out that it worked! I just needed to get the context and then the filestreampath and it found the file.Thanks for everyone who has posted on this question, it really helped me understand what I was really trying to do!

Upvotes: 0

LazerBanana
LazerBanana

Reputation: 7211

You need to point to the file and check if it exists.

 File file = new File("C:\\file.txt");

    if(!file.exists()){
        System.out.println("file does not exist");
        //Do all your stuff here
    }

Upvotes: 0

Firerazzer
Firerazzer

Reputation: 192

Simply use yourFile.exists();.

Upvotes: 0

Related Questions