Java read txt file, new txt file, write TXT file

Source: Internet
Author: User

1. Create TXT file

Just write according to normal logic.

First define a file given a path--to determine if the file does not exist on this path--and if it doesn't exist, it will be created automatically based on the program prompts. Try/catch

  

  

2. Read TXT file

Idea 1:

Get a file path

Create a new file with this path to determine if the file exists in a normal file format.

Create a reading buffer with this path: Fileinputstream-->inputstreamreader-->bufferedreader

Row by line to determine whether the content is empty, to accumulate the read result on a string (result)

Releasing buffer resources

  

Idea 2

Similar to the idea 1, just the buffer reader is constructed in a different way, see the Red Line section, Filereader-->bufferedreader

  

3. Write TXT file

Idea 1:

To write a file, you need to first determine if the file exists, and if it does not exist, create a

Write to the content using the methods in the FileWriter class, control the Write form with the append parameter, append write when Append is true, overwrite write when Append is False

Close File

  

Idea 2

Make the following adjustments to the above code, i.e. using BufferedWriter

  

  

Java read txt file, new txt file, write TXT file

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.