天天看點

InputStream OutputStream

/*位元組流:InputStream OutputStream

  * 

  */

 public class FileOutPutStremDemo {

public static void main(String[] args) throws IOException {

writeFile();

}

public static void writeFile() throws IOException{

//建立檔案,輸出到

FileOutputStream fos=new FileOutputStream("G:\\fos.txt");

          fos.write("asas".getBytes());//不需要重新整理

          fos.close();//但需要關閉



}

public static void readFile_1() throws IOException{

FileInputStream fis=new FileInputStream("G:\\fos.txt");

int ch=0;

while((ch=fis.read())!=-1){

System.out.println((char)ch);

}

fis.close();

}

public static void readFile_2() throws IOException{

FileInputStream fis=new FileInputStream("G:\\fos.txt");

byte[] buf=new byte[1024];

int len=0;

while((len=fis.read(buf))!=-1){

System.out.println(new String(buf,0,len));

}

fis.close();

}

public static void readFile_3() throws IOException{

FileInputStream fis=new FileInputStream("G:\\fos.txt");

int num=fis.available();//可以拿到檔案中的字元個數包含結尾處的\r\n

byte[] buf=new byte[num];//此方法直接定義大小相當的數組,不用循環,但不安全,記憶體容易溢出

fis.read(buf);

fis.close();


}



 }