首页 > 其他 > 详细

输入输出

时间:2015-10-20 21:18:01      阅读:210      评论:0      收藏:0      [点我收藏+]
  1 package shurushuchu;
  2 
  3 import  java.io.*;
  4 
  5 public class main {
  6 
  7     public static void main(String[] args)
  8     {
  9     File f =new File ("d:/test.txt");
 10     
 11     if (f.exists())
 12     {
 13         System.out.println("文件存在");
 14         System.out.println("getname="+f.getName());
 15         System.out.println("getpath="+f.getPath());             //目录和文件名
 16         System.out.println("getparent="+f.getParent());         //只获取父目录
 17         System.out.println("sss="+f.getAbsolutePath());         //绝对路径
 18         System.out.println("changdu="+f.length());                   //文件长度
 19         
 20         //f.delete();
 21         
 22         try {
 23             //
 24             FileOutputStream fis = new FileOutputStream("d:/test.txt");
 25         //FileInputStream fis = new FileInputStream("f");
 26             
 27         String strIn = "大家好";
 28             byte[] by = strIn.getBytes();
 29             
 30             fis.write(by,  0,  by.length);
 31             
 32             fis.close();
 33             
 34             //
 35             FileInputStream fin = new      FileInputStream ("d:/test.txt");
 36             
 37             byte [] inp = new byte [256];
 38             
 39             fin.read(inp);
 40             fin.close();
 41             
 42             String r = new String (inp, 0, inp.length);
 43             
 44             System.out.println("读取的是 "+r);
 45             
 46             FileWriter fw = new FileWriter(f);
 47             
 48             fw.write("我要写的字符");
 49             
 50             fw.close();
 51             
 52             FileReader fr = new FileReader(f);
 53             
 54             char [] cin = new char [123];
 55             
 56             fr.read(cin);
 57             fr.close();
 58             
 59             String strin = new String(cin);
 60             System.out.println("读出的内容 "+strin.trim());    //strin.trim()   去空格
 61             
 62             
 63             
 64             
 65             
 66             
 67         }
 68         
 69         
 70         catch (Exception e) {
 71         
 72             e.printStackTrace();
 73         }
 74         
 75         
 76         
 77         
 78         
 79     }
 80     else
 81     {
 82         
 83         try {                                                                              //处理异常
 84             
 85         if (f.createNewFile())
 86         {
 87             System.out.println("文件创建成功");
 88         }
 89         else
 90         {
 91             System.out.println("文件创建失败");
 92         }
 93         } 
 94         catch (IOException e) {                                  //捕获异常
 95             // 输出错误信息
 96             e.printStackTrace();
 97         }
 98         
 99         // System.out.println("文件不存在");
100     }
101     
102     System.out.println();
103     
104     
105     
106     
107 
108     
109     
110     
111     
112     
113     
114     
115     
116     
117 }
118 }

 

输入输出

原文:http://www.cnblogs.com/ymf123/p/4895963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!