字节流:
FileInputStream读取,FileOutputStream输出
字节流使用数组缓冲区复制文件,最后得出所使用的时间
1 public class work2 { 2 public static void main(String[] args) { 3 FileOutputStream out=null; 4 FileInputStream in=null; 5 Date a=new Date(); 6 try { 7 out=new FileOutputStream("D:\\test\\d\\eclipse.zip"); 8 in=new FileInputStream("D:\\test\\eclipse.zip"); 9 byte[] b=new byte[1024*1024]; 10 int len=0; 11 12 while((len=in.read(b))!=-1) 13 { 14 out.write(b,0,len); 15 } 16 } catch (IOException e) { 17 18 e.printStackTrace(); 19 }finally{ 20 try { 21 out.close(); 22 } catch (IOException e) { 23 24 e.printStackTrace(); 25 } 26 try { 27 in.close(); 28 } catch (IOException e) { 29 30 e.printStackTrace(); 31 } 32 } 33 Date b=new Date(); 34 System.out.println(b.getTime()-a.getTime()); 35 } 36 }
字符流:
FileReader 字节流读取 FileWriter 字节流输出
字节流使用数组缓冲区复制文件
1 public class copy { 2 public static void main(String[] args) throws IOException { 3 FileReader fr=new FileReader("D:\\test\\a.txt"); 4 FileWriter fw=new FileWriter("D:\\test\\d\\a.txt"); 5 char[] ch=new char[2]; 6 int len=0; 7 8 while((len=fr.read(ch))!=-1) 9 { 10 fw.write(ch,0,len); 11 System.out.println((char)len); 12 fw.flush(); 13 } 14 fw.close(); 15 fr.close(); 16 } 17 }
转换流:
借助字节流写入方法,将字节流转换成字符流并且规定编码格式,例如编码格式为utf-8
FileOutputStream fo=new FileOutputStream("D:\\test\\d\\utf.txt");
OutputStreamWriter osw=new OutputStreamWriter(fo, "utf-8");
FileInputStream fi=new FileInputStream("D:\\test\\utf.txt");
InputStreamReader isr=new InputStreamReader(fi,"utf-8");
缓冲区:
//字符流缓冲区
FileReader fr=new FileReader("D:\\test\\aaa.txt");
BufferedReader br=new BufferedReader(fr);
//明确目的地
FileWriter fw=new FileWriter("D:\\test\\d\\aaa.txt");
BufferedWriter bw=new BufferedWriter(fw);
//字节流缓冲区
FileInputStream fi=new FileInputStream("D:\\codetool\\eclipse.zip");
BufferedInputStream bi=new BufferedInputStream(fi);
FileOutputStream fo=new FileOutputStream("D:\\test\\eclipse.zip");
BufferedOutputStream bo=new BufferedOutputStream(fo);
跨平台换行:.newLine();
读取一整行:readline();
Properties类
用于操作配置文件,后缀名只能是properties的文件
方法:
load(input字符流或字节流对象);
store(output字符流或字节流对象,描述信息);
写入:
创建对象:Properties prop = new Properties();
写入prop集合:prop.setProperty("周迅", "张学友");
创建字符流文件写入:FileWriter out = new FileWriter("prop.properties");
写入:prop.store(out,"描述");
取出:
创建集合:Properties prop = new Properties();
创建流对象:
FileInputStream in = new FileInputStream("prop.properties");
//FileReader in = new FileReader("prop.properties");
把流所对应文件中的数据 读取到集合中:
prop.load(in);
4,关闭流:
in.close();
5,显示集合中的数据:
System.out.println(prop);