I/O flow

Source: Internet
Author: User

I/O flow

File is used to represent files (directories), which means that programmers can manipulate files and directories on the hard disk in the program through the file class. The file class is used only to represent information (name, size, and so on) of files (directories) and cannot access the contents of a file.

File f=new File(".");System.out.println(f.getCanonicalPath());f.getCanonicalPath()---获取当前路径创件一个新的文件File d=new File(f,"demo");if(!d.exists()){d.mkdir();}创件新的文件对象File text=new File(d,"text");if(!text.exists()){text.createNewFile();    }  获取文件File f=new File("c:");    System.out.println(f.getCanonicalPath());    f.getCanonicalPath();    File[] files=f.listFiles();    System.out.println(Arrays.toString(files))  

The FileFilter class is the filter for the action file

File f=new File("src/test");    File[] files=f.listFiles(new FileFilter() {        @Override        public boolean accept(File pathname) {            return pathname.getName().endsWith(".java")&&pathname.isFile();        }    });    for(File file:files){        System.out.println(file.getName());    }

The Listfiles () method will pass each file in Dir to the Accept () method for detection and, if true, as the returned result element of the method

//FileInputStream 输入流        FileInputStream f=new FileInputStream(new File("d:/hello.txt"));        byte[] b=new byte[1024];//存储读取类容        int n=0;        //返回-1 read        while((n=f.read(b))!=-1){            System.out.println(n);            System.out.println(new String(b));        }        f.close();  

Bufferedinputstream && Bufferedoutputstream provides buffers for IO operations, usually with buffered streams when opening files for write-ring reads, which are designed to improve IO (input-output) performance.

File f=new File("c:/wode.txt");byte[] b=new byte[(int) f.length()];BufferedInputStream bu=new BufferedInputStream(new FileInputStream(f));int i=0;    while((i=bu.read(b))!=-1){    System.out.println(new String(b,"gbk"));   
Character Stream:

Character Stream (Reader Writer) characters, processing one character at a time (Unicode encoding) primarily using them two FileReader FileWriter
Two common classes in the reader class: FileReader: Reads a file as a stream of characters, basically consistent with fileinputstream usage.
BufferedReader: Reads the stream into the buffer, then reads from the buffer, and provides the ReadLine () method to read the data from the entire line in the text file.

Two common classes in the writer class: FileWriter: Writing a file as a stream of characters, basically consistent with fileoutputstream usage. BufferedWriter: Outputs the buffer to the stream.

实验1:写出文件:    File file=new File("c:\\test.txt");    FileWriter fw=newFileWriter(file);//在这里加入参数true会实现对文件的续写    String s="举头望明月,低头写代码";//\r\n为window下边的换行    fw.write(s);    fw.close();实验2:读取文件    File file=new File("c:\\test.txt");    BufferedReader br=new BufferedReader(new FileReader(file));    String strline =br.readLine();    System.out.println(strline);--但是我们发现,这样只读取了一行数据File file=new File("c:\\test.txt");    FileReader fr=new FileReader(file);    int ch=0;    while((ch=fr.read())!=-1){        System.out.print((char)ch);    }
Serialization of objects

It is simply to save the state of various objects in memory and to read the state of the saved objects again.

Object serialization is the conversion of an object to a byte sequence, which in turn is called deserialization. 1) serialized Stream (ObjectOutputStream), is the filter stream

 ObjectOutputStream writeObject(Object)    序列化对象 ObjectInputStream readObject()        对象的反序列化 Users u=new Users(); u.setUserId(1); u.setUserName("zhangsan"); u.setUserPwd("admin"); File file=new File("c://wode.txt"); FileOutputStream fo=new FileOutputStream(file); ObjectOutputStream ops=new ObjectOutputStream(fo); ops.writeObject(u);读取:    File file=new File("c://wode.txt");    FileInputStream fis=new FileInputStream(file);    ObjectInputStream ois=new ObjectInputStream(fis);    Users u=(Users) ois.readObject();    System.out.println(u.getUserId()+" "+u.getUserName());  

2) The serialized Interface (Serializable) object must implement a "serialization interface" to serialize, otherwise the exception will not be serialized! Serializable is an empty interface, there is no method, only as an identity of the serialization

Serialization (serialization) is a process of describing objects in a sequence of bytes, and deserializing deserialization is a process of re-building these bytes into an object. The Java Serialization API provides a standard mechanism for handling object serialization. Here you can learn how to serialize an object, when to serialize and the Java serialization algorithm, and we use an instance to demonstrate how the serialized bytes describe the information of an object.

I/O flow

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.