zoukankan      html  css  js  c++  java
  • 输入输出

      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 }
  • 相关阅读:
    IntelliJ IDEA隐藏不想看到的文件或文件夹
    IntelliJ IDEA更改字体和大小
    Dubbo实践(一)入门示例
    部署Jar包到远程Maven仓库
    Linux网卡别名
    ORACLE NLS_LENGTH_SEMANTICS 参数的用途
    Oracle编辑数据时提示:这些查询结果不可更新,请使用ROWI或者SELECT……FOR UPDATE获得可更新结果
    Notepad++格式化JSON字符串
    Xshell配色为Ubuntu风格
    【剑指offer】判断一个序列是否是二叉搜索树的后序遍历,C++实现
  • 原文地址:https://www.cnblogs.com/ymf123/p/4895963.html
Copyright © 2011-2022 走看看