Ask Question
20 December, 20:04

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. 20 December, 20:22
    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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers