IO流和File类的小练习

1.在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去;之后遍历IOTest这个目录下的文件 

 

​
package lianxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class test {

	@Test
	public void test() throws IOException {
		File  f=new File("D:\\HelloWorld.txt");
		//判断HelloWorld是文件还是目录
		if(f.isFile()==true) {
			System.out.println("是文件");
		}else {
			System.out.println("不是文件");
		}
		
		//创建目录IOTest
		File fs=new File("D:\\IOTest");
		fs.mkdirs();
		
		//将HelloWorld.txt移动到IOTest目录下去
		//创建FileInputStream对象,以字节为单位读取文件内容
		FileInputStream f1=new FileInputStream("D:\\HelloWorld.txt");
		FileOutputStream fs1=new FileOutputStream("D:\\IOTest\\HelloWorld.txt");
		int i=0;
		while((i=f1.read())!=-1) {
			fs1.write(i);
		}
		System.out.println("操作成功");
		fs1.close();
		f1.close();
		
		//遍历IOTest目录下的所有文件
		String[] str=fs.list();
		for(String s:str) {
			System.out.println(s);
		}
		System.out.println("遍历结束");
	}
}

​

2.统计一个文件中各个字母出现次数,包括字符出现次数

package lianxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class test2 {

	public static void main(String[] args) throws IOException {
		FileInputStream f=new FileInputStream("D:\\HelloWorld.txt");
		int i=0;
		String sum="";
		while((i=f.read())!=-1) {
			sum+=(char)i;
		}
		f.close();
		String[] str=sum.split("");
		Map<String,Integer> map=new TreeMap<String,Integer>();
		for(int k=0;k<str.length;k++) {
			Integer v=map.get(str[k]);
			if(v==null) {
				map.put(str[k], 1);
			}else {
				map.put(str[k], v+1);
			}
			}
		Set<Entry<String, Integer>> set = map.entrySet();
		for(Entry<String, Integer> en:set) {
			System.out.println(en);
		}
	}

}

3.将一个文本文件内容倒置读出

 

package lianxi;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class test3 {

	public static void main(String[] args) throws IOException {
		FileInputStream f=new FileInputStream("D:\\HelloWorld.txt");
		
		String sum="";
		int i=0;
		while((i=f.read())!=-1) {
			sum+=(char)i;
		}
		System.out.println("内容倒置输出前:"+sum);
	StringBuffer str=new StringBuffer(sum);
	System.out.println("内容倒置输出后:"+str.reverse());
	}

}

4.模拟用户登陆,控制台输入用户名和密码,将用户名密码存储到文件中,并随时可以查询所有用户名及密码,在模拟用户登录,读取文件用户名和密码

package lianxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public class test4 {

	public static void main(String[] args) throws IOException {
		//键盘输入用户名
		System.out.println("请输入用户名");
		//接受键盘输入的用户名
		String userName=new Scanner(System.in).next();
		//键盘输入密码
		System.out.println("请输入密码");
		//接收键盘输入的密码
		String passWord=new Scanner(System.in).next();
		//通过FileOutputStream以字节的为单位存入内容
		FileOutputStream f=new FileOutputStream("D:\\userNameandpassWord.txt");
		//向目标文件写入用户名
		f.write(userName.getBytes());
		//向目标文件写入密码
		f.write("  ".getBytes());
		f.write(passWord.getBytes());
		//提示写入成功
		System.out.println("写入成功");
		//关流
		f.close();
		//通过FileInputStream对象,以字节为单位读文件中的内容
		FileInputStream f1=new FileInputStream("D:\\userNameandpassWord.txt");
		int i=0;
		String str="";
		while((i=f1.read())!=-1) {//读取字节
			str+=(char)i;//写入字节
		}
		f1.close();//关流
		//输出文件中保存的用户名和密码
		System.out.println(str);
	}

}

 

5.从控制台输入一个文件名,判断所在文件夹是否存在该文件,如果不存在,则创建该文件,如果存在,创建一个copy_文件名的文件

 

 

 

 

package lianxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class test5 {

	public static void main(String[] args) throws IOException {
		System.out.println("请输入要查找的文件名");
		String name=new Scanner(System.in).next();
		File f=new File(name);
		if(f.exists()) {
			System.out.println("存在");
			File f1=new File(f.getParent()+"\\copy_"+f.getName());
			f1.createNewFile();
			FileOutputStream f3=new FileOutputStream(f.getParent()+"\\copy_"+f.getName());
			FileInputStream f2=new FileInputStream(f.getParent()+"\\copy_"+f.getName());
			int i=0;
			while((i=f2.read())!=-1) {
				f3.write(i);
			}
			f2.close();
			f3.close();
			System.out.println("创建copy_"+f.getName()+"文件成功");
		}else {
			System.out.println("文件不存在");
			File f3=new File(name);
			f3.createNewFile();
			System.out.println("创建成功");
		}

	}

}

6.在当前目录下创建一个文件“test.txt”,并向文件输出“Hello World”,如果文件已存在,则在原有文件内容后面追加。

 

package lianxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class test6 {

	@Test
	public void test() throws IOException {
		File f=new File("D:\\test.txt");
		//文件中不存在
		FileOutputStream f1=new FileOutputStream("D:\\test.txt");
		f1.write("Hello World".getBytes());
		FileInputStream f2=new FileInputStream("D:\\test.txt");
		int i=0;
		String sum="";
		while((i=f2.read())!=-1) {
			sum+=(char)i;
		}
		System.out.println(sum);
		f2.close();
		f1.close();
		
		//文件中存在,向末尾追加
		StringBuffer str=new StringBuffer(sum);
		str.append("Hello World");
		String n= new String(str); 
		FileOutputStream f3=new FileOutputStream("D:\\test.txt");
		f3.write(n.getBytes());
		f3.close();
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值