Java read-write file and input processing by line

Source: Internet
Author: User
Tags readline

First, we look at Python is very simple:

1. Read the file:

1 with open ("/path/file","R") as fr:2 for in       fr.readlines ():3         do_somethings (line)

2. Write files:

1 with open ("/path/file","w/a") as fr:  2     fr.write ("ssssssss")

Second, the above knowledge a primer, not the focus, or to learn the Java file read and write operations:

The most common is to read and write by line, of course, there will be a bit of other reading and writing methods:

1, read by line:

1File File =NewFile ("Absolute path");2BufferedReader BufferedReader =NewBufferedReader (Newfilereader (file));3String str =NULL;4  while(str = bufferedreader.readline ())! =NULL)  5{  6     System.out.println (str); 7 }8Bufferedreader.close ();

2, write by line:

1 New FileWriter ("Absolute path",true); 2 // true indicates append, otherwise overwrite write, overwrite write does not need true 3 filew.write ("\ntest\n"); 4 filew.close ();
1 list<string> b = files.readalllines (Paths.get ("/users/a003797/desktop/a1.txt")); 2  for (String item:b) {3    System.out.println (item)    ;     4 }

3. Other reading methods:

1 // Read all bytes: 2 byte [] A = Files.readallbytes (Paths.get ("/users/a003797/desktop/a1.txt"));

Third, input processing

1 ImportJava.io.*;2 3  Public classtest{4      Public Static voidMain (String args[])throwsIOException {5BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); 6          while(true){7String string =br.readline ();8 System.out.println (string);9         }Ten     } One}

Java read-write file and input processing by line

Related Article

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.