📄 littleprogramapplet.java
字号:
}else
throw new NumberNotFoundException();
Collection c = mp.values();
Iterator iter = c.iterator();
while(iter.hasNext())
{
s1 = (String)iter.next();
s3 +=s1+"\n";
}
BufferedReader in2 =
new BufferedReader(
new StringReader(s3));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("data.txt")));
//System.out.println("delete No"+deleteNumber);
while( (s1 = in2.readLine())!=null)
{
out.println(s1);
}
out.close();
}catch (NumberNotFoundException e)
{
isDelete = false;
System.out.println(deleteNumber+" no found :(");
}
}catch(IOException e){}
}
public long numberTokenizer(String s)
throws IOException
{
StringTokenizer st =
new StringTokenizer(s," ");
return Integer.valueOf((st.nextToken())).longValue();
}
public void findData(long find)//查找数据
throws IOException,NumberNotFoundException
{
isFind = true;
String s = "",findString ="";
long i;
DataMap dm = new DataMap();
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
//in.close();
try{
if(dm.containsKey( String.valueOf(find).toString()))
{
findString = dm.get(find);
System.out.println("学号"+find+"学生的资料是:");
System.out.println(findString);
}else
throw new NumberNotFoundException();
}catch (NumberNotFoundException e){
System.out.println(find+" no found :(");
isFind = false;
}
}
public static void print()//读取文本文件把数据打印到终端的方法
throws IOException
{
try{
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
String read = "";
while ((read = in.readLine())!=null)
System.out.println(read);
}catch(IOException e){}
}
public static void timeOut(double sec)//停顿短暂时间的一个方法完全可以不要这个功能
{
double seconds = sec;
long t = System.currentTimeMillis()+(int)(seconds*1000);
while ((System.currentTimeMillis())<t)
;
}
public void numSort()//按学号排序
throws IOException
{
long i = 0;
String s = "";
try{
DataArrayList dal = new DataArrayList();
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
Collections.sort(dal);
DataMap dm = new DataMap();
BufferedReader in2 =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in2.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
PrintWriter out =
new PrintWriter (
new BufferedWriter(
new FileWriter("data.txt")));
Iterator it = dal.iterator();
long temp = 0;
String tempStr = "";
while (it.hasNext())
{
temp = Integer.valueOf((String)it.next()).longValue();
tempStr = dm.get(temp);
out.println(tempStr);
}
out.close();
}catch(IOException e){}
}
public void rewrite()
throws IOException,NumberNotFoundException
{
try{
System.out.print("请输入你要修改的学生学号:");
BufferedReader in =
new BufferedReader (
new InputStreamReader(System.in));
String inputLine = in.readLine();
long num = Integer.valueOf(inputLine).longValue();
findData(num);
if(isFind)
{
deleteData(num);
System.out.print("请重新输入该学生的资料:");
String str = inputData();
addData(str);
System.out.println("rewrite true!");
}
}catch(IOException e){}
catch(NumberNotFoundException e){}
}
public int count()
throws IOException
{
DataArrayList dal = new DataArrayList();
try{
String s = "";
long i =0;
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
}catch(IOException e){}
return dal.size();
}
}
/*
*
* @author RangWei
* TODO 这是个我们写的一个容器,继承公共类HashMap
* 大概的功能就相当一个数组
*
*/
class DataMap extends HashMap//一个存储数据的Map
{
public void put(long i,String str)//把学号和数据放进这个Map
{ //以后一个学号(key)对应的是一个人的数据(value)
put(String.valueOf(i).toString(),str);
}
public void remove(long i)//接收学号,然后删除学号(key)和它对应的数据(value)
{
remove(String.valueOf(i).toString().toString());
}
public String get(long i)//接收一个学号,然后返回这个key对应的value
{
String s = String.valueOf(i).toString();
if (!containsKey(s))
{
System.err.println("Not found Key: "+s);
}
return (String)get(s);
}
}
/*
*
* @author RangWei
*
* TODO 这个类继承ArrayList
* 用来按数字排序,在用学号排序时要用到它
*
*/
class DataArrayList extends ArrayList
{
public void add(long num)
{
String numToString = String.valueOf(num).toString();
add(numToString);
}
}
/*
*
* @author RangWei
*
* TODO 增加的一个Exception,主要是在文件里没有要找
* 的学号就抛出
*
*/
class NumberNotFoundException extends Exception
{
public NumberNotFoundException()
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -