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

Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
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.
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...
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 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 program in Java that can take a given text file and then compress it...
write a program in Java that can take a given text file and then compress it with Huffman coding due 20 october 2020 Huffman coding can be used to “zip” a text file to save space. You are required to write a program in Java that can take a given text file and then compress it with Huffman coding. Your program should be able to decompress the zipped files as well [4 marks]. In addition, show percentage gain in data...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT