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();
} } }