Main (string[] args) {Copy ();}static void Copy () {Reader reader = null;BufferedReader br = null;writer writer = null;BufferedWriter bw = NULL;try {Part I: Preparing to read data from a file to a programreader = new FileReader (New File ("D://a.txt"));Create a buffered stream wrapper object readerbr = new BufferedReader (reader);Part two: Preparing to write to a file from a program; writing to an object w
InputStreamReader do not define the variable , the objects that are generated by new are used only once.d) the difference between the three ways is whether the FileInputStream and InputStreamReader objects are used only once, whether they need to define their object variables, and the individual's coding habits.e) But to be aware of exception handling, FileInputStream (file) throws Notfilefoundexception,If you use the Surround method (trycatch) processing, should be used in the second way, so
One, Java read and store file data streamJava reads a file, which is actually the process of converting a byte stream in a file into a character flow output to the screenThis involves two classes: InputStreamReader and OutputStreamWriterinputstreamreader: Convert byte streams to character streamOutputStreamWriter: Converting character flow to byte streamInputStreamReader inheriting reader classOutputStreamWriter inheriting writer classSecond,
Basic summary of Flow Java/ioA common code discussion through one line: New BufferedReader (New InputStreamReader (system.in))Java IO is based on the stream concept, what is a stream, as a beginner,As I understand it, the bits that are transmitted between applications, which can already be thought of as fluids, can be thought of as currents, so what should be the
FileInputStream by opening a connection to the actual file, which is specified by the pathname name in the file system.Reader|--bufferedreader|___inputstreamreader|__filereaderBufferedReader: Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows. Construction Method SummaryBufferedReader (Reader in)Creates a buffered character input stream that uses the default size inp
by opening a connection to the actual file, which is specified by the pathname name in the file system.Reader|--bufferedreader|___inputstreamreader |__filereaderBufferedReader: Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.Construction Method SummaryBufferedReader (ReaderInCreates a buffered character input stream that uses the default size input buffer.Buffered
have been confused FileReader FileInputStream InputStreamReader bufferedreader The difference between the connection, each write read the document Java program is online Baidu. This issue was discussed in depth today.First, the sourceJava.io has two abstract class InputStream and Reader, the above classes are inherited by these two super classes. The difference between them is that InputStream is the input
[Java Basics] InputStream, InputStreamReader, BufferedReader, inputstream, and reader
In Java, the above three classes are often used to process data streams. The following describes the differences between the three classes and their usage.
InputStream: A super class of all byte input streams. It is generally used as a subclass: FileInputStream and so on, whi
character into a portion of an array. A String readLine () - reads a line of text. - BooleanReady () the determines whether this stream is ready to be read. - voidReset () - Resets the stream to the latest markup. - LongSkipLongN) +Skips a character.(3)BufferedReader Use code example:1 Packagecn.itcast_05;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.FileReader;5 Importjava.io.IOException;6 7 /*8 * BufferedReader9 * Reads text from the cha
= br.readline ()) = null) {System.out.println (str);//At this point, Str saves a line of string}This should make it possible to get one line without losing characters.Although the writing IO aspect of the program is not much, but Bufferedreader/bufferedinputstream has been used several times, the reason is:
It has a very special method: ReadLine (), especially convenient to use, each read back is a row, save a lot of manual splicing buffer t
------ Solution ---------------------------------------------------------- respectively call the following two methods to see what is output
Java code
Public static void readgbkcodingfile (string filename) {// read the content of the GBK file bufferedreader BR = NULL; try {BR = new bufferedreader (New inputstreamreader (New fileinputstream (filename ), "GBK")
If you want to receive data of any length and avoid garbled characters, you can useBufferedreaderClass
Public class bufferedreader extends Reader
Because the input data may contain Chinese characters, the complete stream is used here. Bufferedreader reads content from the buffer. All input bytes are stored in the buffer zone.
System. In itself represents inputstream (byte stream). Currently, it is req
formed in memory, and all of the content is temporarily stored in memory before output, so buffers are used.If you do not want to output the contents of the character stream when you close it, use the writer's Flush () method.The principle of character streamJava supports character stream and byte stream, the character stream itself is a special kind of byte stream, the reason is to have a character stream, because there are a lot of characters in Java
) {System.out.println (str); } reader.close (); Br.close (); } }Run, first of all the "Buffered.txt" in the D-disk, the file content is:Then take a look at the console output:1234234534564567No problem, output the contents of the file. Note two points:1, write with BufferedWriter, write content will be placed in the buffer, until you encounter close (), flush () the content will be written to the file once. Also note the order of close (), must first close the BufferedWriter, and th
, so buffers are used.If you do not want to output the contents of the character stream when you close it, use the writer's Flush () method.The principle of character streamJava supports character stream and byte stream, the character stream itself is a special kind of byte stream, the reason is to have a character stream, because there are a lot of characters in Java operation, so there is a character stream. The conversion of byte stream and chara
Java Tour (25)--file copy, character stream buffer, Bufferedwriter,bufferedreader, copy file via buffer, readline working principle, custom ReadLine
Let's continue IO for the last space
I. Text copying
Read and write all said, we look at the other operations, we first look at the replication
The principle of replication: In fact, the file data under the C drive is store
Use Java BufferedReader to read files from the network to local, to be stored in a database, or to be saved to local Java code1. Download the contents of the network file into StringBuffer/** 从网络地址url下载文件读成字符串 * @param downloadUrl 文件的网络地址 * @return */public static StringBuffer downloadFromUrl(String downloadUrl) {
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.