What is the advantage of using Scanner class in java?

Տարի: The Scanner class in java is a new util class that was added to Java 1.5.This class is really useful for handling input from a user.The Scanner class can do all that a BufferedReader classdo with same efficiency.But there are some advantages over the BufferedReader class.The Scanner class can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice and also do forward scanning of the underlying stream disregarding the delimiter.The Scanner is not thread safe but BufferedReader is thread safe.

Այնպես որ եզրակացությունն, if you are performing simple read operation the BufferedReader is good enough.If you need to parse some file then Scanner is the natural choice.

Example code:-
--------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class ScannerDemo {
հասարակական ստատիկ անվավեր հիմնական(Լարային args []){
Scanner scanner = null;
փորձել {
scanner = new Scanner(new File(“D:/Workspace/TestProj/src/scanner_class_19_09_2012.txt”));
} բռնելը (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream out = null;
փորձել {
out = new PrintStream(new File(“ScannerOutfile.txt”));
} բռնելը (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ժամանակ(scanner.hasNextLine()){
String line = scanner.nextLine();
line = line.trim();
եթե(line.length() > 0)
out.println(line);
}
}
}

============================================= ============================================== Գնեք լավագույն տեխնոլոգիական գրքերը Amazon- ում,en,Էլեկտրականություն CT շագանակագույն էլեկտրաէներգիա,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share