`

java中的IO整理(4)

    博客分类:
  • java
 
阅读更多

打印流

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 使用PrintStream进行输出
 * */
import java.io.*;
 
class hello {
    public static void main(String[] args) throws IOException {
        PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
                + File.separator + "hello.txt")));
        print.println(true);
        print.println("Rollen");
        print.close();
    }
}

 

 

【运行结果】:

 

true

 

Rollen

 

当然也可以格式化输出

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 使用PrintStream进行输出
 * 并进行格式化
 * */
import java.io.*;
class hello {
    public static void main(String[] args) throws IOException {
        PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
                + File.separator + "hello.txt")));
        String name="Rollen";
        int age=20;
        print.printf("姓名:%s. 年龄:%d.",name,age);
        print.close();
    }
}

 

 

【运行结果】:

 

姓名:Rollen. 年龄:20.

 

 

 

使用OutputStream向屏幕上输出内容

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 使用OutputStream向屏幕上输出内容
 * */
import java.io.*;
class hello {
    public static void main(String[] args) throws IOException {
        OutputStream out=System.out;
        try{
            out.write("hello".getBytes());
        }catch (Exception e) {
            e.printStackTrace();
        }
        try{
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

【运行结果】:

 

hello

 

 

 

输入输出重定向

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
 
/**
 * 为System.out.println()重定向输出
 * */
public class systemDemo{
    public static void main(String[] args){
        // 此刻直接输出到屏幕
        System.out.println("hello");
        File file = new File("d:" + File.separator + "hello.txt");
        try{
            System.setOut(new PrintStream(new FileOutputStream(file)));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        System.out.println("这些内容在文件中才能看到哦!");
    }
}

 

 

【运行结果】:

eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
 
/**
 * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息
 * */
public class systemErr{
    public static void main(String[] args){
        File file = new File("d:" + File.separator + "hello.txt");
        System.err.println("这些在控制台输出");
        try{
            System.setErr(new PrintStream(new FileOutputStream(file)));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        System.err.println("这些在文件中才能看到哦!");
    }
}

 

 

【运行结果】:

 

你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
 * System.in重定向
 * */
public class systemIn{
    public static void main(String[] args){
        File file = new File("d:" + File.separator + "hello.txt");
        if(!file.exists()){
            return;
        }else{
            try{
                System.setIn(new FileInputStream(file));
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }
            byte[] bytes = new byte[1024];
            int len = 0;
            try{
                len = System.in.read(bytes);
            }catch(IOException e){
                e.printStackTrace();
            }
            System.out.println("读入的内容为:" + new String(bytes, 0, len));
        }
    }
}

 

 

【运行结果】:

 

前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!

 

 

 

BufferedReader的小例子

 

注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:

 

 

BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));

 

 

下面给一个实例:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 使用缓冲区从键盘上读入内容
 * */
public class BufferedReaderDemo{
    public static void main(String[] args){
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        String str = null;
        System.out.println("请输入内容");
        try{
            str = buf.readLine();
        }catch(IOException e){
            e.printStackTrace();
        }
        System.out.println("你输入的内容是:" + str);
    }
}

 

 

运行结果:

 

请输入内容

 

dasdas

 

你输入的内容是:dasdas

 

 

 

Scanner

 

其实我们比较常用的是采用Scanner类来进行数据输入,下面来给一个Scanner的例子吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
/**
 * Scanner的小例子,从键盘读数据
 * */
public class ScannerDemo{
    public static void main(String[] args){
        Scanner sca = new Scanner(System.in);
        // 读一个整数
        int temp = sca.nextInt();
        System.out.println(temp);
        //读取浮点数
        float flo=sca.nextFloat();
        System.out.println(flo);
        //读取字符
        //...等等的,都是一些太基础的,就不师范了。
    }
}

 

其实Scanner可以接受任何的输入流

 

下面给一个使用Scanner类从文件中读出内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
 
/**
 * Scanner的小例子,从文件中读内容
 * */
public class ScannerDemo{
    public static void main(String[] args){
 
        File file = new File("d:" + File.separator + "hello.txt");
        Scanner sca = null;
        try{
            sca = new Scanner(file);
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        String str = sca.next();
        System.out.println("从文件中读取的内容是:" + str);
    }
}

 

【运行结果】:

 

从文件中读取的内容是:这些文件中的内容哦!

 

数据操作流DataOutputStreamDataInputStream

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class DataOutputStreamDemo{
    public static void main(String[] args) throws IOException{
        File file = new File("d:" + File.separator + "hello.txt");
        char[] ch = { 'A', 'B', 'C' };
        DataOutputStream out = null;
        out = new DataOutputStream(new FileOutputStream(file));
        for(char temp : ch){
            out.writeChar(temp);
        }
        out.close();
    }
}

 

 

A B C

 

现在我们在上面例子的基础上,使用DataInputStream读出内容

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
public class DataOutputStreamDemo{
    public static void main(String[] args) throws IOException{
        File file = new File("d:" + File.separator + "hello.txt");
        DataInputStream input = new DataInputStream(new FileInputStream(file));
        char[] ch = new char[10];
        int count = 0;
        char temp;
        while((temp = input.readChar()) != 'C'){
            ch[count++] = temp;
        }
        System.out.println(ch);
    }
}

 

 

【运行结果】:

 

AB

 

合并流 SequenceInputStream

 

SequenceInputStream主要用来将2个流合并在一起,比如将两个txt中的内容合并为另外一个txt。下面给出一个实例:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
 
/**
 * 将两个文本文件合并为另外一个文本文件
 * */
public class SequenceInputStreamDemo{
    public static void main(String[] args) throws IOException{
        File file1 = new File("d:" + File.separator + "hello1.txt");
        File file2 = new File("d:" + File.separator + "hello2.txt");
        File file3 = new File("d:" + File.separator + "hello.txt");
        InputStream input1 = new FileInputStream(file1);
        InputStream input2 = new FileInputStream(file2);
        OutputStream output = new FileOutputStream(file3);
        // 合并流
        SequenceInputStream sis = new SequenceInputStream(input1, input2);
        int temp = 0;
        while((temp = sis.read()) != -1){
            output.write(temp);
        }
        input1.close();
        input2.close();
        output.close();
        sis.close();
    }
}

 

 

【运行结果】

 

结果会在hello.txt文件中包含hello1.txthello2.txt文件中的内容。

 

转自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

分享到:
评论

相关推荐

    java中的IO整理完整版

    java中的IO整理完整版

    Java中IO系统总结[整理].pdf

    Java中IO系统总结[整理].pdf

    java中的IO流整理

    此文档是对JAVA 中的 IO 流的整理,其中有大多实用 而平时可以接触到的 IO 基础,对开发工作者有很大的帮助

    Java中的IO整理完整版

    Java中的IO整理完整版

    JAVA_IO流整理思维导图.emmx

    JAVA_IO流整理思维导图.

    java IO全面整理

    java IO全面整理,整理了一下关于java 的IO操作,我是直接在测试的时候将关键的测试代码放上去了,并配以简洁的注解,适合有一定基础的朋友!

    javaIO流整理.txt

    javaIO流整理.txt

    java基础 IO流

    此文档属于本人当初学习java基础之IO流,所整理的文档。里面有字节流与字符流的比较,也有总结使用不同方式读取文档的demo。希望对你的学习有帮助,谢谢!

    Java中IO流简介_动力节点Java学院整理

    Java io系统的设计初衷,就是为了实现“文件、控制台、网络设备”这些io设置的通信。例如,对于一个文件,我们...而到了java 1.1,为了与国际化进行接轨,在java io中添加了许多以字符(Unicode)为单位进行操作的类。

    javaIO流思维导图

    自己整理了一下javaIO流的相关知识点 用xmind软件做了一下

    Java IO流.xmind

    Java IO流思维导图,主要摘录整理的是java.io.*包下的所有IO对象,其中对应备注里包含各个IO对象的构造方法

    Java IO复用_动力节点Java学院整理

    对于服务器的并发处理能力,我们需要的是:每一毫秒服务器都能及时处理这一毫秒内收到的数百个不同TCP连接上的报文,与此同时,可能服务器上还有数以十万计的最近几秒没有收发任何报文的相对不活跃连接。...

    Java多线程.drawio

    Java多线程.drawio

    Java基础篇:IO流.pdf

    该文档主要整理了Java IO流的相关信息,主要包括IO流的按照不同维度的分类、节点流、处理流、输入输出流的处理过程、抽象基类的使用等细节内容

    scalable-io-in-java-中文.pdf

    网上都是不带书签,并且有些地方翻译有歧义。 所以我整理了一个。 特点:带书签 Scalable io in java 中文版,并且对有歧义的语义进行了修改。

    IO流体系继承结构图_动力节点Java学院整理

    Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚其结构,需要明白两点: 1. 其对称性质:InputStream 与 OutputStream, Reader 与 Writer,他们分别是一套字节输入-输出,字符输入-输出体系 2. 原始处理器(适配器)...

    Java线程和IO总结[整理].pdf

    Java线程和IO总结[整理].pdf

    JAVA高级知识点整理.rar

    Java高级技术整理,包含多线程、虚拟机、JAVA IO/NIO 、Java集合 等高级进阶知识点

    Java中的IO与NIO-jiava求职面试-15题,答案

    最新整理的Java中的IO与NIO相关面试题目总结,java求职面试题,共15题,含答案解析,希望能帮到有需求的同学

    java核心知识点整理

    java详细的知识点整理,包括:jvm原理、IO、类加载过程、集合、线程、反射、泛型等java基础,spring原理、特点,微服务架构、数据库引擎、消息组件、算法、数据结构等。偏理论的知识较多,主要用于面试。

Global site tag (gtag.js) - Google Analytics