A brief description of Java Basic Learning flow and demonstration case, buffer method to read and write files, use of File class object, serializable Tag interface (6)

Source: Internet
Author: User

1. Flow Summary and demonstration cases
The input stream and the output stream are relative to the memory device.
Read data from peripherals into memory: input
Writes the number of memory to the peripheral: output.
The origin of the character stream:
Actually is: The byte stream reads the literal bytes data, does not direct operation but first checks the specified encoding table. Gets the corresponding text.
The text is being manipulated. Simple: Byte stream + encoding table
Two top-level parent class for byte stream:
1,inputstream 2,outputstream.
Two top-level parent classes of character streams:
1,reader 2,writer

String FilePath = "F:\\demo.txt";
FileWriter fw = new FileWriter (filepath,true);
/*
* writes the data by calling the write (string) method in the writer object.
*
* The data is actually written to the temporary storage buffer.
*
*/
Fw.write ("Abcdehahaha");

Fw.write ("Xixi");
/*
* Refreshes to write the data directly to the destination.
*/
Fw.flush ();

//1, creating a stream object that reads character data.
/*
* When you create a read stream object, you must explicitly read the file. Be sure that the file exists.
*
* Associates an existing file with a read stream.
*/
FileReader fr = new FileReader ("Demo.txt");
The first method reads
//int CH3 = 0;
while ((Ch3=fr.read ())!=-1) {//Use the Read () loop to read the
//system.out.print ((char) CH3) by a character-by-string;
}

//Second way read
//char[] buf = new char[1024];
int len = 0;
//while ((Len=fr.read (BUF))!=-1) {
//system.out.println (new String (Buf,0,len));
}
//Third Way read
BufferedReader bufr = new BufferedReader (FR);
String line = null;
while ((Line=bufr.readline ())!=null) {
System.out.println (line);
}
Fr.close ();

2. Use buffer method to read and write files
Read demo, write Demowrite
String Readfilepath = "F:\\demo.txt";
String Writefilepath = "F:\\demowrite.txt";
FileReader FR = new FileReader (Readfilepath);
BufferedReader bufr = new BufferedReader (FR);
FileWriter FW = new FileWriter (Writefilepath);
BufferedWriter BUFW = new BufferedWriter (FW);
String line = null;
while (line = Bufr.readline ()) = null) {
Bufw.write (line);
Bufw.newline ();
Bufw.flush ();
}
Bufw.close ();
Bufr.close ();

3.LineNumberReader
String Readfilepath = "F:\\demo.txt";
FileReader FR = new FileReader (Readfilepath);
LineNumberReader LNR = new LineNumberReader (FR);
String line = Null;//linenumberreader has a function more than BufferedReader, which is to return the current row number. In addition Setlinenumber can change the line number, but can not change the position of reading.
Lnr.setlinenumber (1);//getlinenumber () The line number shown will start at 2
while ((Line=lnr.readline ())!=null) {
System.out.println (Lnr.getlinenumber () + ":" +line);
}
Lnr.close ();


Use of 4.File class objects
File File = new file ("f:\\");
System.out.println ("Getfreespace:" +file.getfreespace ());//Get the remaining space of the F-disk
System.out.println ("Gettotalspace:" +file.gettotalspace ());
System.out.println ("Getusablespace:" +file.getusablespace ());
//
file[] files = file.listroots ();//Get the computer's disk
//
for (File file2:files) {
//
System.out.println (file2);
//
}

Rename, rename Demo.txt to Demowrite1
File F1 = new file ("F:\\demo.txt");
File F2 = new file ("F:\\demowrite1.txt");
Boolean B = F1.renameto (F2);
System.out.println ("b=" +b);


The creation and deletion of files.
File File = new file ("F:\\file.txt");
/*
* Unlike the output stream, if the file does not exist, it is created and is not created if the file exists.
*/
//
Boolean B = File.createnewfile ();
System.out.println ("b=" + b);
FileWriter writer = new FileWriter (file);
Writer.write ("AAA");
Writer.flush ();
Writer.close ();

Boolean B1 = File.delete ();

System.out.println ("b1=" + B1);

5.Serializable Marking interface
Serializable: Used to add an ID number to the class being serialized.
Used to determine if a class and an object are the same version.
public class Person implements Serializable {
/**
* Transient: Non-static data does not want to be serialized can be modified with this keyword.
*/
Private static final long serialversionuid = 9527l;
private transient String name;
private static int age;

Public person (String name, int age) {
THIS.name = name;
This.age = age;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

public int getage () {
return age;
}

public void Setage (int.) {
This.age = age;
}
}
ObjectOutputStream oos = new ObjectOutputStream (New FileOutputStream ("Obj.object"));
The serialization of the object. The serialized object must implement the Serializable interface.
Oos.writeobject (New Person ("Xiao Qiang", 30));
Oos.close ();

ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("Obj.object"));
The deserialization of the object.
Person P = (person) ois.readobject ();
System.out.println (P.getname () + ":" + p.getage ());
Ois.close ();


A brief description of Java Basic Learning flow and demonstration case, buffer method to read and write files, use of File class object, serializable Tag interface (6)

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.