public void read(){
InputStream in=null;
try {
//创建文件字节流
in=new FileInputStream("jpg/1.jpg");
int i=0;
//一次读取一个字节,返回对去的数据,如果返回-1表示读取完毕
while((i=in.read())!=-1){
System.out.println(i);
}
byte[] by=new byte[1024];
int len=0;
//一次读取1024字节,將读取的数据存入字节数组,返回本次读取的字节数,返回-1表示读取完毕
while((len=in.read(by))!=-1){
System.out.println(len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
public void writer(){
OutputStream out=null;
try {
//创建文件写入流,true表示追加写入数据,默认为替换写入
out=new FileOutputStream("abc.txt",true);
//写入文件 out.write("明天可以睡懒觉".getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
public void copyfile(){
InputStream in=null;
OutputStream out=null;
try {
in=new FileInputStream("d:/西西软件园.txt");
out=new FileOutputStream("abc.txt");
byte[] b=new byte[1024];
int a=0;
while((a=in.read(b))!=-1){
out.write(b, 0, a);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
} } }