Question

In: Computer Science

Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt java program Write...

Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt

java program
Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt

Solutions

Expert Solution

Here is the complete code for the following problem:

NOTE: If you want to write file in another location, you have to pass whole address along with text file name.

For example; If you look in this code, I just pass the txt file name "suka.txt" in File class, it will write the text file in same folder where is your java code is located

Code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

//this class will show how to write a text to a file
public class WriteFile {
  
   //execution of program starts
public static void main(String[] args) {
       //this is the text that we wnat to write
String data = "Saya suka belajar OOP";
      
       FileWriter fr = null;
  
       //since reading a file can throw input output exception, we will do inside try catch block
       try
       {
           File file = new File("suka.txt");
          
           //open the file in write mode
           fr = new FileWriter(file);;
          
           //write the data
fr.write(data);
}
       catch (IOException e)
       {
e.printStackTrace();
}
       finally
       {
//close files
           try
           {
              
fr.close();
          
           }
           catch(IOException ex)
           {
               System.out.println(ex);
           }
}
      
System.out.println("File written successfully");
}

}

------------------------------------------------------------------------------------------------------

Output:

Check if file exist in file explorer:


Related Solutions

In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
In Java language. Write a brief program that writes your name to a file in text...
In Java language. Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT