IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想。
今天整理了一份适合初学者学习的简单例子,让大家可以更深刻的理解IO流的具体操作。

1、文件拷贝

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读文件:实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数,除此之外没有其他字符

 
String fileName = "D:/date.java.bak";
        // String fileName = "D:/test.qqq";
        String line;
        int i = 0, j = 0, f = 0, k = 0;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            line = in.readLine();
            while (line != null) {
                // System.out.println(line);
                char c[] = line.toCharArray();
                for (int i1 = 0; i1 < c.length; i1++) {
                    // 如果是字母
                    if (Character.isLetter(c[i1]))
                        i++;
                    // 如果是数字
                    else if (Character.isDigit(c[i1]))
                        j++;
                    // 是空格
                    else if (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”出现的次数

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

4、 三种方法读取文件

try {
           // 方法一
           BufferedReader br = new BufferedReader(new FileReader(new File(
                   "D:\\1.xls")));
           // StringBuilder bd = new StringBuilder();
           StringBuffer bd = new StringBuffer();
           while (true) {
               String str = br.readLine();
               if (str == null) {
                   break;
               }
               System.out.println(str);
               bd.append(str);
           }
           br.close();
           // System.out.println(bd.toString());
           // 方法二
           InputStream is = new FileInputStream(new File("d:\\1.xls"));
           byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()
                   + "")];
           is.read(b);
           System.out.write(b);
           System.out.println();
           is.close();
           // 方法三
           Reader r = new FileReader(new File("d:\\1.xls"));
           char c[] = new char[(int) new File("d:\\1.xls").length()];
           r.read(c);
           String str = new String(c);
           System.out.print(str);
           r.close();
       } catch (RuntimeException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
 

5、三种方法写文件

 
try {
           PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"));
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
                   "d:\\1.txt")));
           OutputStream os = new FileOutputStream(new File("d:\\1.txt"));
           // 1
           os.write("ffff".getBytes());
           // 2
           // bw.write("ddddddddddddddddddddddddd");
           // 3
           // pw.print("你好sssssssssssss");
           bw.close();
           pw.close();
           os.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
 
 

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

try {
            BufferedReader br = new BufferedReader(new FileReader(new File(
                    "d:\\2.txt")));
            StringBuffer sb = new StringBuffer();
            while (true) {
                String str = br.readLine();
                if (str == null) {
                    break;
                }
                sb.append(str + "、");
            }
            String str = sb.toString();
            String s[] = str.split("、");
            double d[] = new double[s.length];
            for (int i = 0; i < s.length; i++) {
                d[i] = Double.parseDouble(s[i]);
            }
            for (int i = 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();
        }

java IO文件操作简单基础入门例子,IO流其实没那么难的更多相关文章

  1. Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录

    Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录 这里先简单的贴下常用的方法: File.separator //当前系统文件分隔符 File.pathSeparator // ...

  2. java常见文件操作

    收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...

  3. (Unity)XML文件读写与IO文件操作类使用介绍

    using System.Xml;                //xml文件操作命名空间 #region 写入操作 void WriteXMLFile(string _fileName) { Xm ...

  4. Java api 入门教程 之 JAVA的文件操作

    I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...

  5. 【java】文件操作java.io.File

    package 文件操作; import java.io.File; import java.io.IOException; public class TestFile { public static ...

  6. Java学习之==>IO文件操作体系

    一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...

  7. 14、Java文件操作stream、File、IO

    1.文件操作涉及到的基本概念 File File类 是文件操作的主要对象中文意义就是 文件 顾名思意 万物皆文件,在计算上看到的所有东西都是文件保存,不管是你的图片.视频.数据库数据等等都是按照基本的 ...

  8. Java 基本文件操作

    Java 文件操作 , 这也是基于Java API 操作来实现的. 文件是操作系统管理外存数据管理的基本单位, 几乎所有的操作系统都有文件管理机制. 所谓文件, 是具有符号名而且在逻辑上具有完整意义的 ...

  9. java的文件操作类File

    java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: package test.file.IO; import java.io.*; public clas ...

随机推荐

  1. JQuery速成大法

    什么是JQuery呢,很多都是只闻其名. jQuery是一个快速.简洁的JavaScript框架,是一个优秀的JavaScript代码库.jQuery设计的宗旨是"write Less,Do ...

  2. 有些arp请求报文中为什么会有目的mac地址(不使用广播地址)

    有些arp请求报文中为什么会有目的mac地址(不使用广播地址) 最近做实验,注意到局域网内大部分的arp包的以太网头部目的mac地址并不是广播地址,并且包内的目的mac地址字段并不是全0,而是目的ip ...

  3. 【算法系列学习】巧妙建图,暴搜去重 Counting Cliques

    E - Counting Cliques http://blog.csdn.net/eventqueue/article/details/52973747 http://blog.csdn.net/y ...

  4. poj1159二维树状数组

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  5. js 正则表达式验证

    验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...

  6. 关于开发微信小程序后端linux使用xampp配置https

    关于开发微信小程序后端linux使用xampp配置https 背景 由于最近开发微信小程序,前后端交互需要使用https协议,故需要配置https服务 服务器环境 服务器系统 ubuntu 环境 xa ...

  7. 通过一步步创建sharded cluster来认识mongodb

    mongodb是目前使用非常广泛的nosql(not only sql)之一,在db engines上排名非常靠前,下图是5月份的排名: 可以看到前面四个都是传统的关系型数据库,而mongodb在no ...

  8. Archive for required library:xxxxx/spring-beans-3.2.4.RELEASE.jar in project XXXXX cannot be read or is not a valid ZIP file

    今天在导入maven项目的时候在problems视图中报错: Archive for required library:xxxxx/spring-beans-3.2.4.RELEASE.jar in ...

  9. Ubuntu 完全卸载MySQL 重装步骤

    sudo rm /var/lib/mysql/ -R 删除mysql的数据文件   sudo rm /etc/mysql/ -R 删除mqsql的配置文件   sudo apt-get autorem ...

  10. 如何用C#完成控制台日历?

    本题目的最终要就是根据用户输入的年和月在控制台输出单月的日历信息,附加范围年在1900-2100之间,月的范围在1-12之间,当用户输入不在范围时要给予错误信息提示:已知条件是1900年1月1日为星期 ...