编程语言
首页 > 编程语言> > Java中读入读出文件

Java中读入读出文件

作者:互联网

Java中读入读出文件

1.从文件中读取数据,并将读取的数据输出都屏幕上。

package BasicExercise;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Main{
	public static void main(String [] args) throws Exception{
		//注意这里D:/Eclipse_EE/text.txt与传统的文件路径表示法方式【D:\Eclipse_EE\text.txt】是不匹配的
		//创建一个文件字节输入流
		FileInputStream in = new FileInputStream("D:/Eclipse_EE/text.txt");
		int b =0;
		while(true){
			b =in.read();
			if(b==-1){//如果读取的字节为-1,那么跳出while循环
				break;
			}
			System.out.println(b);
		}
		in.close();
	}
}
运行结果是 各个字节的十进制数。因为这里使用的是读取字节流【FileInputStream读取的是字节流】

2.从文件中读取数据,并将数据拷到另一个文件中

package  BasicExercise;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main{
	public static void main(String [] args) throws IOException{
		//从text.txt中读入数据
		FileInputStream in =new FileInputStream("D:/Eclipse_EE/text.txt");
		//将数据读到test.txt中
		FileOutputStream out = new FileOutputStream("D:/Eclipse_EE/test.txt");
		
		int length;//Take a note of length of byte
		long begintime = System.currentTimeMillis();
		while((length=in.read())!=-1){//判断是否读到文件末尾
			out.write(length);//将读到的字节写入文件
		}
		long endtime= System.currentTimeMillis();
		System.out.println(endtime-begintime);
		in.close();
		out.close();		
	}
}

3.将数据从文件中读到数据缓冲池,然后再输入到另一份文件中

package BasicExercise;

import java.io.*;

public class Main{
	public static void main(String [] args) throws Exception{
		InputStream in = new FileInputStream("D:/Eclipse_EE/text.txt");//read
		OutputStream out = new FileOutputStream("D:/Eclipse_EE/test1.txt");//write
		
		int length;
		byte [] buff = new byte[1024];//buffer
		long begintime = System.currentTimeMillis();
		while((length = in.read(buff))!=-1){
			out.write(buff,0,length);
		}
		long endtime = System.currentTimeMillis();
		System.out.println(begintime-endtime);
		in.close();
		out.close();
	}
}

注:

public int read(char[] cbuf)
         throws IOException

Reads characters into an array. This method will block until some input is available, an I/O error occurs,
or the end of the stream is reached.

Parameters: 
cbuf - Destination buffer 

Returns: 
The number of characters read, or -1 if the end of the stream has been reached 

Throws: 
IOException - If an I/O error occurs

译注:将读取的字符写入到数组中。如果没有可用的输入,这个方法将会阻塞,并且会出现一个I/O错误,否则,会阅读直到流的末端。

public int read()
         throws IOException

Reads a byte of data from this input stream. This method blocks if no input is yet available.
Specified by: 
read in class InputStream 

Returns: 
the next byte of data, or -1 if the end of the file is reached. 

Throws: 
IOException - if an I/O error occurs.

译注:从输入流中读取数据字节。【说明是字节流】如果没有可用的输入流,这个方法将会阻塞。

返回数据的下一个字节,如果文件访问到底,则会返回-1.

(3)BufferedReader类【非常重要】

public class BufferedReader
extends Reader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading 
of characters, arrays, and lines. 
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. 

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character 
or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations 
may be costly, such as FileReaders and InputStreamReaders. For example, 

 BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() 
could cause bytes to be read from the file, converted into characters, and then returned, 
which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by
replacing each DataInputStream with an appropriate BufferedReader.

Since: 
JDK1.1 
See Also: 
FileReader, InputStreamReader, Files.newBufferedReader(java.nio.file.Path, java.nio.charset.Charset) 

译注:

01.从字符流中读取文本,缓冲字符是为了提供有效的字符,数组,行读取。

02.缓冲区的大小可以被指定,否则会使用默认的缓冲大小。对于大多数的读取目的,默认大小已经足够。
03.通常情况下,每个由Reader类发起的读取请求造成相应的读取请求【 由底层字符或者字节流组成】。因此
明智的方法是:在任何可能存在高代价的Reader读取操作上包装一层 BufferReader。诸如:FileReaders,InputStreamReaders.
例如:
BufferedReade in = new BufferedReader(new FileReader("foo.in"))
这个操作将会把相应的文件放入缓冲。如果没有缓冲的话,每次 read()或者 readLine()方法的调用都会引发对文件字节的读取,再将这些字节转换成字符,接着被返回,这样的操作将是非常的低效。
04.使用 DataInputStreams去进行文本输入的程序中的每个 DataInputStream能够用一个恰当的BufferReader被局部替换。



标签:Java,字节,read,读出,读入,new,out,public,读取
来源: https://blog.51cto.com/lawsonabs/3002202