Learning experience (1) formal parameters for class name + variable, array container, stream insights

Source: Internet
Author: User
Tags throw exception

Learning Java in the January and a half, for the formal parameters I have not been able to understand before is the class name + variable name, now I have my own way of understanding. Is that if you want to use the member variables and the member methods of a class, you can pass the object of this class in. As long as the object name (variable). A member variable, or. method can be used. Call this method as soon as the new object is passed in as a formal parameter.

For that ArrayList container, I was also a half-solution. In fact, it is very well understood, it is a container, the first step to use it is to create a container, ArrayList ArrayList = new ArrayList (); Here you can actually use generics so that the container can store the data type, or it can store reference types. arraylist<t> ArrayList = new arraylist<t> ();  The second step, where I'm giving an example of storing reference types, we can create student objects, Student s1 = new Student ("cx1", 1);  Student S1 = new Student ("Cx2", 2);  There are two data to be stored here.   The third step: is to add data to the container, the container ArrayList has a method add (), so Arraylist.add (S1); Arraylist.add (S2); The fourth step is to iterate over this array, where we can use the enhanced for.

for (element type element name: array name)


System.out.println (element name);

In these steps, the functionality of the storage is resolved.

And that input and output stream is quite easy to understand. Many kinds of streams, but careful observation, the steps are similar. Here I will list 2: Character output stream and character input stream. (Fileread,filewrite)

Write FileWrite and write Fileread first.

Because there will be a need to throw exception handling, and to determine whether the stream is empty, the initial value of the stream is defined as null first.

FileWrite FW = NULL;

FileReader FR = null;

Define the path where the file is located, with "D:\\text.txt" as an example

String pash = "D:\\text.txt"

Define a string you want to write

String str = "SFDSGFDHGDJ";

Create the FileWrite object and give him the path.

FW = new Fliewrite (pash);

Start writing

Fw.write (str);

After writing, remember to force the data in the buffer to quit, so as not to remain

Fw.flush ();

The next step is to write the input stream, create the input stream object, and pass the path of the file to him.

FR = new FileReader (pash);

Because it's a character stream, we can define an array of characters to read at a certain length.

Char []ch = new char[1024];

Because we want to judge whether there are characters that let us read, by reading the API, we know that when there is no data to read, it returns the

Yes-1 So we can define a variable to receive the return value, and set a stringbuffer, as a condition of the judgment of the loop.

object to splice the data that is being read.

int Len;

while (-1!) = (len = fr.read (ch)))


Buffer.append (New String (CH, 0, Len));


System.out.println (buffer.tostring ())

Finally remember to turn off the stream and write him in the fanlly.


Determine if the stream is empty, not empty, and then shut him down.

if (FW!) = null)


Fw.close ();


if (FW!) = null)


Fr.close ();



Learning experience (1) formal parameters for class name + variable, array container, stream insights

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.