package com.knock0801am.IOtest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* IO 类有很多,只关注重点
* 分类: 输入流;输出流
* 字节流;字符流
* 节点流;处理流
*
* 四个基本的抽象类:IuputStream;OutputStream;Reader;Writer
*
* 常用的实现类:FileInputStream;
* */
public class TestIORead {
/*四个步骤:
1选择源
2选择流
3操作,如定义byte[]+ read +close
4结束,释放资源*/
public static void main(String[] args) {
File file = new File("D:/test/a.txt");
InputStream is = null;
try {
is = new FileInputStream(file);
byte[] resource = new byte[1024];
int len = 0;
/*read方法有三个重载,包括:
read();每次只读一个字节,效率很低
read(byte[] resource);每次读一个数组长度的字节
resd(byte[] resource,offset,resourcelength);每次读一个实际获得的数组的字节(从0开始读,读到数组内容结尾)(推荐)*/
while(-1!= (len=is.read(resource))){
//构建字符串,然后把他输出
String str = new String(resource,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<pre name="code" class="java">package com.knock0801am.IOtest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 四个基本步骤
* 输出流需要多留意一个方法:flush()
*
* */
public class TestIOWrite {
/*四个步骤:
1选择源
2选择流
3操作,如定义byte[]+ read +close
4结束,释放资源*/
public static void main(String[] args) {
OutputStream os = null;
try {
/*注意输出流这里跟输入流稍有不同,除了有File参数和路径字符串参数以外,输出流还可以带一个boolean类型的参数,默认为false,表示覆盖;如果为true,则为追加*/
os = new FileOutputStream("E:/test/a.txt",true);
/*把字符串转化为字符数组,然乎输出到文件中*/
String str ="锄禾日当午,码农真辛苦,一本小破书,一读一上午";
byte[] byteStr = str.getBytes();
int len = 0 ;
os.write(byteStr, 0, byteStr.length);//跟输入流不一样,不需要定义一个数组来确定每次输出的大小,一次全部输出,但是需要flush();
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.knock0801am.IOtest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 把输入输出结合起来,完成文件拷贝:从D拷贝到E
*
* */
public class CopyFile {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("D:/test/1.jpg");
os = new FileOutputStream("E:/test/100.jpg");
byte[] resource = new byte[1024];
int len = 0;
while(-1 != (len = is.read(resource))){
os.write(resource, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=os){
os.close();
}
if(null!=is){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.knock0801am.IOtest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 稍微封装一下
* */
public class CopyFile2 {
public static void copyFile(FileInputStream fis,FileOutputStream fos){
InputStream is = null;
OutputStream os = null;
try {
is = fis;
os = fos;
byte[] resource = new byte[1024];
int len = 0;
while(-1 != (len = is.read(resource))){
os.write(resource, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=os){
os.close();
}
if(null!=is){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.knock0801am.IOtest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:/test/1.jpg");
FileOutputStream fos = new FileOutputStream("E:/test/100.jpg");
CopyFile2.copyFile(fis, fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}