[转帖]Java IO文件操作经典例子_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3370 | 回复: 0   主题: [转帖]Java IO文件操作经典例子        下一篇 
    本主题由 红与黑 于 2013-2-27 14:44:09 版块置顶
caohang
注册用户
等级:少校
经验:877
发帖:78
精华:0
注册:2013-1-31
状态:离线
发送短消息息给caohang 加好友    发送短消息息给caohang 发消息
发表于: IP:您无权察看 2013-2-17 9:54:34 | [全部帖] [楼主帖] 楼主

IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想。
在公司没活干,复习了一下IO,发现很多都忘记了,所以写的不好,只够初学用。我把我复习过程中写的代码贴出来,大家共同学习,并请多指教指教哈。顺便一起讨论IO
1、文件拷贝

1234567891011121314151617181920212223242526try{ File inputFile = newFile(args[0]); if(!inputFile.exists()) { System.out.println("源文件不存在,程序终止"); System.exit(1); } File outputFile = newFile(args[1]); InputStream in = newFileInputStream(inputFile); OutputStream out = newFileOutputStream(outputFile); bytedate[] = newbyte[1024]; inttemp = 0; while((temp = in.read(date)) != -1) { out.write(date); } in.close(); out.close(); } catch(FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }


 try {
      File inputFile = new File(args[0]);
      if (!inputFile.exists()) {
            System.out.println("源文件不存在,程序终止");
            System.exit(1);
      }
      File outputFile = new File(args[1]);
      InputStream in = new FileInputStream(inputFile);
      OutputStream out = new FileOutputStream(outputFile);
      byte date[] = new byte[1024];
      int temp = 0;
      while ((temp = in.read(date)) != -1) {
            out.write(date);
      }
      in.close();
      out.close();
} catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
} catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
}


2、java读文件:实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数,除此之外没有其他字符

try {
      BufferedReader in = new BufferedReader(new FileReader(file));
      line = in.readLine();
      while (line != null) {
            char[] ch = line.toCharArray();
            for (int a=0;a<ch.length;a++) {
                  if (Character.isLetter(ch[a])) {//统计字母
                        i++;
                  } else if (Character.isDigit(ch[a])) {//统计数字
                        j++;
                  } else if (Character.isWhitespace(ch[a])) {//统计空格
                        f++;
                  }
            }
            line = in.readLine();//统计行数
            k++;
      }
      in.close();
      System.out.println("字母有:" + i + "  数字有:" + j + "  空格有:" + f + "  行数:" + k);
      System.out.println("aa一共出现了" + check("D://a.txt") + "次");
} catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
      }


12345678910111213141516171819202122232425262728293031String fileName = "D:/date.java.bak"; // String fileName = "D:/test.qqq"; String line; inti = 0, j = 0, f = 0, k = 0; try{ BufferedReader in = newBufferedReader(newFileReader(fileName)); line = in.readLine(); while(line != null) { // System.out.println(line); charc[] = line.toCharArray(); for(inti1 = 0; i1 < c.length; i1++) { // 如果是字母 if(Character.isLetter(c[i1])) i++; // 如果是数字 elseif(Character.isDigit(c[i1])) j++; // 是空格 elseif(Character.isWhitespace(c[i1])) f++; } line = in.readLine(); k++; } in.close(); System.out .println("字母:"+ i + ",数字:"+ j + ",空格:"+ f + ",行数:"+ k); } catch(IOException e) { e.printStackTrace(); }



3、 从文件(d:\test.txt)中查出字符串”aa”出现的次数

 int count = 0;
try {
      BufferedReader in = new BufferedReader(new FileReader(file));
      StringBuilder sb = new StringBuilder();
      while (true) {
            String str = in.readLine();
            if (str == null)
            break;
            sb.append(str);
      }
      Pattern p = Pattern.compile("aa");
      Matcher m = p.matcher(sb);
      while (m.find()) {
            count ++;
      }
      System.out.println("aa一共出现了" + count + "次");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();


}
1234567891011121314151617181920212223try{ BufferedReader br = newBufferedReader(newFileReader( "D:\\test.txt")); StringBuilder sb = newStringBuilder(); while(true) { String str = br.readLine(); if(str == null) break; sb.append(str); } Pattern p = Pattern.compile("aa"); Matcher m = p.matcher(sb); intcount = 0; while(m.find()) { count++; } System.out.println("\"aa\"一共出现了"+ count + "次"); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); }


123456789101112131415161718192021222324252627282930try{ BufferedReader br = newBufferedReader(newFileReader(newFile( "d:\\2.txt"))); StringBuffer sb = newStringBuffer(); while(true) { String str = br.readLine(); if(str == null) { break; } sb.append(str + "、"); } String str = sb.toString(); String s[] = str.split("、"); doubled[] = newdouble[s.length]; for(inti = 0; i < s.length; i++) { d[i] = Double.parseDouble(s[i]); } for(inti = 0; i < d.length; i++) { System.out.println(d[i]); } br.close(); } catch(FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论