文件的读与写

来源:互联网 发布:手机怎么复制淘宝链接 编辑:程序博客网 时间:2024/05/01 01:02
步骤:
文件的读取 源头
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();                           }                     }              }       }}




0 0
原创粉丝点击