您的位置:首页 > 编程语言 > Java开发

java文件输入输出

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

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

读/写流的一般流程

读(Reading)

1.open a stream

2.while more information

3. read information

4.close the stream

写(Writing)

open a stream

while more information
write information

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: