Question

How do I create a simple java program file that opens and reads a file using...

How do I create a simple java program file that opens and reads a file using java.io? I have the file Im supposed to open and read but I dont know how to code it to open and read the file?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package abcSolution1;

import java.io.File;

import java.io.FileNotFoundException;

import java.net.URISyntaxException;

import java.net.URL;

import java.util.Scanner;

public class ReadFile {

public static void main(String[] args) {

// create scanner to get filename from keyboard

Scanner sc=new Scanner(System.in);

System.out.println("Enter file name");

// gettin input from user

String fileName=sc.nextLine();

// creating url object for the file

URL url=ReadFile.class.getResource(fileName);

try {

// creating file object based on the url

File file = new File(url.toURI());

// creating scanner on file

sc=new Scanner(file);

// check if file has another line to be read

while(sc.hasNextLine()) {

// read another line and print that

System.out.println(sc.nextLine());

}

} catch (URISyntaxException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Add a comment
Know the answer?
Add Answer to:
How do I create a simple java program file that opens and reads a file using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

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

  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • This isa Java program Create file, file 1, with the following values: 3 4 35 20...

    This isa Java program Create file, file 1, with the following values: 3 4 35 20 -43 17 -10 6 7 -2 13 1 2 3 Write a program in Java to do the following: Open "file 1" and read the first two values, rand c, which are supposed to indicate the dimensions of the two-dimensional array whose values are given in the file after the values of r and c. Create a two-dimensional array of size r times c....

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • The purpose of this assignment is to create a program that works like a simple html...

    The purpose of this assignment is to create a program that works like a simple html file converter. Your program will read a text file and will produce an html file that can be viewed using a web browser, like Mozilla Firefox or Internet Explorer. Write code that converts an input file, myfile, into a simple Hyper Text Markup Language (HTML) file, myfile.html. In order to do this, you will read the input file, myfile, from the beginning to the...

  • I need to write a program in java that reads a text file with a list...

    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); } //...

  • Assignment Modify the code Binary File IO Example Code. The code reads and prints information abo...

    In java Assignment Modify the code Binary File IO Example Code. The code reads and prints information about a BMP file. Modify the code so that it also prints the resolution of the BMP file (both width and height) and the number of bits per pixel. Do not use any Java library or third-party library code that reads BMP files. You must read the file using simple Java code and extract the information, similar to the code in the example....

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT