io操作文件和回文比较

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

public class Test {
 public static void a(){
  File srcFile = new File("D:/a.gif");
  File dstFile = new File("D:/b.gif");
  BufferedInputStream in =null;
  BufferedOutputStream out =null;
  
  try {
   in  = new BufferedInputStream(new FileInputStream(srcFile));
   out = new BufferedOutputStream(new FileOutputStream(dstFile));
   byte []b=new byte[10];
   int len = -1;

   while((len=in.read(b))!=-1){
    out.write(b,0,len);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(in!=null){
    try {
     in.close();
    } catch (Exception e2) {
     e2.printStackTrace();
    }
   }
   if (out!=null) {
    try {
     out.flush();
     out.close();
    } catch (Exception e2) {
     e2.printStackTrace();
    }
    
   }
  }
 }
 public static void main(String[] args) {
  //a();
   // TODO Auto-generated method stub    
  //从键盘上输入一个字符串str 
  String str=""; 
  System.out.println("请输入一个字符串:");
  Scanner in=new Scanner(System.in); 
  str=in.nextLine();
  //根据字符串创建一个字符缓存类对象sb 
  StringBuffer sb=new StringBuffer(str);
  //将字符缓存中的内容倒置
  sb.reverse();
  //计算出str与sb中对应位置字符相同的个数n 
  int n=0; 
  for(int i=0;i<str.length();i++){ 
   if(str.charAt(i)==sb.charAt(i))  
    n++;
   }
  //如果所有字符都相等,即n的值等于str的长度,则str就是回文。   
  if(n==str.length())    
   System.out.println(str+"是回文!");   
  else     
   System.out.println(str+"不是回文!");
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值