socket通信中字节流、字符流区别 续

 

写数据

   

1.import java.io.File; 

2.import java.io.FileOutputStream;  .

3.import java.io.IOException; 

4.import java.io.OutputStream; 

5. 

6.public class Test11 { 

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

8.         File f = new File("d:" + File.separator+"test.txt"); 

9.         OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建 

10.         String str="Hello World"; 

11.         byte[] b=str.getBytes(); 

12.         out.write(b);//因为是字节流,所以要转化成字节数组进行输出 

13.         out.close(); 

14.    }  

15.} 

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

 

public class Test11 {

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

         File f = new File("d:" + File.separator+"test.txt");

         OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建

         String str="Hello World";

         byte[] b=str.getBytes();

         out.write(b);//因为是字节流,所以要转化成字节数组进行输出

         out.close();

    }

}

 

 

也可以一个字节一个字节进行输出,如下:

 

1.import java.io.File; 

2.import java.io.FileOutputStream; 

3.import java.io.IOException; 

4.import java.io.OutputStream; 

5. 

6.public class Test11 { 

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

8.         File f = new File("d:" +File.separator+"test.txt"); 

9.         OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建 

10.         String str="Hello World"; 

11.         byte[] b=str.getBytes(); 

12.         for(int i=0;i<b.length;i++){ 

13.              out.write(b[i]); 

14.         } 

15.        out.close(); 

16.    } 

17. } 

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

 

public class Test11 {

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

         File f = new File("d:" + File.separator+"test.txt");

         OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建

         String str="Hello World";

         byte[] b=str.getBytes();

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

              out.write(b[i]);

         }

        out.close();

    }

 }

 

 

以上输出只会进行覆盖,如果要追加的话,请看FileOutputStream类的另一个构造方法:

public FileOutputStream(File file,booleanappend)throws FileNotFoundException

在构造方法中,如果将append的值设置为true,则表示在文件的末尾追加内容。

 

 

1.import java.io.File; 

2.import java.io.FileOutputStream; 

3.import java.io.IOException; 

4.import java.io.OutputStream; 

5. 

6.public class Test11 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        OutputStream out=new FileOutputStream(f,true);//追加内容 

10.        String str="\r\nHello World"; 

11.        byte[] b=str.getBytes(); 

12.        for(int i=0;i<b.length;i++){ 

13.             out.write(b[i]); 

14.        } 

15.       out.close(); 

16.   } 

 

 import java.io.File;

 import java.io.FileOutputStream;

 import java.io.IOException;

 import java.io.OutputStream;

 

 public class Test11 {

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

         File f = new File("d:" + File.separator+"test.txt");

         OutputStream out=new FileOutputStream(f,true);//追加内容

         String str="\r\nHello World";

         byte[] b=str.getBytes();

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

              out.write(b[i]);

         }

        out.close();

    }

}

 

 

 

 

 

 

文件中换行为:\r\n

字节输入流:InputStream

既然程序可以向文件中写入内容,则就可以通过InputStream从文件中把内容读取进来,首先来看InputStream类的定义:

public abstract class InputStream extendsObject implements Closeable

OutputStream类一样,InputStream本身也是一个抽象类,必须依靠其子类,如果现在是从文件中读取,就用FileInputStream来实现。

观察FileInputStream类的构造方法:

public FileInputStream(File file)throwsFileNotFoundException

读文件:

 

1.import java.io.File; 

2.import java.io.FileInputStream; 

3.import java.io.IOException; 

4.import java.io.InputStream; 

5. 

6.public class Test12 { 

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

8.         File f = new File("d:" +File.separator+"test.txt"); 

9.         InputStream in=new FileInputStream(f); 

10.         byte[] b=new byte[1024]; 

11.         int len=in.read(b); 

12.           in.close(); 

13.           System.out.println(newString(b,0,len)); 

14.    } 

15. }

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

 

public class Test12 {

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

         File f = new File("d:" + File.separator+"test.txt");

         InputStream in=new FileInputStream(f);

         byte[] b=new byte[1024];

         int len=in.read(b);

          in.close();

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

     }

 }

 

但以上方法是有问题的,用不用开辟这么大的一个字节数组,明显是浪费嘛,我们可以根据文件的大小来定义字节数组的大小,File类中的方法:public long length()

 

1.import java.io.File; 

2.import java.io.FileInputStream; 

3.import java.io.IOException; 

4.import java.io.InputStream; 

5. 

6.public class Test13 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        InputStream in=new FileInputStream(f); 

10.        byte[] b=new byte[(int) f.length()]; 

11.         in.read(b); 

12.         in.close(); 

13.         System.out.println(new String(b)); 

14.     } 

15.} 

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

 

public class Test13 {

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

        File f = new File("d:" + File.separator+"test.txt");

        InputStream in=new FileInputStream(f);

        byte[] b=new byte[(int) f.length()];

         in.read(b);

         in.close();

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

     }

}

 

我们换种方式,一个字节一个字节读入

 

1.import java.io.File; 

2.import java.io.FileInputStream; 

3.import java.io.IOException; 

4.import java.io.InputStream; 

5.  

6. public class Test14 { 

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

8.         File f = new File("d:" +File.separator+"test.txt"); 

9.         InputStream in=new FileInputStream(f); 

10.        byte[] b=new byte[(int) f.length()]; 

11.        for(int i=0;i<b.length;i++){ 

12.             b[i]=(byte) in.read(); 

13.       } 

14.        in.close(); 

15.        System.out.println(new String(b)); 

16.    } 

17. } 

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

 

 public class Test14 {

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

         File f = new File("d:" + File.separator+"test.txt");

         InputStream in=new FileInputStream(f);

        byte[] b=new byte[(int) f.length()];

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

            b[i]=(byte) in.read();

       }

        in.close();

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

    }

 }

 

但以上情况只适合知道输入文件的大小,不知道的话用如下方法:

 

1. import java.io.File; 

2. import java.io.FileInputStream; 

3. import java.io.IOException; 

4. import java.io.InputStream; 

5.  

6. public class Test15 { 

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

8.         File f = new File("d:" + File.separator+"test.txt"); 

9.         InputStream in=new FileInputStream(f); 

10.        byte[] b=new byte[1024]; 

11.        int temp=0; 

12.        int len=0; 

13.        while((temp=in.read())!=-1){//-1为文件读完的标志 

14.             b[len]=(byte) temp; 

15.             len++; 

16.        } 

17.        in.close(); 

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

19.    } 

20.} 

 

 import java.io.File;

 import java.io.FileInputStream;

 import java.io.IOException;

 import java.io.InputStream;

 

 public class Test15 {

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

         File f = new File("d:" + File.separator+"test.txt");

         InputStream in=new FileInputStream(f);

        byte[] b=new byte[1024];

        int temp=0;

        int len=0;

        while((temp=in.read())!=-1){//-1为文件读完的标志

            b[len]=(byte) temp;

            len++;

        }

        in.close();

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

    }

}

 

字符流

在程序中一个字符等于两个字节,那么java提供了ReaderWriter两个专门操作字符流的类。

 

字符输出流:Writer

Writer本身是一个字符流的输出类,此类的定义如下:

public abstract class Writer extends Objectimplements AppendableCloseableFlushable

此类本身也是一个抽象类,如果要使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileWriter的子类。

FileWriter类的构造方法定义如下:

public FileWriter(File file)throwsIOException

字符流的操作比字节流操作好在一点,就是可以直接输出字符串了,不用再像之前那样进行转换操作了。

 

 

 

 

 

写文件:

 

1.import java.io.File; 

2.import java.io.FileWriter; 

3.import java.io.IOException; 

4.import java.io.Writer; 

5. 

6. public class Test16 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        Writer out=new FileWriter(f); 

10.       String str="Hello World"; 

11.       out.write(str); 

12.       out.close(); 

13.   } 

14.} 

 

 import java.io.File;

 import java.io.FileWriter;

 import java.io.IOException;

 import java.io.Writer;

 

 public class Test16 {

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

         File f = new File("d:" + File.separator+"test.txt");

         Writer out=new FileWriter(f);

        String str="Hello World";

        out.write(str);

        out.close();

    }

 }

 

在默认情况下再次输出会覆盖,追加的方法也是在构造函数上加上追加标记

 

1.import java.io.File; 

2.import java.io.FileWriter; 

3.import java.io.IOException; 

4.import java.io.Writer; 

5.

 

6.public class Test17 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        Writer out=new FileWriter(f,true);//追加 

10.       String str="\r\nHello World"; 

11.       out.write(str); 

12.       out.close(); 

13.   } 

14.} 

 

 import java.io.File;

 import java.io.FileWriter;

 import java.io.IOException;

 import java.io.Writer;

 

 public class Test17 {

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

         File f = new File("d:" + File.separator+"test.txt");

         Writer out=new FileWriter(f,true);//追加

        String str="\r\nHello World";

        out.write(str);

        out.close();

    }

 }

 

字符输入流:Reader

Reader是使用字符的方式从文件中取出数据,Reader类的定义如下:

public abstract class Reader extendsObjects implements ReadableCloseable

Reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用FileReader子类。

FileReader的构造方法定义如下:

public FileReader(File file)throwsFileNotFoundException

以字符数组的形式读取出数据:

 

1.import java.io.File; 

2.import java.io.FileReader; 

3. import java.io.IOException; 

4.import java.io.Reader; 

5. 

6. public class Test18 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        Reader input=new FileReader(f); 

10.       char[] c=new char[1024]; 

11.       int len=input.read(c); 

12.       input.close(); 

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

14.   } 

 

 import java.io.File;

 import java.io.FileReader;

 import java.io.IOException;

 import java.io.Reader;

 

 public class Test18 {

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

         File f = new File("d:" + File.separator+"test.txt");

         Reader input=new FileReader(f);

        char[] c=new char[1024];

        int len=input.read(c);

        input.close();

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

     }

}

 

也可以用循环方式,判断是否读到底:

 

1.import java.io.File; 

2.import java.io.FileReader; 

3.import java.io.IOException; 

4.import java.io.Reader; 

5. 

6. public class Test19 { 

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

8.        File f = new File("d:" +File.separator+"test.txt"); 

9.        Reader input=new FileReader(f); 

10.       char[] c=new char[1024]; 

11.       int temp=0; 

12.       int len=0; 

13.       while((temp=input.read())!=-1){ 

14.           c[len]=(char) temp; 

15.            len++; 

16.       } 

17.       input.close(); 

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

19.   } 

20.} 

 

 import java.io.File;

 import java.io.FileReader;

 import java.io.IOException;

 import java.io.Reader;

 

 public class Test19 {

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

         File f = new File("d:" + File.separator+"test.txt");

         Reader input=new FileReader(f);

        char[] c=new char[1024];

        int temp=0;

        int len=0;

        while((temp=input.read())!=-1){

            c[len]=(char) temp;

            len++;

        }

        input.close();

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

    }

 }

 

 

字节流与字符流的区别

字节流和字符流使用是非常相似的,那么除了操作代码的不同之外,还有哪些不同呢?

字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的

字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close方法的话,则不会输出任何内容,说明字符流用的是缓冲区,并且可以使用flush方法强制进行刷新缓冲区,这时才能在不close的情况下输出内容

 

那开发中究竟用字节流好还是用字符流好呢?

在所有的硬盘上保存文件或进行传输的时候都是以字节的方法进行的,包括图片也是按字节完成,而字符是只有在内存中才会形成的,所以使用字节的操作是最多的。

 

如果要Java程序实现一个拷贝功能,应该选用字节流进行操作(可能拷贝的是图片),并且采用边读边写的方式(节省内存)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值