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(); } }
新闻热点
疑难解答