Answers for "read line method in java"

1

find in line java

// Java program to illustrate the 
// findInLine() method of Scanner class in Java 

import java.util.*; 
import java.util.regex.Pattern; 

public class GFG1 { 
	public static void main(String[] argv) 
		throws Exception 
	{ 

		try { 

			// Get the string to be searched 
			String s = "Geeksforgeeks has Scanner Class Methods"; 

			// Print the string 
			System.out.println("Target String:\n" + s); 

			// create a new scanner 
			// with the specified String Object 
			Scanner scanner = new Scanner(s); 

			// finds a pattern of any 5 letter plus "for" 
			System.out.println("\nAny 5 letter plus for : "
							+ scanner.findInLine( 
									Pattern.compile(".....for"))); 

			// close the scanner 
			scanner.close(); 
		} 
		catch (IllegalStateException e) { 
			System.out.println("Exception thrown : " + e); 
		} 
	} 
}
Posted by: Guest on October-27-2020
1

java read lines from file

Scanner sc = null;
        try {
            File file = new File("myfile.txt"); // java.io.File
            sc = new Scanner(file);     // java.util.Scanner
            String line;
            while (sc.hasNextLine()) {
              line = sc.nextLine();
              // process the line
            }
          }
          catch(FileNotFoundException e)
          {
              e.printStackTrace();
          }
          finally {
            if (sc != null) sc.close();
          }
Posted by: Guest on March-12-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language