java 程序题

//1.编写一个程序,该程序可读入一个外部文件,并将文件内容赋值给一个字符串
package Zhangyiwei

import java.io.*;

public class Zhangyiwei {
    public static String filename = "c://1.txt";
    public static void main(String[] args) throws FileNotFoundException, IOException  {
         System.out.println("File:"+File2String(filename));
    }
    /**
    * 函数:File2String
    * 功能:可读入一个外部文件,并将文件内容赋值给一个字符串
    * @param file 文件路径
    * @return 字符串内容
    * @since 1.0
    */
    public static String File2String(String filename) throws FileNotFoundException, IOException{
        char[] context = new char[(int)new File(filename).length()];
        FileReader filereader = new FileReader(filename);
        filereader.read(context);
        filereader.close();
        return new String(context);
    }
}

//2.编写一个程序,该程序可将一个字符串写入外部文件
package Zhangyiwei;

import java.io.*;

public class Zhangyiwei {
    public static String filename = "c://1.txt";
    public static String context = "This is a file made by java.";
    public static void main(String[] args) throws IOException  {
         String2File(filename,context,false);
    }
    /**
    * 函数:String2File
    * 功能:将字符串内容赋值给一个外部文件
    * @param filename 文件路径
    * @param context 字符串内容
    * @param expand 如果目标文件存在,是否在文件尾部追加字符串内容而不是覆盖源文件
    * @return null
    * @since 1.0
    */
    public static void String2File(String filename,String context,boolean expand) throws IOException {
        FileWriter filewriter = new FileWriter(new File(filename),expand);
        filewriter.write(context);
        filewriter.close();
        System.out.println("Done!");
    }
}

//3.编写一个程序,该程序可将一个现有的文件压缩到一个ZIP压缩文件中
package Zhangyiwei;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zhangyiwei {
    public static String filename = "1.txt";
    public static String zipfilename = "c://1.zip";
    public static void main(String[] args) throws FileNotFoundException, IOException {
         ZipFile(filename,zipfilename);
    }
    /**
    * 函数:ZipFile
    * 功能:将文件ZIP
    * @param filename 被压缩文件
    * @param zipfilename 压缩后zip文件
    * @return null
    * @since 1.0
    */
    public static void ZipFile(String filename,String zipfilename) throws FileNotFoundException, IOException{
        byte buffer[]=new byte[1024];
        int len;
        File infile = new File("c://"+filename);
        ZipOutputStream zipfile =new ZipOutputStream(new FileOutputStream(zipfilename));
        ZipEntry zipentry = new ZipEntry(filename);
        zipfile.putNextEntry(zipentry);
        BufferedInputStream is=new BufferedInputStream(new FileInputStream(infile));
        while((len = is.read(buffer)) !=-1)   {
                    zipfile.write(buffer,0,len);              
                    }
        zipfile.close();
        System.out.println("Zip done!");
    }
}

//4.利用线程编写一个程序,该程序可以监视硬盘上某一个文件a.txt,一旦该文件的长度发生变化,系统立刻通过控制台输出字符串报警
package Zhangyiwei

import java.io.*;
import java.util.Timer;
import java.util.TimerTask;

public class Zhangyiwei {
    public static File file ;
    public static int size1 ;
    public static int size2 ;
    public static int duration ;
    public static void main(String[] args)  {
        file = new File("c://a.txt");
        duration = 10000;
        CheckFile(duration,file);
    }

    /**
    * 函数:CheckFile
    * 功能:定时检查指定文件大小是否改变
    * @param duration 间隔时间,1秒 = 1000
    * @param file 文件路径
    * @return null
    * @since 1.0
    */
    public static void CheckFile(int duration,File file){
        size1 =(int) file.length();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerMainEvent(),100,duration);
    }
}
    class TimerMainEvent extends TimerTask {
            public TimerMainEvent() {
            }
        @Override
        public void run() {
        Zhangyiwei.size2 = (int) Zhangyiwei.file.length();
        if (Zhangyiwei.size2 != Zhangyiwei.size1){
            System.out.println("File size is changed from "+Zhangyiwei.size1+" to "+Zhangyiwei.size2+"!");
            Zhangyiwei.size1 = Zhangyiwei.size2;
        }
        else
                System.out.println("File size is not changed!");
    }
}

//5.使用collection标准类库实现“I have a book""I have a pen"两个集合的并集,交集,减法运算
package Zhangyiwei;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class Zhangyiwei {
    public static String a[] = {"I","have","a","book"};
    public static String b[] = {"I","have","a","pen"};
    public static void main(String[] args)  {
        Collection c1 = new ArrayList(1);
        c1.add(a[0]);
        c1.add(a[1]);
        c1.add(a[2]);
        c1.add(a[3]);
        Collection c2 = new ArrayList(1);
        c2.add(b[0]);
        c2.add(b[1]);
        c2.add(b[2]);
        c2.add(b[3]);
        System.out.println("Combination Result: "+getCombination(c1,c2));
        System.out.println("Intersection Result: "+getIntersection(c1,c2));
        System.out.println("Remove b from a Result: "+getRemove(c1,c2));
    }
    /**
    * 函数:getCombination
    * 功能:求两集合的并集
    * @param a 集合一
    * @param b 集合二
    * @return 并集
    * @since 1.2
    */
      public static Collection getCombination(Collection a,Collection b){
      HashSet hs = new HashSet(a);
      hs.addAll(b);
      return hs;  
  } 
    /**
    * 函数:getIntersection
    * 功能:求两集合的交集
    * @param a 集合一
    * @param b 集合二
    * @return 交集
    * @since 1.2
    */
      public static Collection getIntersection(Collection a,Collection b){
      HashSet hs= new HashSet(a);
      hs.retainAll(b);  
      return hs;
  } 
    /**
    * 函数:getRemove
    * 功能:求两集合相减
    * @param a 集合一
    * @param b 集合二
    * @return 相减
    * @since 1.2
    */
      public static Collection getRemove(Collection a,Collection b){
      HashSet hs= new HashSet(a);
      hs.removeAll(b);
      return hs;
  }
}
//6.现有数据库(9.6.7.3.4.5.2.1)请使用Collections对数据集排序并求出数据集中的最大值最小值

package Zhangyiwei;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Zhangyiwei {
    public static void main(String[] args)  {
        int array[]={9,6,7,3,4,5,2,1};
        int i;
        List list = new ArrayList();
        for(i=0;i<8;i++){
            System.out.print(array[i]);
            list.add(array[i]);
        }
        collectionSort(list);
        System.out.println("/nAfter sort:");
        Iterator it= list.iterator();  
        while(it.hasNext())
        {
             System.out.print(it.next());
        }
        System.out.println("/nmax value is"+collectionMax(list));
        System.out.println("min value is"+collectionMin(list));
    }
    /**
    * 函数:collectionSort
    * 功能:列表按升序排序
    * @param list 列表
    * @since 1.2
    */
    private static  List collectionSort(List list){
        Collections.sort(list);
        return list;
    }
    /**
    * 函数:collectionMin
    * 功能:求列表中最小值
    * @param list 列表
    * @since 1.2
    */
    private static int collectionMin(List list){
        int min= Collections.min(list);
        return min;
    }
    /**
    * 函数:collectionMin
    * 功能:求列表中最大值
    * @param list 列表
    * @since 1.2
    */
    private static double collectionMax(List list){
        int  max= Collections.max(list);
        return max;
    }  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值