Ask Question
28 July, 19:28

Define a public static method named s2f that takes two String arguments, the name of a file and some text. The method creates the file and writes the text to it. If all goes well the method returns true. If, however, either of its arguments are null or if there is any problem in creating or writing to the file, the method returns false.

+3
Answers (1)
  1. 28 July, 23:05
    0
    import java. io. IOException;

    import java. io. PrintWriter;

    /*

    * To change this license header, choose License Headers in Project Properties.

    * To change this template file, choose Tools | Templates

    * and open the template in the editor.

    */

    public class WriteFile {

    public static boolean s2f (String fileName, String content)

    {

    boolean result = true;

    if (content = = null || fileName = = null)

    result = false;

    else

    {

    try{

    PrintWriter fileWriter = new PrintWriter (fileName, "UTF-8");

    fileWriter. println (content);

    fileWriter. close ();

    } catch (IOException e) {

    result = false;

    }

    }

    return result;

    }

    public static void main (String[] args)

    {

    boolean res = s2f ("data. txt","Java is awesome.");

    if (res==true)

    System. out. println ("Successful");

    else

    System. out. println ("There was some error in writing to a file.");

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Define a public static method named s2f that takes two String arguments, the name of a file and some text. The method creates the file and ...” in 📙 Engineering if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers