您的位置:首页 > 其它

IO流_part1

2016-07-26 23:42 501 查看
package 流.IO.读写TXT.part1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* 通过InputStream的read方法,获得字节数组,然后用字节数组初始化字符串,从而输出文件内容到控制台
* File
* InputStream FileInputStream
* read方法
* close方法
*/
public class Read1 {
public static void main(String[] args) throws IOException {
File file = new File("D://测试文件.txt");
InputStream inputStream = new FileInputStream(file);
byte[] b = new byte[1024];
int len = inputStream.read(b);  //从流中读取字节到byte数组中,这个len是指读取到的数量
inputStream.close();
System.out.println(len);
System.out.println(new String(b,0,len));
}

}


package 流.IO.读写TXT.part1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* 通过file.length获得文件中的字节大小,从而创建一个刚刚大小的字节数组
*/
public class Read2 {
public static void main(String[] args) throws IOException {
File file = new File("D://测试文件.txt");
InputStream inputStream = new FileInputStream(file);
int fileLength = (int) file.length();
byte[] b = new byte[fileLength];
inputStream.read(b);
inputStream.close();
System.out.println(new String(b));
}

}


package 流.IO.读写TXT.part1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* 一个字节一个字节的读取
*/
public class Read3 {
public static void main(String[] args) throws IOException {
File file = new File("D://测试文件.txt");
InputStream inputStream = new FileInputStream(file);
int fileLength = (int) file.length();
byte[] b = new byte[fileLength];
int temp=0;
int len=0;
while((temp=inputStream.read())!=-1){
b[len++]=(byte) temp;
}
inputStream.close();
System.out.println(new String(b));
}

}


package 流.IO.读写TXT.part1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
* InputStream OutputStream
* BufferedInputStream BufferedOutputStream
*
* 比较了buffered类的效率
*
* 实现的是一个复制的功能
*
*/
public class ReadWrite {
public static void bufferedStream() throws Exception {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D://测试文件.txt"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("G://测试文件2.txt"));
int b = 0;
long startTime = System.currentTimeMillis();
while ((b = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(b);
}
bufferedInputStream.close();
bufferedOutputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}

// 非缓冲
public static void stream() throws Exception {
InputStream inputStream = new FileInputStream("D://测试文件.txt");
OutputStream outputStream = new FileOutputStream("G://测试文件.txt");
int b = 0;
long startTime = System.currentTimeMillis();
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}

public static void main(String[] args) throws Exception {
stream();
bufferedStream();
}

}


package 流.IO.读写TXT.part1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* 以覆盖的方式写入文件
* File
* OutputStream FileOutputStream
* write方法
* close方法
*/
public class Write1 {
public static void main(String[] args) throws IOException {
File file = new File("D://测试文件.txt");
OutputStream out=new FileOutputStream(file,true);
String str="你好, Java";
byte[] b=str.getBytes();
out.write(b);
out.close();
}

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