《chapter07 补充1》由会员分享,可在线阅读,更多相关《chapter07 补充1(8页珍藏版)》请在金锄头文库上搜索。
1、import java.io.*;class T1 public static void main(String args) throws IOException FileReader f=new FileReader(“aa.txt“); int c; while(c=f.read()!=-1)System.out.print(char)c); 读取并输出指定文件的内容hello大家好读取并输出指定文件的内容import java.io.*;class T1 public static void main(String args) throws IOException FileInputSt
2、ream f=new FileInputStream(“aa.txt“); int c; while(c=f.read()!=-1)System.out.print(char)c); hello?实现接收键盘输入的数据,并写到指定文件中import java.io.*;class T2 public static void main(String args) throws IOException InputStreamReader isr=new InputStreamReader(System.in); FileWriter fw=new FileWriter(“tt.txt“); int
3、c=isr.read(); while(c!=-1) fw.write(c); c=isr.read(); fw.close(); 读取并输出指定文件的内容import java.io.*;public class T52 public static void main(String args) try FileInputStream fis=new FileInputStream(“aa.txt“); InputStreamReader isr=new InputStreamReader(fis); BufferedReader br=new BufferedReader(isr); Str
4、ing s=br.readLine(); while(s!=null) System.out.println(s); s=br.readLine(); catch(IOException e) 实现接收键盘输入的数据,并写到指定文件中import java.io.*; public class T53 public static void main(String args) try FileOutputStream fis=new FileOutputStream(“aa.txt“); OutputStreamWriter isr=new OutputStreamWriter(fis); Bu
5、fferedWriter bw=new BufferedWriter(isr); System.out.println(“请输入文件内容: “); BufferedReader br=new BufferedReader(new InputStreamReader( System.in); String s=br.readLine(); while(!s.equals(“exit“) bw.write(s); bw.newLine(); s=br.readLine(); bw.flush(); bw.close(); br.close(); catch(IOException e) 获取指定目
6、录下的文件及目录信息import java.io.*;class FileDirpublic static void main(String args)File f=new File(“c:“);File fs=f.listFiles();for(int i=0;it“+fsi.getName();非缓存的文件输入字节流 import java.io.*; import java.util.*;class T2 public static void main(String args) throws IOException FileInputStream f=new FileInputStrea
7、m(“aa.txt“); Date d1=new Date(); int c=f.read(); while(c!=-1)c=f.read(); f.close(); Date d2=new Date(); long t=d2.getTime()-d1.getTime();System.out.println(t); 缓存的文件输入字节流 import java.io.*; import java.util.*;class T2 public static void main(String args) throws IOException FileInputStream fi=new FileInputStream(“aa.txt“); BufferedInputStream bis=new BufferedInputStream(fi); Date d3=new Date(); int a=bis.read(); while(a!=-1)a=bis.read(); bis.close(); Date d4=new Date();long t2=d4.getTime()-d3.getTime(); System.out.println(t2);