zoukankan      html  css  js  c++  java
  • 编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。

    package lianxi;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class IOdemo {
    
        public static void main(String[] args)
        {
            String a = "d:/java" ;
            String b ="extRw.txt" ;
            File file = new File(a,b) ;
            File c = new File(a) ;
            
            //判断目录是否存在
            if(!c.exists())
            {
                c.mkdirs();
            }
            
            try
            {
                //创建
                file.createNewFile() ;
                
                //输出流
                FileOutputStream out = new FileOutputStream(file) ;
                
                //写入内容
                String st = "123456;
    付士亮";
                
                //转换到byte[]
                byte[ ]  by = st.getBytes( ) ;
                
                //写入
                out.write(by);
                
                //关闭流,释放资源
                out.close();
                
                System.out.println("写入成功!");
                
                //输入流
                FileInputStream in = new FileInputStream("d:/java/extRw.txt") ;
                
                //装在数据的数组,
                byte[ ]  be = new byte[1024] ;        //1k大小
                int i =0;
                
                String s ="" ;
                
                while((i=in.read(be))>0)
                {
                    //组合数据
                    //参数1 :0 起始位置, i  : 数据长度   
                    s +=new String(be,0,i)  ;
                }
                
                System.out.println("输入成功!");
                
                System.out.println("s=" +s);
                
                in.close();
                
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            
            
        }
    
    }

  • 相关阅读:
    抽象类和接口
    回调函数
    Spring Aop、拦截器、过滤器的区别
    事务
    SQL 模糊查询条件的四种匹配模式
    shell编程(二)
    shell编程(一)
    shell介绍
    字符验证码
    selenium
  • 原文地址:https://www.cnblogs.com/20gg-com/p/5913781.html
Copyright © 2011-2022 走看看