InputStream输入流 文件从硬盘读取到内存
OutputStream输出流 文件从内存写入到硬盘
一批代码:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
byte b[]=new byte[1024];
inputStream.read(b);
inputStram.close();
syso(new String(b));//1024个字节多余空字符代替
优化:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
byte b[]=new byte[1024];
int len= inputStream.read(b);
inputStram.close();
syso(new String(b,0,len));//截取0-len个
优化:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
int filelength=(int)file.length();//强制类型转换
byte b[]=new byte[filelength];
inputStream.read(b);
inputStram.close();
syso(new String(b));
一个字节读取代码:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
int filelength=(int)file.length();//强制类型转换
byte b[]=new byte[filelength];
int len=0;
int temp=0;
while((temp=inputStram.read())!=-1){
b[len++]=(byte)temp;
}
inputStram.close();
syso(new String(b));
===============================
一次性写入代码:
File file=new File("c://a.txt");
OutputStream out=new FileOutputStream(file);
String str="aaaaa";
byte b[]=str.getBytes();
out.write(b);
out.close();
追加写入代码:
File file=new File("c://a.txt");
OutputStream out=new FileOutputStream(file,true);
String str="aaaaa";
byte b[]=str.getBytes();
out.write(b);
out.close();
BufferedInputStream
代码:
public static void stream() throws Exception{
InputStream inputStream=new FileInputStream("c://a.doc");
OutputStream outputStream=new FileOutputStream("c://复制的b.doc");
int b=0;
long startTime=System.currentTimeMillis();//获取当前时间
while((b=inputStream.read())!=-1){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long endTime=System.currentTimeMillis();//获取当前时间
syso(endTime-startTime);
}
public static void main(String []args) throws Exception{
stream();
}
BufferedOutputStream
代码:
public static void bufferStream() throws Exception{
BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream("c://a.doc"));
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutStream("c://复制的b.doc"));
int b=0;
long startTime=System.currentTimeMillis();//获取当前时间
while((b=inputStream.read())!=-1){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long endTime=System.currentTimeMillis();//获取当前时间
syso(endTime-startTime);
}
public static void main(String []args) throws Exception{
bufferStream();
}
Reader
一批读取代码
public static void main(String []args){
File file=new File("c://a.txt");
Reader reader=new FileReader(file);
char c[]=new char[1024];
reader.read(c);
reader.close();
syso(new String(new String(c)));
}
优化:
public static void main(String []args){
File file=new File("c://a.txt");
Reader reader=new FileReader(file);
char c[]=new char[1024];
int len=reader.read(c);
reader.close();
syso(new String(new String(c,0,len)));
}
一个字节读取
public static void main(String []args){
File file=new File("c://a.txt");
Reader reader=new FileReader(file);
char c[]=new char[1024];
int len=0;
int temp=0;
while((temp=reader.read())!=-1){
c[len++]=(char)temp;
}
reader.close();
syso(new String(new String(c,0,len)));
}
Writer
代码
public static void main(String []args){
File file=new File("c://a.txt");
Writer out=new FileWriter(file,true);
out.writer;
out.close;
}