zoukankan      html  css  js  c++  java
  • 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

    package homework007;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Text001
    {
    
        public static void main(String[] args) throws Exception
        {
            
            try
            {
            
                //读取
                FileInputStream in = new FileInputStream("e:/text.txt");
                String str = "";
                int i =0;            
                byte[] b1 = new byte[1024];
                while((i=in.read(b1))>0)
                {
                    str+=new String(b1,0,i);
                }            
                in.close();
                System.out.println("文件读取完成");
                
                //写入
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入您要另存的文件全路径");
                
                String str1 = sc.nextLine();
                FileOutputStream out = new FileOutputStream(str1);
                byte[] b = str.getBytes();
                out.write(b);
                System.out.println("内容添加成功");
                out.close();
            } 
            catch (FileNotFoundException e)
            {        
                e.printStackTrace();
            }
            
        }
    
    }

  • 相关阅读:
    设计模式开篇——7大设计原则
    MySQL MVCC专题
    Spring常考的面试题
    HashMap常考面试题
    Equals和==的比较
    高并发编程
    一文读懂JVM
    scala实现定时任务的方法
    PLAY2.6-SCALA(十二) 表单的处理
    PLAY2.6-SCALA(十一) 模板常用场景
  • 原文地址:https://www.cnblogs.com/HRZJ/p/5914409.html
Copyright © 2011-2022 走看看