Java Stream Programming

 

1往文件中写入:

 

FileOutputStream BufferedOutputStream FileWriter 的使用方法。

 

 

 

 

import java.io.BufferedOutputStream;   
import java.io.File;   
import java.io.FileOutputStream;   
import java.io.FileWriter;   
  
public class TestTxtWriter {   
  
    public TestTxtWriter() {   
    }   
  
    public static void main(String[] args) {   
        FileOutputStream out = null;   
        FileOutputStream outSTr = null;   
        BufferedOutputStream Buff = null;   
        FileWriter fw = null;   
        int count = 100000;
        try {
        	
        	//=========== FileOutputStream ==============
            out = new FileOutputStream(new File("C:/FileOutputStream.txt"));   
            for (int i = 0; i < count; i++) {   
                out.write("Test FileOutputStream,BufferedOutputStream and FileWriter \r\n".getBytes());   
            }   
            out.close();
            
            //=========== BufferedOutputStream ==============
            outSTr = new FileOutputStream(new File("C:/BufferedOutputStream.txt"));   
            Buff = new BufferedOutputStream(outSTr);
            for (int i = 0; i < count; i++) {   
                Buff.write("Test FileOutputStream,BufferedOutputStream and FileWriter \r\n".getBytes());   
            }   
            Buff.flush();   
            Buff.close(); 
            
            //=========== FileWriter ==============
            fw = new FileWriter("C:/FileWriter.txt");
            for (int i = 0; i < count; i++) {   
                fw.write("Test FileOutputStream,BufferedOutputStream and FileWriter \r\n");   
            }   
            fw.close();
  
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                fw.close();   
                Buff.close();   
                outSTr.close();   
                out.close();   
            } catch (Exception e) {   
                e.printStackTrace();   
            }   
        }   
    }   
}

 

2、往文件中写入:

                FileOutputStream  PrintStream 结合使用。

                FileWriter  PrintWriter 结合使用。

import java.io.*;

class FileOutputDemo {
	public static void main(String args[]) { 
		FileOutputStream out; 
		PrintStream p; 
		try {
			out = new FileOutputStream("C:/Documents and Settings/cz84786/Desktop/temp/1.txt"); 
			p = new PrintStream( out ); 
			p.println ("This is written to a file");
			out.close();
			p.close(); 
		} catch (Exception e) { 
			System.err.println ("Error writing to file"); 
		} 
	}
}

 

import java.io.*; 

class FileWriteTest { 
	public static void main (String[] args) { 
		FileWriteTest t = new FileWriteTest(); 
		t.WriteMyFile(); 
	} 
	void WriteMyFile() { 
		try { 
			FileWriter fw = new FileWriter("C:/Documents and Settings/cz84786/Desktop/temp/1.txt"); 
			PrintWriter out = new PrintWriter(fw); 
			out.print("hi,this will be wirte into the file!"); 
			out.close();
			fw.close(); 
		} catch (IOException e) { 
			System.out.println("Uh oh, got an IOException error!"); 
			e.printStackTrace(); 
		} 
	} 
}

 

3、从文件中读:

                FileReader  BufferedReader  结合使用。

import java.io.*;
class FileReadTest {	
	public static void main (String[] args) { 
		FileReadTest t = new FileReadTest(); 
		t.readMyFile(); 
	} 
	void readMyFile() { 
		String record = null; 
		int recCount = 0; 
		try { 
			FileReader fr = new FileReader("C:/FileWriter.txt"); 
			BufferedReader br = new BufferedReader(fr); 
			record = new String(); 
			while ((record = br.readLine()) != null) { 
				recCount++; 
				System.out.println(recCount +" : "+ record); 
			} 
			br.close(); 
			fr.close(); 
		} catch (IOException e) { 
			System.out.println("Uh oh, got an IOException error!"); 
			e.printStackTrace(); 
		} 
	} 
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Stream is a sequence of elements that supports various operations to perform operations on the data. It is introduced in Java 8 and is used to perform bulk operations on collections of data, such as filtering, mapping, sorting, and reducing. Streams are designed to work with functional programming concepts, such as lambda expressions and method references. They provide a concise and easy-to-read syntax for performing complex operations on collections of data. Streams can be created from various sources such as arrays, lists, and collections, and can also be created dynamically using stream builder. Once a stream is created, operations can be performed on it to transform, filter, or aggregate the data. The two types of operations that can be performed on a stream are intermediate and terminal operations. Intermediate operations return a new stream and allow further operations to be performed on the data. Terminal operations are the final step in the stream pipeline and return a non-stream result, such as a value or a collection. Java Stream provides several built-in methods for performing common operations on collections, such as filter(), map(), flatMap(), reduce(), and collect(). It also supports parallel processing, allowing operations to be performed on multiple elements concurrently to improve performance. Overall, Java Stream provides a powerful and flexible way to work with collections of data, allowing developers to write more concise, readable, and efficient code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值