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

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();

        }

时间: 2024-08-26 09:44:19

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

Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)

Linux下的IO/文件操作练习,知识虽然简单 但是往往基础容易被忽略,偶尔的练习是有必要的. 练习printf /************************************************************************* > File Name: printf.c > Author: > Mail: > Created Time: Wed 11 Feb 2015 01:08:15 AM PST ***********************

java常见文件操作

收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } //2.创建文件 //impor

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

I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本能力.本章IO类的使用就从读写文件开始. 1 文件操作 文件(File)是 最常见的数据源之一,在程序中经常需要将数据存储到文件中,例如图片文件.声音文件等数据文件,也经常需要根据需要从指定的文件中进行数据的读取.当然, 在实际使用时,文件都包含一个的格式,这个格式需要程序员根据需要进行设计,读取已

Java学习之==&gt;IO文件操作体系

一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializable.掌握了这些IO的核心操作那么对于Java中的IO体系也就有了一个初步的认识了. File(文件类):主要用来描述文件或目录的属性,例如:文件大小,修改文件名,删除文件,判断文件所在路径等. InputStream(字节输入流):抽象类,基于字节的输入操作,是所有输入流的父类.定义了所有输入流

python速成第二篇(小爬虫+文件操作+socket网络通信小例子+oop编程)

大家好,由于前天熬夜写完第一篇博客,然后昨天又是没休息好,昨天也就不想更新博客,就只是看了会资料就早点休息了,今天补上我这两天的所学,先记录一笔.我发现有时候我看的话会比较敷衍,而如果我写出来(无论写到笔记本中还是博客中,我都有不同的感觉)就会有不同的想法,我看书或者看资料有时候感觉就是有一种惰性,得过且过的感觉,有时候一个知识想不通道不明,想了一会儿,就会找借口给自己说这个知识不重要,不需要太纠结了,还是去看下一个吧,然后就如此往复下去,学习就会有漏洞,所以这更加坚定了我写博客来记录的想法.

java的文件操作类File

java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: 1 package test.file.IO; 2 3 import java.io.*; 4 public class TestFile{ 5 public static void main(String[] args){ 6 //1.File类是java用来处理文件的类,它可以获取文件的所有属性 和 完成文件所需要的所有操作 7 // 所以使用的第一步就是new一个File对象 8 // Fil

JAVA的文件操作【转】

11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本能力.本章IO类的使用就从读写文件开始. 11.3.1 文件操作 文件(File)是 最常见的数据源之一,在程序中经常需要将数据存储到文件中,例如图片文件.声音文件等数据文件,也经常需要根据需要从指定的文件中进行数据的读取.当然, 在实际使用时,文件都包含一个的格式,这个格式需要程序员根据

java中文件操作《一》

在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.FileOutputStream.reader.writer.FileReader.FileWriter等等,其中对文件的操作又分为两大类,一类是字符流,一类是字节流.所谓的字符流是以字节(8b)为单位进行读/写,字符流即使用字符为单位读/写,java使用unicode编码,一个字符两个字节,下面分别对

java之文件操作

一.File类 1.1File类说明 File类关心的是磁盘上文件的存储,描述的是一个文件或者文件夹.在整个java.io包中,File类是唯一一个与文件本身操作有关的类,所谓的文件本身指的是文件的创建,删除,重命名,修改文件日期,获取文件的大小等. 1.2体验File类 package com.boxiaoyuan.www; import java.io.File; public class Demo03 { public static void main(String[] args) { F