java中io练习_关于Java中IO流的练习

练习一:统计一个文件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);

}

}

}

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值