java io入门程序集合
2014-08-02 11:10
309 查看
</pre><pre name="code" class="java"> import java.io.*; import javax.imageio.stream.FileImageOutputStream; public class ioTest1 { public static void main(String []args){ //创建一个文件对象 File f=new File("f:\\myCounter.txt"); //得到文件路径 System.out.println("文件路径"+f.getAbsolutePath()); //得到文件的大小 System.out.println("文件大小:"+f.length()); //得到文件是否刻度 System.out.println("可读"+f.canRead()); //创建文件 File f=new File("f:\\shj.txt"); if(!f.exists()){ //可以创建 try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ //已经存在 System.out.println("文件存在了!"); } //创建文件夹 File f=new File("f:\\ff"); if(f.isDirectory()){ //存在 }else{ //创建 f.mkdir(); } //查看文件夹下面有多少个文件 File f=new File("f:\\ff"); if(f.isDirectory()){ File lists[]=f.listFiles(); for(File i : lists){ System.out.println("文件名:"+i); i.delete();//删除文件,需要路径 } } //file 没有读写的能力,所以需要InputStream File f=new File("f:\\ff.txt"); FileInputStream fis=null; try { fis=new FileInputStream(f); //定义一个字节数组,相当于缓存 byte []bytes=new byte[1024]; int n=0;//实际读取到的字节数 //循环读取 while((n=fis.read(bytes))!=-1){ //把字节转成String String s=new String(bytes,0,n); System.out.println(s); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭文件流,必须放这 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //outputStream流的使用 File f=new File("f:\\ff.txt"); FileOutputStream fos=null; try { fos=new FileOutputStream(f); String s="孙helloworld\r\n"; //回车换行 String s1="sdfsfd"; //byte []bytes=new byte[1024]; //如果大的话就用,小的话就不用了。 fos.write(s.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //拷贝图片 //必须用字节流,用字符流会出错。 File f=new File("f:\\1.png"); FileInputStream fis=null; File f1=new File("f:\\ff\\2.png"); FileOutputStream fos=null; try { fos=new FileOutputStream(f1); fis=new FileInputStream(f); //定义一个字节数组,相当于缓存 byte []bytes=new byte[1024]; int n=0;//实际读取到的字节数 //循环读取 while((n=fis.read(bytes))!=-1){ //把字节转成String //String s=new String(bytes,0,n); fos.write(bytes); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭文件流,必须放这 try { fis.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //把文件拷贝到另一个文件 FileReader fr=null; FileWriter fw=null; //创建fr对象 try { fr=new FileReader("f:\\ff.txt"); fw=new FileWriter("f:\\ff\\ff.txt"); char c[]=new char[1024]; int i=0; while((i=fr.read(c))!=-1){ //方法一 String s=new String(c,0,i); char c1[]=new char[i]; //System.out.println(s); s.getChars(0, i, c1, 0); fw.write(c1); //方法二 fw.write(c,0,i); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fr.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //为了提高效率引入了缓冲字符流 BufferedReader br=null; BufferedWriter bw=null; try { //先创建FileReader对象,再newBufferedReader FileReader fr=new FileReader("f:\\ff.txt"); br=new BufferedReader(fr); //创建FileWriter对象 FileWriter fw=new FileWriter("f:\\ff\\aa.txt"); bw=new BufferedWriter(fw); String s=""; while((s=br.readLine())!=null){ System.out.println(s+"11"); bw.write(s+"\t\n"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- java IO 集合 学生成绩管理程序
- Java2实用教程(第二版)程序代码——第一章 Java语言入门
- Corba入门程序(Java --- C)---omniORB
- Java程序基础测试 基础笔试、面试常见问题集合
- java数据结构:链表与迭代器入门程序
- [转]新手入门写Java程序的三十个基本规则
- Java手机程序设计入门 电子书开放下载
- 新手入门:写Java程序的三十个基本规则
- Java程序性能优化总结--------数组/集合篇
- 新手入门:写Java程序的三十个基本规则
- Sun Java Wireless Toolkit 开发手机程序入门指南(2)
- 一些入门的java小程序
- java程序中IO输入问题
- 使用技巧:Java程序用缓冲IO来提高性能
- 使用技巧:Java程序用缓冲IO来提高性能
- Java语言入门 -- 第二章 Java程序编译与运行环境
- 一些入门的java小程序-zt
- java io 操作集合
- 新手入门:写Java程序的三十个基本规则
- 新手入门:写Java程序的三十个基本规则