Recently in the Bi Xiangdong Teacher's Java basic video IO part of the knowledge of the explanation. Because IO this piece of knowledge is very important, and small knowledge dot many, method also more, tonight make a simple summary for later review.
IO Overview The method of the stream of words the method of byte th
Java IO (input output) stream I. IO stream Overview 1. IO stream is used for data transmission between devices. 2. Data Operations in Java are performed in a stream-based manner. 3. Classification: 1) data by Operation: byte streams (available in the early stage, can transfe
(file);} }
}
}
Test class:
public class Fileutilstest {public
static void Main (string[] args) throws IOException {
fileutils.listdirectory (New File ("D:\\iostudy"));
}
Run Result:
Third, the use of Randomaccessfile class
Randomaccessfile:java provides access to the contents of a file, either by reading a file or by writing a file.
Randomaccessfile supports random access to files and can access any location in the file.
Note
File ("Love 2.txt ");//file file1 = new File (" C:\\users\\hasee\\desktop\\qq20171107180120.png ");//note suffix, with and without a different file File1 = new File (SRC); File File2 = new file (dest);//Two formats can be copied over fileinputstream FIS = null; FileOutputStream fos = null;try {fis = new FileInputStream (file1); fos = new FileOutputStream (file2); byte[] B = new Byte[5] ; int len;while (len = Fis.read (b))! =-1) {//fos.write (b); Two types of errors: Fos.write (b, 0, b.length),
Direct code: Read the keyboard input data code demo:Import Java.io.ioexception;import Java.io.InputStream; /** * Read the keyboard input data and print it on the console. * * The keyboard itself is a standard input device, * for Java, */public class ReadKey {/** * @param args * @throws IOException */public static void Main (string[] args) throws IOException {//R
) {System.out.println (num); System.out.println (TEMP1); System.out.println (NewString (temp1,0, num)); } f.close ();; }}All of these are examples of node streams, and here's an example of a process flowExample 5: Process flow Public classFileinputstreamtest { Public Static voidMain (string[] args)throwsIOException {printstream ps=NULL; Try { //Create a node streamFileOutputStream fs =NewFileOutputStream ("D:\\javapro\\iojava\\xin.txt"); //wrapping FileOutputStrea
IO ExampleThe following is a presentation of the file file.txtHello world! Hello nerd!Let's look at an example of a file read:Import java.io.*;public class test{public static void Main (string[] args) {try{BufferedReader br =New BufferedReader (New FileReader ("file.txt"));String line = Br.readline ();while (line! = null) {System.out.println (line);line = Br.readline ();}Br.close ();}catch (IOException e) {
be converted to the use of characters, you can specify the code table for file data.4. One instance, moving files1 Public Static voidCopy (string src, string desc)throwsException {2 3File Srcfile =NewFile (SRC);4File Descfile =NewFile (DESC);5InputStream in =NULL;6OutputStream out =NULL;7 Try {8in =NewFileInputStream (srcfile);9out =NewFileOutputStream (descfile);Ten intLen =-1; One byte[] bytes =New byte[1024 * 1024]; A while(len = in.read (bytes))
FilesYou can use FileInputStream or filereader to read into a file based on binary, which is based on text. They do not need to read the entire file, but can read bytes, or characters sequentially, in the order in which they are stored.Using FileOutputStream or FileWriter, data can be written out to a file and stored in the order in which the data is written.Randomaccessfile can jump into a file, read data, or write data to a file. Random Access does not mean that you are actually reading data f
or spread across the entire directory tree, displaying the folders under the current file, and the list of files, which can traverse the entire directory tree. For example environment:->dirhome-->java ——— >jdk ——— >document.doc-->spring ——— >spring.zip ——— >springapi ———— > Document.doc-->pass.txt output:./java./java/jdk./ja
surrogate that has no previous high surrogate. This class always replaces the error proxy element and the non-mapped character sequence with the default substitution sequence for the character set. If you need more control over the encoding process, you should use the Charsetencoder class. View Code Decoding, the solution to what we can understand, that is, to solve the string A simple example is as follows: (not particularly important, not specifically introduced here)@Test Public voidtest1
); }Catch(IOException e) {E.printstacktrace (); }//Print the data in the array as a stringSystem. out. println ("Your input is:"+NewString (BUF,0, Len)); }}Results:However, for the above scenario, if you enter Chinese, the output is garbled. Please pay attention to them. The reason for this is that bytes are read. If a character is read, there is no garbled condition.In fact, for the current possible garbled situation, we can not be entangled.
a new FileReader that gives the file read.
Common methods:
int read ()Read a character
int read (char[] cbuf)Reads a character into an array.
void Close ()Closes the stream and frees any system resources associated with it.
FileWriterConstruction Method:
FileWriter (File file)Constructs a FileWriter object for a file object.
FileWriter (File File, Boolean append)Constructs a FileWriter object for a file object.
FileWriter (String fileName)Constructs a
-------Android Training, Java training, look forward to communicating with you! ----------ConceptFlow: A stream is an abstraction of a sequence of bytes, a data source that can be continuously read, and a stream that can be continuously written to the data.IO stream: Used to process data on the deviceCommon equipment: Hard disk, memory, keyboard input, etc.Classification of IO streams:1, according to the pr
----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------First,I. Three ways to classify IO streams1. Divided by Flow direction: input stream and output stream2. Different data units by stream are divided into: byte stream and character stream3. According to the function of the stream is divided
OverviewIO streams are used to process data transfer between devicesHow Java operates on data by streamingThe objects that Java uses to manipulate the flow are in the IO packageStream-by-operation data is divided into: byte stream and character streamFlow is divided into: input stream and output stream by different flowcommon base classes for
Java.io.FileInputStream;Import java.io.FileNotFoundException;Import Java.io.FileOutputStream;Import java.io.IOException;PublicClassA3 {PublicStaticvoidMain(string[] args) {A3 A2 =New A3 ();The path of the Cat.png picture in the computer D drive String filePath1 ="D:/cat.png";The path to the Cat.png picture in the E-drive of the computer String filePath2 ="E:/cat.png";Copy file A2.copyfile (filePath1, filePath2); }/** * File Copy *@param filepath_old: The path of the file to be copied *@param fi
Introduction:
About Java IO flow is very common, basically every project will be used, each encounter is to find a search on the internet, and tried. Last time a colleague asked me to read a Java file, I suddenly Meng the first reaction is to go online to find, although also can find, but their total feeling is not very practical, so today took time to see some
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.