首页 > 编程 > Java > 正文

javaI/O流

2019-11-06 06:20:11
字体:
来源:转载
供稿:网友
package TestDemo;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class TestIO {		public void fisTest(){		try {			int data;			FileInputStream fis =new FileInputStream("D://test.txt");			fis.available();			while((data = fis.read())!=-1){				System.out.PRintln(data);			}			fis.close();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}		public void fosTest(){		String str ="hello world";		byte[] Words=str.getBytes();		try {			FileOutputStream fos = new FileOutputStream("D://test.txt",true);			fos.write(words, 0, words.length);		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}		public void frTest(){		try {			FileReader fr = new FileReader("D://test.txt");			BufferedReader br = new BufferedReader(fr);			String line = br.readLine();			while(line!=null){				System.out.println(line);				line=br.readLine();			}			br.close();			fr.close();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}		public void fwTest(){		try {			FileWriter fw = new FileWriter("D://test.txt");			BufferedWriter bw = new BufferedWriter(fw);			bw.write("hello");			bw.write("java");			bw.newLine();			bw.write("good");			bw.flush();			bw.close();		} catch (IOException e) {			e.printStackTrace();		}	}		public void dataTest() throws IOException{		FileInputStream fis = new FileInputStream("D://test.class");		DataInputStream dis = new DataInputStream(fis);		FileOutputStream fos = new FileOutputStream("D://temp.class");		DataOutputStream dos = new DataOutputStream(fos);		int temp;		while((temp=dis.read())!=-1){			dos.write(temp);		}		dos.close();		fos.close();		dis.close();		fis.close();	}	}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表