关闭

java.io的详细代码

526人阅读 评论(0) 收藏 举报

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

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:254452次
    • 积分:3853
    • 等级:
    • 排名:第8280名
    • 原创:112篇
    • 转载:64篇
    • 译文:0篇
    • 评论:52条
    文章分类
    最新评论