java文件输入输出

在java程序中,对于数据的输入/输出操做以流(Stream)方式进行;J2SDK提供了各类各样的“流”类,用以获取不一样种类的数据;程序中经过标准的方法输入或输出数据,I/O流提供一条通道程序,能够使用这条通道把源中的字节序列送给目的地把输入流的指向称做源,程序从指向源的输入流中读取源读取源中的数据而输出流的指向是字节要去的一个目的地,程序经过向输出流中写入数据把信息传递到目的地。虽然I/O流常常与磁盘文件存取有关,可是程序的源和目的地也能够是键盘、鼠标、内存或者显示器窗口。java

输入流经过使用read()方法从输入流读出源中的数据,输出流经过用write()方法把数据写入输出流到达目的地。函数

读/写流的通常流程spa

读(Reading)命令行

1.open a streamcode

2.while more informationorm

3.    read information对象

4.close the stream
内存

写(Writing)
get

  1. open a stream
    input

  2. while more information

    write information

  3. close ths stream

两种流的定义(读取信息的基本数据单位)

字节流:一个字节一个字阶的读或者写,可以输出任何类型数据

字符流:一个字符一个字符读或者写,本质是基于字节流读取字节时,去查指定的吗表


输入/输出流的分类

java.io包中定义了多个流类型来实现输入/输出功能;能够从不一样的角度对其进行分类:

按照数据流的方向的不一样能够分为输出流输出流

按照处理数据单位的不一样能够分为字节流和字符流

按照功能不一样能够分为字节流和处理流



标准输入输出

package com.zhong.test;
import java.io.IOException;
public class StandardStream {
    public static void main(String args[]) throws IOException{
        byte b[] = new byte[10];
        System.in.read(b);
    }
}



scanner是队System.in.read();的扩展,SYstem.in.read();是每按一次键接收到一个一个字符,而scanner则能够根据不一样的条件接受不一样的值。

Scanner reader = new Scanner(System.in);

而后reader对象调用下列方法(函数),读取用户在命令行输入的各类数据类型:

nextByte(),nextDouble(),nextFloat().nextInt(),nextLine(),nextLong(),nextShort()

上述方法都会形成阻塞,等待用户在命令行输入数据回车确认。

package com.zhong.test;
import java.util.Scanner;
public class ScannerDemo {
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextShort();
        System.out.println("s = "+s);
    }
}

文件的读写操做

文件读/写流程

1.打开文件流2.条件判断3.读出/写入4.关闭文件流

两种类型文件

FIleInputStream/FileOutputStream(字节流)

FileReader/FileWriter(字符流)

package com.zhong.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes {
    public static void main(String args[]) throws IOException{
        File inputfile = new File("/root/Desktop/targetfile.txt");
        File outputfile = new File("/root/Desktop/resultfile.txt");
        FileInputStream in = new FileInputStream(inputfile);
        FileOutputStream out = new FileOutputStream(outputfile);
        int c;
        while((c = in.read()) != -1){
            out.write(c);
        }
        in.close();
        out.close();
    }
}

提升文件读写效率

两类缓冲流

针对字节流:

java.io.BufferedInputStream类

java.io.BufferedOutStream类

针对字符流

java.io.BufferedReader类

java.io.BufferedWriter类


IO的缓冲区的存在就是为了提升效率,把要操做的数据存进缓冲区,而后一次性把缓冲区的内容写道目的地,而不是写一次就往目的地写一次,因此创建缓冲区对象时,要先有流对象。

package com.zhong.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferRead {
    public static void main(String args[]) throws IOException{
        String filename = "/root/Desktop/String.txt";
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        int count = 0;
        int c;
        while((c = bis.read())!=-1){
            if(c == 'A'){
                count++;
            }
        }
        fis.close();
        bis.close();
        System.out.println(""+count);
    }
}
package com.zhong.test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferWriter {
    public static void main(String args[]) throws IOException{
        FileWriter fw = null;
        fw = new FileWriter("/root/Desktop/String.txt");
        BufferedWriter bfw = new BufferedWriter(fw);
        bfw.write("asda2312asd\n");
        bfw.write("aaaaaaasccc6656xzc23\n");
        bfw.write("aa3as4f546xckjb asjkdbkjck\n");
        bfw.flush();
        bfw.close();
    }
}
package com.zhong.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Echo {
    public static void main(String args[]) throws IOException{
        BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while((s = bw.readLine()).length()!=0){
            System.out.println(s);
        }
    }
}
相关文章
相关标签/搜索