Java Read file: text file

Source: Internet
Author: User
Tags readline string to file

It is generally read or written in a serial way. In general, the input stream is used to read the contents of the file into memory, and the output stream is used to write out the information in memory to the file. These classes are located under the Java.io package.
The input and output classes and methods are often the corresponding

Text file

Learn how to read and write text files, and then see how to read and write binary files. Java.ioreader is used to read characters, and its subclass FileReader is used to read text files.
FileReader Open the file under the specified path. The path delimiter for a file can be represented by "\ \" or "/".

"\ \" is a Windows-style notation because special characters in the string are escaped, so a slash is represented by a two slash.

New // hit shipowner text file

"/" is the path of the Linux style, because it does not need to be escaped, so the forward slash just need to write one.

New // Open a text file

If there is a pile of bricks to move, one can not finish, will not take a brick at a time, will try to get a few more pieces. If there are many internal
To read, not to read one byte at a time, but to read as many bytes as possible at a time to the cache.

New // Open a text file

BufferedReader br = new BufferedReader (FR); Cache Read
String Line;
while (line = Br. ReadLine ()) = null) {//read into file by row
System.out.println (line);
}
Fr.close (); Close a text file

The input stream reads the data from the hard disk into random access memory, or RAM, for short. OK
Build the BufferedReader based on the input stream, as shown in the implementation code below.

// file name New Filelnputstream (new// Open input stream // Cache read-in data new BufferedReader (  New InputStreamReader (file, "GBK"));

For loop reads a file by line

// file name New Filelnputstream (new File); " Open input stream // Cache read-in data new bufferedreader (file, "GBK"= in.readline ();  while NULL  = in.readline ();} In.close ();

In general, writer writes the content immediately to the hard disk. If you want to call the Write method multiple times, the bulk write efficiency
Higher. Similar to group buying, the price of group buying is often lower than the price of a single item. You can use caching to speed up file writes.

// using the default cache size New BufferedWriter (new  FileWriter (fileName)); Bw.write ("Hello,china// write line feed Bw.write ("hello,world!"  // write the contents of the cache to a file

When you write data using BufferedWriter, you finally need to call the Close method of BufferedWriter. If you do not close
File, the data in the cache may be lost, and the data written to the file is incomplete.

The complete code that writes the text by the specified encoding is shown below.

/** Write String to file * * @param content String * * @param filename filename * * @param encoding encoding */     Public Static voidWriteToFile (string content, String fileName, String encoding) {Try{FileOutputStream fos=NewFile0utputstream (fileName); OutputStreamWriter OSW=Newoutputstreamwriter (FOS, encoding); BufferedWriter BW=NewBufferedWriter (OSW);            Bw.write (content);        Bw.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }

If there is already a word on the blackboard, you can choose to erase the existing words on the blackboard, or you can continue after the original text
Write. If a file already exists, you can append the new content to the end, or you can write the new content from scratch, which is
Overwrite write. The construction method of FileWriter differs from these two writing methods.

// FileWriter construction method. Boolean throws IOException

where mode = False indicates overwrite write, mode = True indicates append write. In order to avoid conflict, at a moment only
Can have a thread to write a file.

Java Read file: text file

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.