jianghu的gravatar头像
jianghu 2014-02-12 10:30:13

java IO文件操作简单基础入门例子,IO流其实没那么难

1. [代码]1、文件拷贝 

01try {
02            File inputFile = new File(args[0]);
03            if (!inputFile.exists()) {
04                System.out.println("源文件不存在,程序终止");
05                System.exit(1);
06            }
07            File outputFile = new File(args[1]);
08            InputStream in = new FileInputStream(inputFile);
09            OutputStream out = new FileOutputStream(outputFile);
10            byte date[] = new byte[1024];
11            int temp = 0;
12            while ((temp = in.read(date)) != -1) {
13                out.write(date);
14            }
15            in.close();
16            out.close();
17        } catch (FileNotFoundException e) {
18            // TODO Auto-generated catch block
19            e.printStackTrace();
20        } catch (IOException e) {
21            // TODO Auto-generated catch block
22            e.printStackTrace();
23        }


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

01String fileName = "D:/date.java.bak";
02        // String fileName = "D:/test.qqq";
03        String line;
04        int i = 0, j = 0, f = 0, k = 0;
05        try {
06            BufferedReader in = new BufferedReader(new FileReader(fileName));
07            line = in.readLine();
08            while (line != null) {
09                // System.out.println(line);
10                char c[] = line.toCharArray();
11                for (int i1 = 0; i1 < c.length; i1++) {
12                    // 如果是字母
13                    if (Character.isLetter(c[i1]))
14                        i++;
15                    // 如果是数字
16                    else if (Character.isDigit(c[i1]))
17                        j++;
18                    // 是空格
19                    else if (Character.isWhitespace(c[i1]))
20                        f++;
21                }
22                line = in.readLine();
23                k++;
24            }
25            in.close();
26            System.out
27                    .println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);
28        } catch (IOException e) {
29            e.printStackTrace();
30        }


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

01try {
02            BufferedReader br = new BufferedReader(new FileReader(
03                    "D:\\test.txt"));
04            StringBuilder sb = new StringBuilder();
05            while (true) {
06                String str = br.readLine();
07                if (str == null)
08                    break;
09                sb.append(str);
10            }
11            Pattern p = Pattern.compile("aa");
12            Matcher m = p.matcher(sb);
13            int count = 0;
14            while (m.find()) {
15                count++;
16            }
17            System.out.println("\"aa\"一共出现了" + count + "次");
18        } catch (FileNotFoundException e) {
19            e.printStackTrace();
20        } catch (IOException e) {
21            e.printStackTrace();
22        }


4. [代码]4、 三种方法读取文件     

01try {
02           // 方法一
03           BufferedReader br = new BufferedReader(new FileReader(new File(
04                   "D:\\1.xls")));
05           // StringBuilder bd = new StringBuilder();
06           StringBuffer bd = new StringBuffer();
07           while (true) {
08               String str = br.readLine();
09               if (str == null) {
10                   break;
11               }
12               System.out.println(str);
13               bd.append(str);
14           }
15           br.close();
16           // System.out.println(bd.toString());
17           // 方法二
18           InputStream is = new FileInputStream(new File("d:\\1.xls"));
19           byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()
20                   + "")];
21           is.read(b);
22           System.out.write(b);
23           System.out.println();
24           is.close();
25           // 方法三
26           Reader r = new FileReader(new File("d:\\1.xls"));
27           char c[] = new char[(int) new File("d:\\1.xls").length()];
28           r.read(c);
29           String str = new String(c);
30           System.out.print(str);
31           r.close();
32       } catch (RuntimeException e) {
33           // TODO Auto-generated catch block
34           e.printStackTrace();
35       } catch (FileNotFoundException e) {
36           // TODO Auto-generated catch block
37           e.printStackTrace();
38       } catch (IOException e) {
39           // TODO Auto-generated catch block
40           e.printStackTrace();
41       }


5. [代码]5、三种方法写文件     

01try {
02           PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"));
03           BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
04                   "d:\\1.txt")));
05           OutputStream os = new FileOutputStream(new File("d:\\1.txt"));
06           // 1
07           os.write("ffff".getBytes());
08           // 2
09           // bw.write("ddddddddddddddddddddddddd");
10           // 3
11           // pw.print("你好sssssssssssss");
12           bw.close();
13           pw.close();
14           os.close();
15       } catch (IOException e) {
16           // TODO Auto-generated catch block
17           e.printStackTrace();
18       }


6. [代码]6、读取文件,并把读取的每一行存入double型数组中     
 

01try {
02            BufferedReader br = new BufferedReader(new FileReader(new File(
03                    "d:\\2.txt")));
04            StringBuffer sb = new StringBuffer();
05            while (true) {
06                String str = br.readLine();
07                if (str == null) {
08                    break;
09                }
10                sb.append(str + "、");
11            }
12            String str = sb.toString();
13            String s[] = str.split("、");
14            double d[] = new double[s.length];
15            for (int i = 0; i < s.length; i++) {
16                d[i] = Double.parseDouble(s[i]);
17            }
18            for (int i = 0; i < d.length; i++) {
19                System.out.println(d[i]);
20            }
21            br.close();
22        } catch (FileNotFoundException e) {
23            // TODO Auto-generated catch block
24            e.printStackTrace();
25        } catch (IOException e) {
26            // TODO Auto-generated catch block
27            e.printStackTrace();
28        }


最代码官方编辑于2014-2-12 15:15:25


打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友