Java.io读取大文件

package myPro1;

import java.io.*;

//读取大文件

package myPro1;

import java.io.*;

//读取大文件

public class TestMain {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("C:/123.txt");
			fos = new FileOutputStream("C:/to23.txt");
			byte[] b = new byte[1024];
			while (true) {// 循环读取数据

				// 一次读取1024个字节
				int length = fis.read(b, 0, b.length);
				// 如果返回的字节为-1,代表已经没有数据
				// break,跳出循环
				if (length == -1) {
					break;
				}
				fos.write(b, 0, length);// 一次写入1024个字节
			}
		} catch (Exception e) {
			System.out.println(e);
		} finally {//关闭输入输出流
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				System.out.println(e);
			}

		}

	}
}

猜你喜欢

转载自8850702.iteye.com/blog/2274826