Answers for "java save File to disk"

31

writing to a file in java

import java.io.FileWriter;   // Import the FileWriter class
import java.io.IOException;  // Import the IOException class to handle errors

public class WriteToFile {
  public static void main(String[] args) {
    try {
      FileWriter myWriter = new FileWriter("filename.txt");
      myWriter.write("Files in Java might be tricky, but it is fun enough!");
      myWriter.close();
      System.out.println("Successfully wrote to the file.");
    } catch (IOException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}
Posted by: Guest on April-22-2020
0

java stream write to file

@Test
public void givenWritingStringToFile_whenUsingPrintWriter_thenCorrect() 
  throws IOException {
    FileWriter fileWriter = new FileWriter(fileName);
    PrintWriter printWriter = new PrintWriter(fileWriter);
    printWriter.print("Some String");
    printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
    printWriter.close();
}
Posted by: Guest on December-29-2020
0

write file from a specific location in java

File file = new File("Z:\\results\\results.txt");
Posted by: Guest on June-23-2020
0

save file to disk java

public class Test {

   static final String[] conjunction = { "and", "or", "but", "because"};

   static final String[] proper_noun = { "Fred", "Jane", "Richard Nixon","Miss America"};

   static final String[] common_noun = { "man", "woman", "fish", "elephant", "unicorn"};                                  

   static final String[] determiner = { "a", "the", "every", "some"};

   static final String[] adjective = { "big", "tiny", "pretty", "bald"};

   static final String[] intransitive_verb = { "runs", "jumps", "talks", "sleeps"};

   static final String[] transitive_verb = { "loves", "hates", "sees", "knows", "looks for", "finds"};

  

  public static void main(String[] args) {

      while (true) {

         randomSentence();

      System.out.println(".\n");

         try {

             Thread.sleep(3000);

         }

         catch (InterruptedException e) {

         }

      }

   }

   static void randomSentence() {

      randomNounPhrase();

              randomVerbPhrase();

      if (Math.random() > 0.75) {

              System.out.print(" " + randomItem(conjunction));

              randomSentence();

      }

   }

   static void randomNounPhrase() {

          if (Math.random() > 0.75)

             System.out.print(" " + randomItem(proper_noun));

          else

          {

             System.out.print(" " + randomItem(determiner));

             if (Math.random() > 0.5)

         System.out.print(" " + randomItem(adjective)+".");

                System.out.print(" " + randomItem(common_noun));

                 if (Math.random() > 0.5){

                      System.out.print(" who" );

                      randomVerbPhrase();

                 }

          }

     }

      static void randomVerbPhrase() {

          if (Math.random() > 0.75)

             System.out.print(" " + randomItem(intransitive_verb));

                else if (Math.random() > 0.50) {

                        System.out.print(" " + randomItem(transitive_verb));

                        randomNounPhrase();

                }

                else if (Math.random() > 0.25)

                    System.out.print(" is " + randomItem(adjective));

                else {

                    System.out.print(" believes that");

                    randomNounPhrase();

                    randomVerbPhrase();

                }

       }

   static String randomItem(String[] listOfStrings){

       return listOfStrings[(int)(Math.random()*listOfStrings.length)];

   }

}
Posted by: Guest on June-29-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language