文件的读与写

步骤:
文件的读取 源头
1.建立联系 File对象
2.选择流   输入流 InputStream FileInputStream
3.操作       byte[] car = new byte[1024] +read+读取大小               输出
4.释放资源 关闭流
public class MyInputStream {
       public static void main(String[] args){
              /**
               * 文件的读取 源头
               * 1.建立联系 File对象
               * 2.选择流   输入流 InputStream FileInputStream
               * 3.操作            byte[] car = new byte[1024] +read+读取大小               输出
               * 4.释放资源 关闭流
               */
              File src = new File("C:/Users/Administrator/Desktop/android学习笔记/a.txt");
              InputStream iStream = null;//提升作用域
              try {
                     iStream = new FileInputStream(src);
                     byte[] car =new byte[20];//每次读取的大小为20
                     int len = 0;//接收实际读取大小
                     try {
                           while((len=iStream.read(car))!=-1){
                                  //将字节数组转换成字符串
                                  String info = new String(car, 0, len);
                                  System.out.print(info);
                           }
                     } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                           System.out.println("读取文件失败");
                     }
              } catch (FileNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     System.out.print("读取文件不存在");
              }finally {
                     if(null != iStream){
                           try {
                                  iStream.close();
                           } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                                  System.out.println("关闭失败");
                           }
                     }
              }
       }
}

步骤:
文件的写出 目的地
1.建立联系 File对象
2.选择流   输出流 OutputStream FileOutputStream
3.操作       write() + flush()
4.释放资源 关闭流
public class MyOutputStream {
       public static void main(String[] args){
              /**
               * 文件的写出 目的地
               * 1.建立联系 File对象
               * 2.选择流   输出流 OutputStream FileOutputStream
               * 3.操作            write() + flush()
               * 4.释放资源 关闭流
               */
              File dest = new File("C:/Users/Administrator/Desktop/android学习笔记/a_1.txt");
              OutputStream oStream = null;
              try {
                     oStream = new FileOutputStream(dest, false);//true代表以追加的形式写入数据,false则覆盖原来数据
                     String string = "hello world!";
                     byte[] data = string.getBytes();//字符串转换为字节数组
                     oStream.write(data, 0, data.length);
                     oStream.flush();//强制写出去,就像管道一样,若数据没满出不去。
              } catch (FileNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     System.out.println("文件未找到");
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }finally {
                     if(null!=dest){
                           try {
                                  oStream.close();
                           } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                           }
                     }
              }
       }
}




猜你喜欢

转载自blog.csdn.net/Best_CXY/article/details/70666606
今日推荐