java.io的详细代码

闲言少序,直入正题,看代码. 

String s1 = new String() ;
  String s2 = new String();
  try {
   BufferedReader bd = new BufferedReader(new FileReader("E://SortActionBean.xml")) ;
   while((s2=bd.readLine())!=null){
    s1+=s2+"/n";
   }
   System.out.println("222=="+s1);
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  try {
   FileInputStream str = new FileInputStream("E://hibernate.cfg.xml");
   int c = 0 ;
   while((c = str.read())!=-1){
    System.out.print((char)c);
   }
   StringReader str1 = new StringReader("dsadsadsa3213213213防盗锁防盗锁反对司法第三分速度");
   int d = 0 ;
   while((d = str1.read())!=-1){
    System.out.println((char)d);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  System.out.println(System.getProperty("user.dir"));;
  
  String sDir = File.separator;
  System.out.println(sDir);
  String url = File.separator+"a.txt";
  File file = new File(sDir,url);
  try {
   file.createNewFile();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  try {
   //往文件里面写数据,前提是要修改的文件必须存在。否则会抛异常
   BufferedWriter fw = new BufferedWriter(new FileWriter("f://aaa.txt"));//创建文件。但是不能创建文件夹
   char c[] = {'我','们','是','中','国','人'};
   String str = " I am a chinese man ";
   fw.write(c);
   fw.write(str);
   fw.flush();
   fw.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  try {
      BufferedReader br = new BufferedReader(new FileReader("E://hibernate.cfg.xml"));
   String temp = "";
   while((temp = br.readLine()) != null){
    System.out.println(temp);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  try {
   BufferedReader in = new BufferedReader(new FileReader("f://aaa.txt"));
   BufferedWriter out = new BufferedWriter(new FileWriter("f://copy.txt"));
   String temp = "";
   while((temp = in.readLine())!=null){
    out.write(temp);
   }
   out.flush();
   out.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  try {
   //按字节读取,因为一个汉字占2个字节,所以拆分的时候会乱码的。因此读汉字的时候用reader
   FileInputStream in = new FileInputStream("f://aaa.txt");
   int i=0 , b;
   while((b=in.read())!=-1){
    System.out.println((char)b);
    i++;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  byte[] b = new byte[10];
  try {
   FileOutputStream out = new FileOutputStream("E://hibernate.cfg.xml");
   for(int i=0;i<10;i++){
    b[i] = (byte)(97+i);//转换成ascii码表示
    System.out.println("b[i]=="+b[i]);
   }
   out.write(b);
   out.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

具体的功能不想多说了,自己建立一个main函数测试下就知道了.呵呵.记得顶啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值