Question

In: Computer Science

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.

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.io.*;

import java.util.Scanner;

public class Excercise{

    public static void main(String[] args) throws FileNotFoundException{

        Scanner userScnr = new Scanner(System.in);

        System.out.print("Enter your name: ");

        String name = userScnr.nextLine();

        File file = new File("output.txt");

        System.out.println("Writing name into a file...");

        PrintWriter pw = new PrintWriter(file);

        pw.println(name);

        pw.close();

        System.out.println("Successfully your name saved into output.txt");

        System.out.println("\nReading your name from output.txt...");

        Scanner fileScnr = new Scanner(file);

        name = fileScnr.nextLine();

        System.out.println("Your name is from file is: "+name);

        fileScnr.close();

    }

}


Related Solutions

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 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 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 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.
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...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT