IO基础

*****有异常需要抛出

//输出流;写;OutputStream
                 File file2=new File("c:\\text.txt"); 
     OutputStream ops = new FileOutputStream("c:\\text.txt");
      String str="Hello,Java!";//声明一个字符串
      byte[] word= str.getBytes();//获取字符串中的字节存入字节数组
            ops.write(word);//将word流存入ops,即text.txt
            ops.flush();//刷新
           
  //输入流;读;IntputStream;
         File file3 = new File("c:/text.txt");
         InputStream is = new FileInputStream(file3);
         System.out.println("可读取的字节数:"+is.available());//输出可读取的字节数
         int data;
         System.out.println("文件内容为:");
         while((data=is.read())!=-1){//循环输出文件内容
          System.out.println(data+"  ,");
         }
        
  //关闭数据流
         is.close();
         ops.close();
         

你可能感兴趣的:(IO基础)