Question

how do you load text from a txt file in java

how do you load text from a txt file in java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.*;
import java.util.Scanner;
public class ReadText {
  public static void main(String[] args) throws IOException {
    String fileName;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter file name: ");
    fileName = scanner.nextLine();
    File file = new File(fileName);
    try {
      // open the file
      Scanner scan = new Scanner(file);
      // if file has contents to read
      // loop to read the contents of the file
      String s;
      while(scan.hasNext()) {
        s = scan.nextLine()+" ";
        System.out.println(s);
      }
      scan.close();
      // close the Scanner object
    } catch (FileNotFoundException e) {
      // catch the exception if file not present
      System.out.println("File " + file.getName() + " not present ");
      System.exit(0);
    }
  }
}
Add a comment
Know the answer?
Add Answer to:
how do you load text from a txt file in java
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
  • In Java code, for a topological sort assignment: How I load a .txt file and use...

    In Java code, for a topological sort assignment: How I load a .txt file and use its information to populate a hashtable that maps IDs to characters, and add the IDs as nodes in the graph.

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • Write a method in JAVA for accepting a text file as input and If the file...

    Write a method in JAVA for accepting a text file as input and If the file is of type .txt, then calculate and return the number of words in that txt file. If the file is of type .csv, then return the number of rows and number of columns in that csv file. If the file is of any other type, then return a message stating that file type is not supported. Note: Use StringBuilder wherever applicable.

  • Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance)...

    Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance) containing the following integer values, one per line: ​12 5 13 56 90 52 82 52 Write a Java program that reads these values from the file and displays their sum on the screen.

  • i have the text file to pull info off of. This is for JAVA. Must use...

    i have the text file to pull info off of. This is for JAVA. Must use arrays, JOptionPane, methods, and be able to pull and count from the file that is provided to me by my instructor. The file is a txt file that has over 1000 test scores on it without commas. Spaces are used. Please help. Standardized Test Scores           A local high school is giving a standardized test to all of its students. There are 50...

  • Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...

    Write a java program to read from a file “input4_02.txt” an integer N (0? N ? 100) and then read N double numbers from this file to an array. Write to file “output4_02.txt” this array but in the reverse order and also the sum of all elements in the array. For example File: “input4_02.txt” 5 1.30 2.22 4.00 17.60 3.14 File “output4_02.txt” 3.14 17.60 4.00 2.22 1.30 Sum = 28.26

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • In Python how to write from an array to a .txt file so that each row...

    In Python how to write from an array to a .txt file so that each row in the array is one row in the text file. Fill in all blank spaces in the array with a 0.

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    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.

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

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