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(); 
		} 
	} 
}

 

 

你可能感兴趣的:(java,C++,c,C#)