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