php io流 读取wav,记php中的io流---帮助理解
//注意:當讀到文件末尾的時候會返回-1.正常情況下是不會返回-1的。
public static void main(String[] args) throws IOException {
File f=new File("aaa.txt"); //定位文件位置
InputStream in=new FileInputStream(f); //創建字節輸入流連接到文件
byte[] b=new byte[1024]; //定義一個數組,用來存放讀入的數據 byte數組的大小也可以根據文件大小來定 (int)f.length()
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){ //in.read()是逐字節讀的。當讀到文件末尾時候返回-1
b[count++]=(byte)temp; //將讀到的字節存儲到byte數組中
}
in.close(); //關閉流
System.out.println(new String(b)); //打印讀取到的字節
}
//加入字節緩沖輸入流,提高了讀取效率
public static void main(String[] args) throws IOException {
File f=new File("aaa.txt"); //定位文件位置
InputStream in=new FileInputStream(f); //創建字節輸入流連接到文件
BufferedInputStream bis=new BufferedInputStream(in); //創建緩沖字節流
byte[] b=new byte[1024]; //定義一個數組,用來存放讀入的數據 byte數組的大小也可以根據文件大小來定 (int)f.length()
int count =0;
int temp=0;
bis.read();
while((temp=bis.read())!=(-1)){ //in.read()是逐字節讀的。當讀到文件末尾時候返回-1
b[count++]=(byte)temp; //將讀到的字節存儲到byte數組中
}
bis.close(); //關閉緩沖字節流
in.close(); //關閉流
System.out.println(new String(b)); //打印讀取到的字節
}
//輸出字節流OutputStream
//定義和結構說明:
//IO 中輸出字節流的繼承圖可見上圖,可以看出:OutputStream 是所有的輸出字節流的父類,它是一個抽象類。
//ByteArrayOutputStream、FileOutputStream是兩種基本的介質流,它們分別向Byte 數組、和本地文件中寫入數據。
//PipedOutputStream 是向與其它線程共用的管道中寫入數據,
//ObjectOutputStream 和所有FilterOutputStream的子類都是裝飾流。具體跟InputStream是對應的。
public static void main(String[] args) throws IOException {
File f = new File("aaa.txt"); // 定位文件位置
OutputStream out = new FileOutputStream(f); // 創建字節輸出流連接到文件
String str = "hhhhhhh";
byte[] b = str.getBytes(); //講數據存入byte數組
out.write(b); //寫數據
out.close(); //關閉流
}
復制代碼
public static void main(String[] args) throws IOException {
File f = new File("aaa.txt"); // 定位文件位置
OutputStream out = new FileOutputStream(f); // 創建字節輸出流連接到文件
BufferedOutputStream bos=new BufferedOutputStream(out);
String str = "hhhhhhh";
byte[] b = str.getBytes(); //講數據存入byte數組
bos.write(b); //寫數據
bos.close(); //關閉緩沖流
out.close(); //關閉流
}
總結
以上是生活随笔為你收集整理的php io流 读取wav,记php中的io流---帮助理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux qtcreator输入中文,
- 下一篇: java se入门_java SE 入门