Operation code for files and stream streams in Java

Source: Internet
Author: User
Tags readline

Application code and explanation of Fileread method in 1.Java

Package example2;
Import Java.io.FileReader;
Import java.io.IOException;
Class fileread{
public static void Main (string[] args) throws ioexception{
Create a FileWriter object
FileWriter fw=new FileWriter ("File01.txt", true);
FileReader fr=new FileReader ("File01.txt");
If the file that is written already exists, it overwrites the write
If you want to append the write, you
Call the Write method to write data to a file
In Windows, the line break is \ r \ n
In MacOS, the previous line break is \ r and is now \ n
In the Linux operating system, the line break is \ n
/* int ch;
while ((Ch=fr.read ())!=-1) {
System.out.println ((char) ch);
}
Fr.close (); */
Bulk reading of data using character arrays
Char [] buf=new char[3];
int Len;
while ((Len=fr.read (BUF))!=-1) {
String Str=new string (Buf,0,len);
System.out.println (str);
}
/*
* Methods for writing data in 5 of FileWriter
* Fw.write (int a); referencing ASCII and Unicode tables
* Fw.write (String str) writes the full string
* Fw.write (String Str,int offset,int count) writes part of a string
* Fw.write (char[] Array) writes the complete character array
* Fw.write (char[] array,int offset,int count) write part of the character array
*
* */
}
}

The use and explanation of FileWrite method in Java

Package example2;
import Java.io.FileWriter;
import java.io.IOException;
class filewrite{
Public static void Main (string[] args) throws IOException {
//Create a FileWriter object
//filewriter fw=new FileWriter ("File01.txt", true);
FileWriter fw=new FileWriter ("File01.txt");
//If the written file already exists, it overwrites the write
//If you want to append a write, you want to
//Call the Write method to write data to the file
//In Windows, the line break is \ r \ n
//In MacOS, the previous line break is \ r and is now \ n
//In the Linux operating system, line break is \ n
    /*
* Methods for writing data in 5 of FileWriter
* Fw.write (int a); referencing ASCII and Unicode tables
* Fw.write (String str) writes the full string
* Fw.write (String Str,int offset,int count) writes part of a string
* Fw.write (char[] Array) writes the complete character array
* Fw.write (char[] array,int offset,int count) write part of the character array
     *
     * */
fw.write ("... daniahhsh");
fw.write ("zhaoliying\r\n");
String str= "Sgsgjajhgs";
fw.write ();
Fw.write (str,0,5);
char[] array= {' V ', ' k ', ' n ', ' V ', ' G '};
Fw.write (array,1,3);
fw.write ("... bajabjd");
//Call the Close method to close the file
fw.close ();
    }
}

How to use Bufferread in Java and its explanation

Package example2;

Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import java.io.IOException;

public class bufferread{
BufferedReader Extra gives you a ReadLine () function to read a string in full line.
Bufferwriter provides a newline function newline () in addition;
Bufferwriter is similar to FileWriter, Bufferwriter has a 8192-length char[] character array buffer inside it.
Each time the data is written, a character is added like a buffer. If the buffer array is full, then write to the file in the hard disk uniformly.
If the array is still not full when it is finally closed, then the remaining valid portions are written to the hard disk file.
public static void Main (string[] args) throws IOException {
Start by creating a common filewriter
FileReader fw = new FileReader ("File02.txt");
This ordinary FileWriter object is passed to the BufferedWriter constructor method.
BufferedReader bw=new BufferedReader (FW);
Reading a single string
/*int ch;
while ((Ch=bw.read ())!=-1) {
System.out.println ((char) ch);
}*/

Reads a character array (reads multiple characters from the buffer at once)
/*int Len;
Char[] Buf=new char[3];
while ((Len=bw.read (BUF))!=-1) {
String Str=new string (Buf,0,len);
System.out.println (str);
}*/
Reads a string with an entire line
String Line;
while ((Line=bw.readline ())!=null) {
System.out.println (line);
}
Bw.close ();
}
}

Application and explanation of Bufferwrite method in Java

Package example2;

Import Java.io.BufferedWriter;
Import Java.io.FileWriter;
Import java.io.IOException;

public class Bufferwrite {
Bufferwriter provides a newline function newline () in addition;
Bufferwriter is similar to FileWriter, Bufferwriter has a 8192-length char[] character array buffer inside it.
Each time the data is written, a character is added like a buffer. If the buffer array is full, then write to the file in the hard disk uniformly.
If the array is still not full when it is finally closed, then the remaining valid portions are written to the hard disk file.
public static void Main (string[] args) throws IOException {
Start by creating a common filewriter
FileWriter FW = new FileWriter ("File02.txt");
This ordinary FileWriter object is passed to the BufferedWriter constructor method.
BufferedWriter bw=new BufferedWriter (FW);
Bw.write ("SDFGHJKL");
Bw.newline ();//This function modifies the use of newline characters depending on the operating system you are using. Very flexible, advocated in the development of common.
Bw.write ("ETRYTUYXCV");
Bw.close ();
}
}

The use and explanation of stream flow

Package example3;

Import java.util.ArrayList;
Import Java.util.stream.Stream;

public class Liststream {
The stream in Java8 is actually an interface object for a stream
A java.util.stream.stream<t>; is provided in the JDK
public static void Main (string[] args) {
1. Get a stream from a collection
arraylist<string> list = new arraylist<> ();
List.add ("Editorialcommittee, 15");
List.add ("Luhan, 18");
List.add ("Shen, 20");
List.add ("Subaojuan, 10");
stream<string> stream = List.stream ();

Gets a stream based on an array
string[] Arraystr = new string[] {"Dfgha", "Tyui", "ERQQ"};
Stream<string> stream1 = Stream.of (ARRAYSTR);

integer[] Arrayint = new integer[] {10, 20, 50, 40, 60};
stream<integer> stream3 = Stream.of (arrayint);
List.stream (). Map (S-s.split (",") [1]). Map (Integer::p arseint). Filter (n-n >= 15)
. ForEach (System.out::p rintln);

After you get the stream, you can use the mapping method: map (lambda expression for conversion)
stream<integer> Arrayintger = List.stream (). Map ((String str), {
int num = integer.parseint (str);
return num;
});

Lambda expression
S->system.out.println (s);
Object Name Call member method
System.out::p rintln ();
If there are many elements in the flow, then one person is slow and inefficient.
Elements in the convection are handled by multiple people. called "Concurrency"
How to obtain a concurrent stream (. Parallelstream ())
System.out.println ("================");
This is the same effect as calling
Using concurrent streams will cause multiple people to preempt positions at the same time, and the order will be disrupted, but no duplicates will occur.
List.stream (). Parallel (). ForEach (System.out::p rintln);

System.out.println ("================");

List.parallelstream (). ForEach (System.out::p rintln);
Note: When using concurrent stream processing, there are several people who operate without a tube at the same time, and the JDK handles it.
As long as it is used correctly, there will be no more than one element at a time.
}
}

Some of the most useful methods in Java are categorized. Is the case code of the success of my actual operation. You can avoid taking a few detours.

The revolution has not yet succeeded, comrades still need to work hard!

Operation code for files and stream streams in Java

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.