当前位置: 移动技术网 > IT编程>开发语言>Java > 关于Java中IO流的练习

关于Java中IO流的练习

2018年12月29日  | 移动技术网IT编程  | 我要评论

练习一:统计一个文件calccharnum.txt中字母‘a’和'a'出现的总次数。

package com.test;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

//练习一:统计一个文件calccharnum.txt中字母‘a’和'a'出现的总次数。
/*
 * 读取文件:fileinputstream
 * 判断单个字符出现的次数,一次只能读一个,当读到的内容相符时,相应数量加1
 */
public class testone {

    public static void main(string[] args) {
        // todo auto-generated method stub
        //1.添加文件路径
        file file=new file("e:\\calccharnum.txt");
        //2.创建流,读取文件
        fileinputstream fis=null;
        try {
            fis=new fileinputstream(file);
            int numa=0;
            int numa=0;
            int data=0;
            while((data=fis.read())!=-1) {
                if(new string((char)data+"").equals("a")) {
                    numa++;
                }
                if(new string((char)data+"").equals("a")) {
                    numa++;
                }
            }
            system.out.println("a的个数:"+numa);
            system.out.println("a的个数:"+numa);
            system.out.println("总数:"+(numa+numa));
        } catch (exception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }finally {
            try {
                fis.close();
            } catch (ioexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
    }
}

 

练习二:在电脑e盘下创建一个文件为helloword.txt文件,

    判断它是文件还是目录,

    再创建一个目录iotest,

    之后将helloworld.txt移动到iotest目录下去,

    之后遍历iotest这个目录下的文件。

package com.test;

import java.io.file;
import java.io.ioexception;

/*
 * 练习二:在电脑e盘下创建一个文件为helloword.txt文件,
    判断它是文件还是目录,
    再创建一个目录iotest,
    之后将helloworld.txt移动到iotest目录下去,
    之后遍历iotest这个目录下的文件。
 */
public class testtwo {

    public static void main(string[] args) {
        // todo auto-generated method stub
        //在e盘下创建文件
        file file=new file("e:","heloworld.txt");
        //创建文件
        boolean iscreate;
        try {
            iscreate=file.createnewfile();
            if(iscreate) {
                system.out.println("创建文件成功");
            }else {
                system.out.println("创建文件失败");
            }
        } catch (ioexception e) {
            // todo auto-generated catch block
            system.out.println("创建文件失败");
        }
        //判断是文件还是目录
        if(file.isfile()) {
            system.out.println("这是一个文件");
        }else {
            system.out.println("这是一个目录");
        }
        //创建目录
        file file2=new file("e:/iotest");
        file2.mkdirs();
        //移动文件至目录下
        if(file.renameto(new file("e:/iotest/helloworld.txt"))) {
            system.out.println("文件移动成功");
        }else {
            system.out.println("文件移动失败");
        }
        //遍历目录
        string[] arrs=file2.list();
        for (string string : arrs) {
            system.out.println(string);
        }
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网