java IO 问题分析

来源:互联网 发布:我有域名如何使用 编辑:程序博客网 时间:2024/06/09 23:44

//输入流   FileInputStream fis=null;   //输出流   FileOutputStream fos=null; try {    fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\vb\\12b30830184512b02cd3b610cfd743c9.jpg");  fos=new FileOutputStream("c:\\a.jpg"); byte buf[]=new byte[1024];  //循环读取    int n=0;//记录实际读取到的字节数while ((n=fis.read(buf))!=-1){     //输出到指定文件   fos.write(buf);  }  } catch (Exception e) {  // TODO Auto-generated catch block e.printStackTrace(); }finally{  //关闭打开的文件流  try {    fis.close(); fos.close(); } catch (Exception e2) {// TODO: handle exception    }  } }

Java中提供看IO类库,可以轻松的用java实现对文件的操作


一、创建文件目录

<pre name="code" class="java"><pre name="code" class="java"> <span style="white-space:pre"></span> String   path="f:/javaIO/"; path=path.toString();//中文转换    File file=new   File(path);    if(!file.exists())   //exists()用于判断文件夹是否存在 file.mkdir();   //mkdir()创建文件夹
</pre>二、创建文件<p></p><pre>
<pre name="code" class="java"> <span style="white-space:pre"></span>  String   path="c:/文件名.txt";     path=path.toString();   //中文转换  File   file=new   File(path);     if(!file.exists())try {file.createNewFile();  FileWriter   resultFile=new   FileWriter(file);     PrintWriter   myFile=new   PrintWriter(resultFile);     String   strContent   =   "中文测试".toString();     myFile.println(strContent);     resultFile.close(); } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}   
</pre>三、删除文件<pre>
</pre><pre name="code" class="java"><pre name="code" class="java"><span style="white-space:pre"></span>  String   filePath="c:/文件名.txt";     filePath=filePath.toString();     java.io.File   myDelFile=new   java.io.File(filePath);     myDelFile.delete();    


<pre name="code" class="java">

四、拷贝文件




0 0
原创粉丝点击