Ask Question
15 November, 07:35

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. 15 November, 07:47
    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?
Find 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 you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers