IO

1.复习File、字节流与字符流的相关知识,掌握File文件操作的核心步骤,掌握使用二进制流进行文件拷贝的代码。

File文件操作核心步骤:

public class Test {
	public static void main(String[] args) {
		//通过文件路劲取得File对象
		File file = new File("C:"+File.separator+"Users"+File.separator+
				                "Lenovo"+File.separator+"Desktop"+File.separator+"test.txt");//C:\Users\Lenovo\Desktop
		//判断文件是否存在,是否为文件
		if(file.exists() && file.isFile()) {
		   //删除文件
			file.delete();
		}else {
			//创建文件
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
     }

}

使用二进制流进行文件拷贝代码

public class Test{
	public static void main(String[] args) throws Exception {
		String sourcePath = "图片.jpg";
		String destPath = "图片1.jpg";
		System.out.println(fileCopy(sourcePath, destPath)?"文件拷贝成功":"文件拷贝失败");
	}
	
	private static boolean fileCopy(String sourcePath, String destPath) throws Exception {
         //通过文件路径取得File对象
		File infile = new File(sourcePath);
		File outfile = new File(destPath);

		//取得File对象的输入输出流(拿到管道)
        //读取数据到程序中;拿到文件输出流,将程序中数据输出到目标文件
		InputStream in = new FileInputStream(infile);
		OutputStream out = new FileOutputStream(outfile);
		return fileCopyHandle(in, out);
	}
	
	private static boolean fileCopyHandle(InputStream in, OutputStream out) throws Exception {
		long start = System.currentTimeMillis();
		int len = 0;
		//开辟缓冲区
		byte[] data = new byte[1024];

		while((len = in.read(data)) != -1) {
			out.write(data);
		}
		long end = System.currentTimeMillis();
		System.out.println("拷贝文件的时间花费: "+(end-start));
		in.close();
		out.close();
		return true;
	}
}



2.掌握打印流以及Scanner类的常用方法,使用打印流和Scanner优化之前的FileInputStream与FileOutputStream操作。要求:在桌面上新建一个Test.txt,使用打印流向文件中输出如下:

Hello 123
hello bit

class PrintUtil{
	private OutputStream out;
	//外部传入要输出的目标终端
	public PrintUtil(OutputStream out){
		this.out=out;
	}
	
	public void print(String str){
		try{
			this.out.write(str.getBytes());
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void println(String str){
		this.print(str);
	}
//	
}

public class Test{
	public static void main(String[] args)throws Exception{
		PrintUtil printUtil=new PrintUtil(new FileOutputStream("C:"+File.separator+"Users"+File.separator+
				"Lenovo"+File.separator+"Desktop"+File.separator+"test.text"));
		printUtil.println("hello123 ");
		printUtil.println("hellobit");
	}
}



然后使用Scanner类读取文件内容并输入到控制台。

public class Test{
	public static void main(String[] args)throws Exception{
		Scanner scanner=new Scanner(new FileInputStream("C:"+File.separator+"Users"+File.separator+
			       "Lenovo"+File.separator+"Desktop"+File.separator+"test.text"));
		scanner.useDelimiter("\n");//自定义分隔符
		while(scanner.hasNext()){
			System.out.println(scanner.next());
		}
		scanner.close();
	}
}


3.复习序列化与反序列化的概念,要求自定义Person类,其中三个属性name,age,school.
age属性不作为序列化保存而其他两个属性使用序列化保存在本地文件TestSer.txt中。
使用序列化和反序列化的方式将自定义类序列化与反序列化操作。

4.算法:
输入一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

public class Test{
	public static void main(String[] args){
		System.out.println("请输入一个五位数:");
		Scanner scanner=new Scanner(System.in);
		int num=scanner.nextInt();
		
		if(num<10000 || num>99999){
			System.out.println("error");
			return ;
		}
		int tenthousand=num/10000;
		int thousand=num/1000%10;
		int ten=num/10%10;
		int bit=num%10;
		if(ten==thousand && bit == tenthousand){
			System.out.printf("是回文数");
		}
		else{
			System.out.printf("不是回文数");
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值