Question

In: Computer Science

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.

Solutions

Expert Solution

SOURCE CODE:

aaa.java file

import java.io.*;
import java.util.*;
public class aaa {
    public static void main(String[] args) throws IOException {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter Your name:");
      // reading user name using scanner
      String s=sc.nextLine();      
      // opening the file "user.txt" in write mode using PrintWriter class
      PrintWriter writer= new PrintWriter(new File("user.txt"));
      // writing in file
      writer.write(s);
      // closing the file                                                 
      writer.close();

      // opening file "user.txt" in read mode
      FileReader f=new FileReader("user.txt");
      int k;

      System.out.println("\nThe file contains the following text:\n");
      //printing the contents of the file
      while((k=f.read())!=-1)
      {
        System.out.print((char)k);
      }
      f.close();

    }
}

CODE SCREENSHOT:

OUTPUT SCREENSHOT:

Output1:-

output-2:-


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.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
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.
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
2. Write a program which reads in the text file generated in part one and writes...
2. Write a program which reads in the text file generated in part one and writes out the same data as a comma-separated values (CSV) file for further processing. The file should have a header line which identifies which column contains which values and should look something like this: Time, Potentiometer, Temperature, Light, Switch0, Switch1, Switch2, Switch3 That header line should be followed by detail lines containing the measurements and should look something like this (matching the above Arduino output):...
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 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...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
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 C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT