java字节的输入输出流,java 字节输入输出流
標簽:/*
字節流:
InputStream
OutputStream
*/
import java.io.*;
class FileStream
{
public static void main(String[] args) throws IOException
{
outputFile();
//inputFile_1();
inputFile_2();
}
/*不利用數組進行緩沖的方法*/
public static void outputFile() throws IOException //寫
{
FileOutputStream fos=new FileOutputStream("D:\\myfile\\mycode\\4.txt");
fos.write("abndks你好".getBytes());
fos.close();
}
public static void inputFile() throws IOException//讀
{
FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
int ch=0;
while((ch=fis.read())!=-1)
{
System.out.println((char)ch);
}
fis.close();
}
/*利用數組進行緩沖的方法進行讀*/
public static void inputFile_1() throws IOException//讀
{
FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
int num=0;
byte[] b=new byte[1024];
while((num=fis.read(b))!=-1)
{
System.out.println(new String(b,0,num));
}
fis.close();
}
/*使用字節流對象中特有的available獲取字節個數。*/
public static void inputFile_2() throws IOException//讀
{
FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
int num=fis.available();
byte[] b=new byte[num];
int x=fis.read(b);
System.out.println("x:"+x);
System.out.println("num:"+num);
System.out.println(new String(b));
fis.close();
}
}
復制圖片:
/*
拷貝圖片:
思路:
1,用字節讀取流對象和圖片關聯。
2,用字節寫入流對象創建一個圖片文件,用于存儲獲取到的圖片數據。
3,通過循環讀寫,完成數據的存儲。
4,關閉資源。
*/
import java.io.*;
class CopyPic
{
public static void main(String[] args)
{
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
System.out.println("1111");
fis=new FileInputStream("D:\\20.jpg");
System.out.println("2222");
fos=new FileOutputStream("C:\\test\\new.jpg");
System.out.println("3333");
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1)
{
fos.write(b,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("復制文件失敗");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("讀取關閉失敗");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("寫入關閉失敗");
}
}
}
}
復制MP3文件,使用字節流緩沖區
/*
通過復制MP3媒體文件來使用緩沖區
*/
import java.io.*;
class CopyMp3
{
public static void main(String[] args)
{
long start=System.currentTimeMillis();
copy_1();
long end=System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
public static void copy_1()
{
FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try
{
fis=new FileInputStream("D:\\CloudMusic\\Lonely.mp3");
fos=new FileOutputStream("D:\\myfile\\mycode\\new.mp3");
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
int by=0;
while((by=bis.read())!=-1)
{
bos.write(by);
}
}
catch (IOException e)
{
throw new RuntimeException("復制失敗");
}
finally
{
try
{
if(bis!=null)
bis.close();
}
catch (IOException e)
{
throw new RuntimeException("讀取流關閉失敗");
}
try
{
if(bos!=null)
bos.close();
}
catch (IOException e)
{
throw new RuntimeException("寫入流關閉失敗");
}
}
}
}
標簽:
總結
以上是生活随笔為你收集整理的java字节的输入输出流,java 字节输入输出流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php程序网站整站301,织梦dede怎
- 下一篇: 抗战2——知识型特种兵个人经验心得