好久没有写java的io流的,回顾一下,BufferedWriter和BufferedReader是字符流的包装类,可以轻松的实现读取一整行,也可以在写的时候,轻松换行。OutPutStream和InputStream是字节流的基类抽象类,FileInputStream和FileOutPutStream是字节流的基类抽象类。字节流方便于音视屏,图片等的传输,但不适用于字符的传输,因为编码的原因,有可能会产生乱码。字符流就是为了方便进行字符的传输了。下面就这这三类流进行分别举例,例子是参考这位兄弟的,毕竟好记性不如自己的烂笔头https://blog.csdn.net/u014150951/article/details/51838799。

一.关于字符流(因为异常捕获,写到本地多用字符流)

String content="hello我是中国人";

         //这里说一下,声明一个文件还可以:File file=new File("/Users/pc/test.xmlt");

         File file=new    File("/Users/pc","test.xmlt");

         char[] c=content.toCharArray();

         try {

             //先把内容写到文件中,内容作为参数,文件作为流的构造函数的参数

             FileWriter fw=new FileWriter(file);

             fw.write(c);

             fw.close();

             //再开始读文件中的内容

             FileReader fr=new FileReader(file);

             //定义一个数组,用于把文件中的数据读到这里面来

             char[] c2=new char[100];

             int count=0;

             //如果成功读到数组里面了,则返回读到的字符的个数

             while((count=fr.read(c2))!=-1){

             //从数组的0位置到count位置生成一个字符串

                 String string=new String(c2,0,count);

                 System.out.println(string);

             }

             fr.close();


         } catch (IOException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         }

二.关于包装字符流(原因同上)

         //字符包装流,可以在读取的时候轻松读取一整行,也可以在写的时候,轻松换行

         String[] content1={"我是中国人","我爱中国","我讨厌日本人","我讨厌韩国人"};


         try {

             FileWriter fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\c.txt");

             BufferedWriter bw=new BufferedWriter(fw);

             for(String str:content1){

                 bw.write(str);

                 bw.newLine();       

             }

             //化妆过的流关了,则原来的流也自动关了

              bw.close();

              //fw.close();

              //读这个文件中的内容

              FileReader fr=new FileReader("C:\\Users\\Administrator\\Desktop\\test\\c.txt");

              BufferedReader br=new BufferedReader(fr) ;

              String string=null;

              while((string=br.readLine())!=null){

                  System.out.println(string);

              }

              br.close();

              fr.close();     

         } catch (IOException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         }

三.字节流

与服务进行数据的交互,文件的拷贝就少不了我们这个事无巨细都一丝不苟的字节流了,实例如下:

  File file1=new File("/Users/pc","test.xml");

         File file2=new File("/Users/pc","test.xmlt");

         //基本上只要读取文件里面的内容就需要一个小数组,包装字符流除外(BufferedReaderStream),可以读取一整行

         byte[] b=new byte[100];

         int count=0;

         String txt="我是中国人";

         byte[] b2=txt.getBytes();

         //使用字节输入输出流进行文件的读写

         try {

             FileInputStream is=new FileInputStream(file1);

             try {

                 while((count=is.read(b))!=-1){

                 //读取小数组里面的所有数据,以字符串的形式输出

                     String string=new String(b,0,count);

                     System.out.println(string);

                 }

                 is.close();

                 //字节输出流,把字符串写入到文件里

                 FileOutputStream os=new FileOutputStream(file2);

                 os.write(b2);

                 os.close();


             } catch (IOException e) {

                 // TODO Auto-generated catch block

                 e.printStackTrace();

             }


         } catch (FileNotFoundException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         }

以上是热心网友提供的例子,我加了点注释,表达了自己的理解,下面最后的这个例子,就是促成我今天回顾io流知识的契机了,做安卓屏幕适配需要自己写一个基础文件,里面有像素的对应关系,所以就用到了io流了,代码如下:

       File file=new File("/Users/pc","test.xml");

       if(!file.exists()) {

       try {

       //mkdir创建单级目录的文件夹,mkdirs创建多级目录的文件夹,createNewFile()创建一个文件

boolean a = file.createNewFile();

System.out.print(a);


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

       }

     

       FileWriter fis;

       BufferedWriter bf = null;

           try {

                fis=new FileWriter(file);

                bf = new BufferedWriter(fis);

                for(int i=0;i<2500;i++) {

                bf.write("<dimen name=\"size"+i+"\">"+i+"dp</dimen>");

                bf.newLine();

                }

                

                bf.flush();

           }  catch (Exception e) {

               e.printStackTrace();

           }

生成的dimens文件效果如下,可爽了:

IO流的的简单使用

好了,写这篇博客记录一下,基本上开发的io流操作也就这些了,下次用到的时候方便回头来找

相关文章:

  • 2022-12-23
  • 2022-01-30
  • 2021-09-01
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-12
  • 2021-12-19
  • 2022-01-07
  • 2021-06-21
  • 2022-12-23
  • 2021-05-30
  • 2021-12-30
相关资源
相似解决方案