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. SpringMVC是什么?

    一,首先是一个MVC框架. 在web模型中,MVC是一种很流行的框架,通过把Model,View,Controller分离,把较为复杂的web应用分成逻辑清晰的几部分,是为了简化开发,减少出错.还是为 ...

  2. Git下载、更新、提交使用总结

    Git使用总结 1.下载代码到本地 1.1指定存储文件路径 1.运行git-bash.exe 2.指定盘符:cd f:work 1.2下载代码 命令:$ git clone <版本库的网址> ...

  3. 学习sql基础注入的方法

    作为一个初学者的我,经学习发现基础真的十分重要, 这个随笔是写给我自己的希望我能坚持住 当然,我也希望对其他人有点帮助 在sql注入的过程中,我越发感觉那些基础函数的重要性 其实我感觉sql注入其实就 ...

  4. python课程day_2-->总结-->字符串功能

    =======================课程大纲=======================> 基本数据类型 - 整数 - 布尔值 - 字符串 - 列表 - 元组 - 字典 - 集合 工 ...

  5. coding.net及git的使用方式

    一般部分测试的公司里可能会用到代码管理工具,这里可能不得不推荐coding.net和git这个工具,类似于svn,不过用命令行的情况多点 这里简单介绍下 1.建立coding.net 首先新建一个co ...

  6. 如何为mysql添加、启动服务

    1.点击"开始",输入cmd,以管理员身份运行: 2.进入mysql的bin目录,执行 mysqld --install,然后执行 net start mysql :

  7. java图片上传(mvc)

    最近有开始学起了java,好久没写文章了,好久没来博客园了.最近看了看博客园上次写的图片上传有很多人看,今天在一些篇关于java图片上传的.后台接收用的是mvc.不墨迹了,直接上图. 先看目录结构.i ...

  8. JAVA的Condition详解

    Condition 将 Object 监视器方法(wait().notify()和notifyAll())分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待 s ...

  9. nginx+gridfs+mongodb 配置访问png图片显示无法加载问题

    上传文件后,浏览器中请求:http://<nginx server ip>:<port>/gfs/<my file> 浏览器出现"无法打开页面" ...

  10. C++ 大作业 超市收银系统

    #include<iostream> #include<fstream> #include<string> #include<iomanip> #inc ...