字节流 2016.10.9

package 字节流;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

/*字符流 

* FileWriter

* BufferedReader

* BufferedWriter

*

* 字节流

* InputStream

* OutputStream

*

* 想要操作图片数据,这时就要用到字节流

*/

public class FileOutputStreamDemo {

public static void main(String[] args) {

// TODO Auto-generated method stub

//写字节

// FileOutputSteamFun();

//读字节

FileInputSteamFun_3();

}

public static void FileOutputSteamFun(){

FileOutputStream fs = null;

try {

fs = new FileOutputStream("src/fox.txt");

try {

fs.write("abcdeddddd".getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

try {

fs.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void FileInputSteamFun_1(){

FileInputStream fis = null;

try {

fis = new FileInputStream("src/fox.txt");

int ch = 0;

try {

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

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

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

try {

fis.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void FileInputSteamFun_2(){

FileInputStream fis = null;

try {

fis = new FileInputStream("src/fox.txt");

byte[] buf = new byte[1024];

int len = 0;

// try {

// System.out.println(fis.available());//打印字节数的个数

// } catch (IOException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

try {

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

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

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

try {

fis.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void FileInputSteamFun_3(){

FileInputStream fis = null;

try {

fis = new FileInputStream("src/fox.txt");

try {

byte[] buf = new byte[fis.available()];

fis.read(buf);

System.out.println(new String(buf));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}//打印字节数的个数

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

try {

fis.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容