Skip to main content

Java Program To Read Data From The Text File

Java Program To Read Data From The Text File 

=============================================================

How To Read Data From The Text File:


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadDataFromTextFile {

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

        FileReader fr = new FileReader("C:\\Users\\lenovo\\Desktop\\test.txt");
        // Read the File From the Location Present inside the PC / Disc.

        BufferedReader br = new BufferedReader(fr);

        String str;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }

        br.close();

    }

}

Note: In this Program, the test.txt file is stored in the Computer / Laptop on Disk.

output:
===========
Welcome To Java Programming!



Approach -2
==============

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadDataFromTextFile2 {

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

        File file = new File("C:\\Users\\lenovo\\Desktop\\test.txt");

        Scanner sc = new Scanner(file);

        // To Read Content From the File We need Loop Statement

        while (sc.hasNextLine()) // To check there are more lines or not
        {
            System.out.println(sc.nextLine()); // Actually used to read the lines
        }

        sc.close();

    }

}

output:
=========
Welcome To Java Programming!


Approach -3:
===============

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadDataFromTextFile3 {

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

        File file = new File("C:\\Users\\lenovo\\Desktop\\test.txt");

        Scanner sc = new Scanner(file);

        sc.useDelimiter("\\z");
        System.out.println(sc.next());
        sc.close();

    }

}

output:
============
Welcome To Java Programming!




Comments

Popular posts from this blog

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

Java 8 Coding Interview Questions | Asked In Many Interviews

 Q 1) Given A List . Write Java 8 Program to find Out 1st Element From List? ===================================================================== import java . util . Arrays ; import java . util . List ; public class FirstElementOfList {     // Given a List. Find First Element From the List       public static void main ( String [] args ) {                     List < Integer > list = Arrays . asList ( 10 , 15 , 24 , 7 , 24 , 74 , 10 , 7 );             list . stream (). findFirst (). ifPresent ( System . out :: println );     } } Q 2) Given A List ? Find Out the Count of All Element In A List? ============================================================================== import java . util . Arrays ; import java . util . List ; public class TotalNumberOfElements {     public static void main ( String [] args ) {       ...

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...