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

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.
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.
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):...
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.
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...
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 php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT