Java文件上传下载核心代码

来源:互联网 发布:造谣的网络大v有哪些 编辑:程序博客网 时间:2024/06/01 08:11

 //创建一个文件
  File file=new File("E://c.txt");

//判断文件是否存在

//如果不存在,则重新创建一个E://c.txt的文件
  if(!file.exists())
  {
   file.createNewFile();

//可以在文件里面写些东西,暂时略了
  }
  //将文件加载到FileInputStream中
  FileInputStream inputStream=new FileInputStream(file.getAbsolutePath());
  FileOutputStream outputStream=new FileOutputStream("E://d.txt");
  byte[] b=new byte[1024];
  int readByte=0;
  //inputStream读取文件流
  //将读的的信息放入byte[]b数组中
  while((readByte=inputStream.read(b))>0)
  {
   //readByte:读取到的字节数
   outputStream.write(b, 0, readByte);
  }
  //关闭读取
  outputStream.close();
  inputStream.close();

原创粉丝点击