输入输出流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40390825/article/details/79958190
read();
import java.io.*;

import org.omg.Messaging.SyncScopeHelper;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream is = new FileInputStream("c:\\abc.txt");
		long l1 = System.currentTimeMillis();
		
		/*int result = is.read();
		while(result!=-1){
			System.out.println(result);
			result = is.read();
		}*/
		
		int result;
		while((result = is.read())!=-1){
			System.out.println(result);
		}
		is.close();
		
		long l2 = System.currentTimeMillis();
		System.out.println(l2-l1);
	}

}

read(byte[]);

import java.io.*;
import java.util.Arrays;

import org.omg.Messaging.SyncScopeHelper;

public class Main2 {

	public static void main(String[] args) throws IOException {
		InputStream is = new FileInputStream("c:\\abc.txt");
		long l1 = System.currentTimeMillis();
		byte[] bytes = new byte[10];
		
		/*int len = is.read(bytes);
		while(len!=-1){
			System.out.println(Arrays.toString(bytes));
			len = is.read(bytes);
		}*/
		
		int len=0;
		while((len=is.read(bytes))!=-1){
			System.out.println(Arrays.toString(bytes));
		}
		is.close();
		
		long l2 = System.currentTimeMillis();
		System.out.println(l2-l1);
	}

}
将abc文件内容写到新的文件aa中
import java.io.*;
import java.util.Arrays;

import org.omg.Messaging.SyncScopeHelper;

public class Main2 {

	public static void main(String[] args) throws IOException {
		InputStream is = new FileInputStream("c:\\abc.txt");
		OutputStream io = new FileOutputStream("c:\\aa.txt");
		long l1 = System.currentTimeMillis();
		byte[] bytes = new byte[10];
		
		/*int len = is.read(bytes);
		while(len!=-1){
			System.out.println(Arrays.toString(bytes));
			len = is.read(bytes);
		}*/
		
		int len=0;
		while((len=is.read(bytes))!=-1){
			System.out.println(Arrays.toString(bytes));
			io.write(bytes,0,len);
		}
		is.close();
		io.close();
		long l2 = System.currentTimeMillis();
		System.out.println(l2-l1);
	}

}

网络的输入输出:

import java.io.*;
import java.net.URL;
import java.util.Arrays;

import org.omg.Messaging.SyncScopeHelper;

public class Main2 {

	public static void main(String[] args) throws IOException {
		URL url = new URL("http://www.sdut.edu.cn"); 
		
		InputStream is = url.openStream();
		OutputStream io = new FileOutputStream("c:\\sdut.html");
		long l1 = System.currentTimeMillis();
		byte[] bytes = new byte[10];
		
		int len=0;
		while((len=is.read(bytes))!=-1){
			System.out.println(Arrays.toString(bytes));
			io.write(bytes,0,len);
		}
		is.close();
		io.close();
		long l2 = System.currentTimeMillis();
		System.out.println(l2-l1);
	}

}

文本输出、

import java.io.*;
import java.net.URL;
import java.util.Arrays;
public class Main2 {

	public static void main(String[] args) throws IOException {
		Reader ir = new FileReader("c:\\abc.txt");
		Writer wr = new FileWriter("c:\\aa.txt");
		long l1 = System.currentTimeMillis();
		char[] chars = new char[10];
		
		int len;
		while((len=ir.read(chars))!=-1){
			
			wr.write(chars,0,len);
			String str = new String(chars , 0, len);
			System.out.println(str);
		}
		ir.close();
		wr.close();
		long l2 = System.currentTimeMillis();
		System.out.println(l2-l1);
	}

}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页