首页 » Java » java 程序题

java 程序题

7885 3

//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; } }

文章评分1次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2009/11/java-%e7%a8%8b%e5%ba%8f%e9%a2%98/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关:


3 Comments

  1. 昨天刚看java到60页,对于上面代码头晕。呵呵
    祝工作顺利

    • tiandi :

      感谢这第一份的留言。
      这些题目,都是我当初上学考试的题目,java还是相对比较简单的。

载入分页评论...