zoukankan      html  css  js  c++  java
  • 学习练习 java 输入输出流练习2

     编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
     1 package com.hanqi.io;
     2 
     3 import java.io.*;
     4 
     5 public class IoDemo {
     6 
     7     public static void main(String[] args) {
     8         
     9         try{
    10             File file = new File("e:/test.txt");
    11 
    12             // 如果文件不存在
    13             if (!file.exists()) 
    14             {
    15                 file.createNewFile();
    16                 
    17                 System.out.println("创建文件成功");
    18             }
    19             
    20             //构造输出流
    21             //覆盖写入
    22             FileWriter fw = new FileWriter(file);
    23             
    24             fw.write("aaaaaaaaaa");
    25             
    26             fw.close();
    27             
    28             System.out.println("写入文件完成");
    29             
    30             //字符输入流
    31             FileReader fr = new FileReader(file);
    32             
    33             char[] c = new char[1024];
    34             
    35             String str = "";
    36             
    37             int i = 0 ;
    38             
    39             while(( i = fr.read(c)) > 0)
    40             {
    41                 str += new String(c,0,i);
    42             }
    43             
    44             System.out.println("str = " + str);
    45             
    46             }
    47             catch (Exception e) 
    48         {
    49                 // TODO 自动生成的 catch 块
    50                 e.printStackTrace();
    51             
    52             }
    53         
    54         File file1 = new File("e:/IoDemo.txt");
    55         
    56         try 
    57         {
    58             file1.createNewFile();
    59             
    60    FileOutputStream fos = new FileOutputStream(file1);
    61             
    62             String str = "aaaaaaaaaa";
    63             
    64             //把数据源转换成byte[]数组
    65             byte [] b  = str.getBytes();
    66             
    67             //写入数据
    68             fos.write(b);
    69             
    70             //关闭流,释放文件
    71             fos.close();
    72         } 
    73         catch (IOException e) 
    74         {
    75             
    76             e.printStackTrace();
    77         }
    78         
    79         }
    80 
    81 
    82     }

  • 相关阅读:
    转载阿里开源的分布式事务框架 Seata
    查看进程上的线程数量
    chmod命令
    查找端口与查找正在运行的进程
    可输入的文本框
    js 类和对象
    js 三维数组
    ajax
    ORACLE数据库主要的系统表和数据字典视图
    url 传参
  • 原文地址:https://www.cnblogs.com/zhoudi/p/5546433.html
Copyright © 2011-2022 走看看