Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this?
Example Output:
--------------------Configuration: <Default>--------------------
Enter the file name: kenb
Choose binary or text file(b/t): b
Choose read or write(r/w): w
Enter a line of information to write to the file:
lasdklj
Would you like to enter another line? Y/N only
n
Continue? (y/n)y
Enter the file name: kenb
Choose binary or text file(b/t): b
Choose read or write(r/w): r
File contains:
lasdklj
Continue? (y/n)y
Enter the file name: kent
Choose binary or text file(b/t): t
Choose read or write(r/w): w
Enter a line of information to write to the file:
OOP
Would you like to enter another line? Y/N only
Y
Enter a line of information to write to the file:
Java, C++ not C.
Would you like to enter another line? Y/N only
n
Continue? (y/n)y
Enter the file name: kent
Choose binary or text file(b/t): t
Choose read or write(r/w):r
File contains:
OOP
Java, C++ not C.
Continue? (y/n)n
Process completed.
/*****************************************WriteToBinaryOrText.java******************************/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class WriteToBinaryOrText {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
char exit = ' ';
do {
System.out.print("Enter the file name: ");
String fileName
= scan.nextLine();
System.out.print("Choose binary of text file(b/t): ");
char fileType =
scan.nextLine().toLowerCase().charAt(0);
System.out.print("Choose read or write(r/w): ");
char option =
scan.nextLine().toLowerCase().charAt(0);
if (fileType == 'b') {
if (option == 'r') {
readBinaryFile(fileName);
} else {
writeToBinaryFile(fileName);
}
} else {
if (option == 'r') {
readTextFile(fileName);
} else {
writeToTextFile(fileName);
}
}
System.out.print("Continue? (y/n): ");
exit =
scan.nextLine().toLowerCase().charAt(0);
} while (exit != 'n');
System.out.println("Process
completed.");
}
public static void readTextFile(String fileName) {
try {
Scanner scan =
new Scanner(new File(fileName + ".txt"));
System.out.println("File contains: ");
while
(scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
scan.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
public static void writeToTextFile(String fileName) {
Scanner scan = new
Scanner(System.in);
try {
char option = '
';
PrintWriter pt =
new PrintWriter(new File(fileName + ".txt"));
do {
System.out.println("Enter a line of information
to write to the file:");
String line = scan.nextLine();
pt.println(line);
System.out.println("Would you like to enter
another line? Y/N only");
option =
scan.nextLine().toLowerCase().charAt(0);
} while (option
!= 'n');
pt.flush();
pt.close();
} catch (FileNotFoundException
e) {
e.printStackTrace();
}
}
public static void readBinaryFile(String fileName) {
fileName = fileName + ".dat";
try {
FileInputStream
fileInputStream = new FileInputStream(fileName);
// specify
UTF-8 encoding explicitly
InputStreamReader inputStreamReader = new
InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new
BufferedReader(inputStreamReader);
String
line;
System.out.println("File contains: ");
while ((line =
bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void writeToBinaryFile(String fileName) {
Scanner scan = new
Scanner(System.in);
try {
char option =
' ';
FileOutputStream
outputStream = new FileOutputStream(fileName + ".dat");
do {
System.out.println("Enter a line of information
to write to the file:");
String bytes = scan.nextLine()+"\n";
byte[] buffer = bytes.getBytes();
outputStream.write(buffer);
System.out.println("Would you like to enter
another line? Y/N only");
option =
scan.nextLine().toLowerCase().charAt(0);
} while (option
!= 'n');
// Always close
files.
outputStream.close();
} catch (IOException ex) {
System.out.println("Error writing file '" + fileName + "'");
}
}
}
/***********************output**********************/
Enter the file name: kenb
Choose binary of text file(b/t): b
Choose read or write(r/w): w
Enter a line of information to write to the file:
lasdklj
Would you like to enter another line? Y/N only
N
Continue? (y/n): y
Enter the file name: kenb
Choose binary of text file(b/t): b
Choose read or write(r/w): r
File contains:
lasdklj
Continue? (y/n): y
Enter the file name: kent
Choose binary of text file(b/t): t
Choose read or write(r/w): w
Enter a line of information to write to the file:
OOP
Would you like to enter another line? Y/N only
Y
Enter a line of information to write to the file:
Java, C++ not C.
Would you like to enter another line? Y/N only
n
Continue? (y/n): y
Enter the file name: kent
Choose binary of text file(b/t): t
Choose read or write(r/w): r
File contains:
OOP
Java, C++ not C.
Continue? (y/n): n
Please let me know if you have any doubt or modify the answer, Thanks :)
Using Java how would I write a program that reads and writes from binary or text...
JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...
( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...
Write a program using python that reads from values from a text file and plots them using matplotlib. The input data for each graph is on three lines of the input textfile. The first line contains the x-coordiates of the points of the graph, and the second line contains the y-coordinates of the points of the graph that correspond, in the same order, to the x-coordinates on the first line. The third line in each group of three lines may...
Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0
IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...
How can I create Loops and Files on Java?
• Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...