专业的JAVA编程教程与资源

网站首页 > java教程 正文

Java IO流之文件输入输出流

temp10 2025-01-12 18:34:58 java教程 8 ℃ 0 评论

一.FileOutputStream(文件输出流)

OutputStream是一个抽象类,抽象类必须通过子类实现。现在要向文件里输出就要用FileOutputStream。

Java IO流之文件输入输出流

FileOutputStream有四个构造方法,分别为

1.FileOutputStream(File file)-------------向File对象的文件写入数据

2.FileOutputStream(File file,boolean append);------向File对象的文件追加写入数据

3.FileOutputStream(String path)-------------向指定文件写入数据

4.FileOutputStream(String path,boolean append);--------向指定文件追加写入数据

当append的值为true时,向文件中写入的数据会追加到原数据的后面,否则会重写该文件的数据。默认为false。

写入方法1:一个个字节写入

Java代码

  1. public static void main(String[] args) {

  2. try {

  3. //创建一个文件字节输出流对象

  4. OutputStream os=new FileOutputStream("L:\\io.txt");

  5. //写入的数据

  6. String string="hello IO Stream";

  7. byte[]bytes=string.getBytes();//转化为字节数组

  8. for(int i=0;i<bytes.length;i++){

  9. //向文件输出

  10. os.write(bytes[i]);

  11. }

  12. os.close();//关闭流

  13. } catch (FileNotFoundException e) {

  14. e.printStackTrace();

  15. } catch (IOException e) {

  16. e.printStackTrace();

  17. }

  18. }

方法二:全部一次写入

Java代码

  1. public static void main(String[] args) {

  2. try {

  3. //创建一个文件字节输出流对象

  4. OutputStream os=new FileOutputStream("L:\\io.txt",true);//追加

  5. //写入的数据

  6. String string="hello IO Stream";

  7. byte[]bytes=string.getBytes();//转化为字节数组

  8. os.write(bytes);//全部写入

  9. //os.write(bytes,0,5)表示从0开始,写入长度为5个字节

  10. os.close();//关闭流

  11. } catch (FileNotFoundException e) {

  12. e.printStackTrace();

  13. } catch (IOException e) {

  14. e.printStackTrace();

  15. }

  16. }

一.FileInputStream(文件输入流)

FileInputStream是从系统的某个文件中获得输入字节,有两个构造方法

1.FileInputStream(File file)

2.FileInputStream(String path)

读取字节方法1:一个个字节读取

Java代码

  1. public static void main(String[] args) {

  2. try {

  3. //创建一个字节输入流对象

  4. InputStream is=new FileInputStream("L:\\io.txt");

  5. int b=-1;

  6. while((b=is.read())!=-1)//字节读取,当为-1时读取完毕

  7. {

  8. System.out.print((char)b);//转化为字符输出

  9. }

  10. is.close();//关闭流

  11. } catch (FileNotFoundException e) {

  12. e.printStackTrace();

  13. } catch (IOException e) {

  14. e.printStackTrace();

  15. }

  16. }

输出结果为:hello IO Stream。这样一个一个字节读取,速度很慢。

读取字节方法2:一次性读取全部字节

Java代码

  1. public static void main(String[] args) {

  2. try {

  3. File file=new File("L:\\io.txt");

  4. //创建一个字节输入流对象

  5. InputStream is=new FileInputStream(file);

  6. //根据文件大小来创建字节数组

  7. byte[]bytes=new byte[(int)file.length()] ;

  8. int len=is.read(bytes);//返回读取字节的长度

  9. System.out.println("读取字节长度为:"+len);

  10. System.out.println("读取的内容为: "+new String(bytes));//构建成字符串输出

  11. is.close();//关闭流

  12. } catch (FileNotFoundException e) {

  13. e.printStackTrace();

  14. } catch (IOException e) {

  15. e.printStackTrace();

  16. }

  17. }

运行结果:

这种读取方法主要缺点是要构建一个和文件大小一样大的字节数组,文件小的时候还可以,当文件很大,内存可能无法构架出如此大的字节数组。所以,这种方法只适合小文件。

读取字节方法3:每次读取指定长度(最常用的方法)

Java代码

  1. public static void main(String[] args) {

  2. try {

  3. //创建一个字节输入流对象

  4. InputStream is=new FileInputStream("L:\\io.txt");

  5. //指定每次读取的大小--可根据性能字节修改

  6. byte[]bytes=new byte[8] ;

  7. StringBuffer sb=new StringBuffer();

  8. int len=-1;//每次读取的实际长度

  9. while((len=is.read(bytes))!=-1)

  10. {

  11. sb.append(new String(bytes,0,len));

  12. }

  13. System.out.println("读取字节为:"+sb);

  14. is.close();//关闭流

  15. } catch (FileNotFoundException e) {

  16. e.printStackTrace();

  17. } catch (IOException e) {

  18. e.printStackTrace();

  19. }

  20. }

输出结果:

---更多Java学习资料可关注微信公众号:kaigexuetang_com(长按可复制关注)

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表