實例01:
需求:向文件中寫入字符串
//public byte[] getBytes():使用平台的默認字符集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中;
package cn.itcast03;
/*
* 將一個字節數據寫入數據流:
* public abstract void write( int b)throws IOException
* 將一個byte數組寫入數據流:
* public void write(byte[] b)throws IOException
* 將一個指定范圍的byte數組寫入數據流:
* public void write(byte[] b, int off,int len)throws IOException
* 刷新緩存區:
* public void flush()throws IOException
* 關閉數據流:
* public void close()throws IOException
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutputStream01 {
public static void main(String[] args) throws IOException {
//聲明File對象
File f = new File("a.txt" );
//通過子類實例化父類對象
OutputStream out = new FileOutputStream(f);
//進行寫操作
String s = "I love JAVA";
byte[] bytes = s.getBytes();
//將一個byte數組寫入數據流:
out.write(bytes);
//將一個指定范圍的byte數組寫入數據流:
out.write(bytes,1,5);
//將一個字節數據寫入數據流
out.write( 'b');
out.close();
}
}
實例02:
package cn.itcast03;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
//追加新的內容
//public FileOutputStream(String name,boolean append)throws FileNotFoundException
//如果將append的值設置為true,則表示在文件的末尾追加內容;
public class FileOutStream03 {
public static void main(String[] args) throws IOException {
File file = new File("b.txt" );
FileOutputStream fos = new FileOutputStream(file,true );
String s = "Hello World";
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++) {
fos.write(bytes[i]);
}
fos.close();
}
}
詳解字節流--(InputStream) 查看API文檔: public abstract class InputStream extends Object implements Closeable InputStream類的常用方法: 取得出入文件的大小: public int available()throws IOException 關閉輸入流: public void close()throws IOException 讀取一個字節內容,以數字的方式讀取(從輸入流中讀取數據的下一個字節。返回
0 到 255 范圍內的 int 字節值。如果因為已經到達流末尾而沒有可用的字節,則返回值 -1
)
public abstract int read()throws IOException
將內容讀到byte數組中,同時返回讀入的個數
public int read(byte[] b)throws IOException
實例01:
package cn.itcast04;
/*
* public abstract int read()throws IOException
* public int read(byte[] b)throws IOException
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo01 {
public static void main(String[] args) throws IOException {
/*
// File f = new File("G:"+File.separator+"JavaTest"+File.separator+"test01.txt ");
File f = new File("a.txt");
FileInputStream fis = new FileInputStream(f);
int b;
while ((b=fis.read())!=-1) {
System.out.println((char)b);
}
fis.close();
*/
System. out.println("==============================================" );
File f = new File("a.txt" );
FileInputStream fis2 = new FileInputStream(f);
/*
//將所有內容讀取到數組中
byte[] bytes =new byte[1024];
//用於記錄本次讀取字節的個數
fis2.read(bytes);
fis2.close();
System.out.println(new String(bytes));
出現的問題:數組長度是1024,但是讀取的數組長度只有17字節,所以會有1007個無用的空間轉為字符串;
修正錯誤如下:
*/
/*
*
byte[] bytes = new byte[1024];
int len ;
while(( len=fis2.read(bytes))!=-1)
{
String s = new String(bytes,0, len);
System.out.println(s);
}
fis2.close();
出現的問題:以上雖然指定了byte數組的范文,但是程序依然開辟了很多的無用的空間,以上的程序沒有從根本上解決問題。
解決方法:使用File提供的length()方法來取得文件的大小
*/
byte[] bytes = new byte[( int)f.length()];
int len;
//如果len的值不是-1,表示文件沒有讀完;
while((len=fis2.read(bytes))!=-1)
{
String s = new String(bytes,0,len);
System. out.println(s);
}
fis2.close();
}
}