/*字节流: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();


}



}