Java one-point input and output techniques

Source: Internet
Author: User

Input:

Format 1:scanner sc = new Scanner (system.in);

Format 2:scanner sc = new Scanner (new Bufferedinputstream (system.in));

When the amount of data is large, format 2 is faster.

Split method:

String str = Scanner.next ();

string[] Date = Str.split ("/");

System. out. println (date[0]+ "year" +date[1]+ "month" +date[2]+ "Day");

DecimalFormat

NumberFormat

Keep the number of decimal place!!

Bigdecimal.setscale (2,BIGDECIMAL.ROUND_HALF_UP);

Sub-string:

String ss = "Wo Ai Zhou";

System. out. println (ss.substring (3,7));

Intercept substring from 3rd to 7th (starting from 0, 7th digit not counted)

The result is: Ai Z

High accuracy:

BigInteger and BigDecimal are places where Java has a big advantage as a tool for solving problems.

BigInteger bign = new BigInteger (st,base); Base indicates the binary

Binary conversion

It's also a place where Java has a great advantage.

int a = Integer. parseint ("12534", 8);

System. out. println (a);

String res = Integer. toString (12345,16);

System. out. println (res);

File input and output

Only the more efficient methods are recorded here:

Write file:

Fliewriter class:

FileWriter FW = new FileWriter ("My.txt");

Fw.write ("Hello");

Fw.close ();

Read the file:

File File = new file ("My.txt");

FileInputStream FIS = new fileinputstream (file);

InputStreamReader Isreader = new inputstreamreader (FIS);

BufferedReader br = new bufferedreader (Isreader);

String SS;

while ((ss = Br.readline ()) =null) {

System. out. println (ss);

}

or directly:

File File = new file ("My.txt");

BufferedReader br =

New BufferedReader ( new InputStreamReader (new fileinputstream (file));

String SS;

while ((ss = Br.readline ()) =null) {

System. out. println (ss);

}

(I actually prefer this kind of, when writing not to be muddy)

Java one-point input and output techniques

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.