在文件中随机插入100个整数,并找出第二十大的数值

此方法仅用于文本文件

import java.io.*;
public class FindMaxOfFile {
	//程序入口
	public static void main(String[] args) throws IOException {			
		WriteToFile();//随机生成100个数值并写入文件中的随机位置
		CatchDate();//读取写入的100个数,并求出第20大的数值
	}
	//写入文件方法
		private static void WriteToFile() throws IOException {
			RandomAccessFile f = new RandomAccessFile("H:/t - 副本.txt", "rw");
			String s;//将s插入文件之中
			int i;// 100个数for
			int k = 0;
			for (i = 0; i < 100; i++) {
				k += ((int) (Math.random() * 10000)) % 100;// 上次保留至+随机移动位置				
				f.seek(k);//进行移动
				s = ConnectS();//得到一个275???39的字符串
				s += f.readLine() + "\n";//将光标之后的所有内容链接到s之后并换行
				f.seek(k);//回到插入时的位置
				f.writeBytes(s);//字符串整体插入文件位置
				k += 30;//移动30个位置
				f.seek(k);
			}
			f.close();//关闭
		}
	static //读取插入数值方法
		int K = 0;
	private static void CatchDate() throws IOException {
		int a[] = new int[20];//用以存放最大的20个数值
		BufferedReader br = new BufferedReader(new FileReader("H:/t - 副本.txt"));
		//按行读取流
		String s;//接受文件中的任一行
		int n = 0;//计数,n<=19
		while ((s = br.readLine()) != null&&K<100) {//文件读完结束
			n = Match(a, s, n);//在任一行中寻找是否有满足275???39的字符串
		}
		br.close();//关闭流
		PrintArr(a, 20);//输出数组
		System.out.println("第二十大的数:" + a[19]);//输出第二十大的数值
	}
	//匹配方法
	private static int Match(int[] a, String s, int n) {
		while (s.length() > 5) {//字符串长度<5时必定不满足275???39
			String t = s.substring(0, 3);//截取子串前三位
			if (t.equals("275")) {//满足字符串前缀
				t = s.substring(0, 8);//整体获取
				String k = t.substring(6, 8);// 截取字符串后缀 39
				if (k.equals("39")) {//判定是否满足后缀
					// 此处可以确定前缀与后缀均满足插入数值要求
					int a1 = stringToInt(t);// 将子串转换为当时插入时的数值即???三位数
					n = InsertArr(a, a1, n);// 将找到的数按照降序插入数组
				}
			}
			s = s.substring(1); // 整个字符串逐一后移一位
		}
		return n;//返回已满足最大数
	}
	//数组输出方法
	private static void PrintArr(int[] a, int n) { //n=20
		int i;
		for (i = 0; i < n; i++) {
			System.out.print("a["+i+"]="+a[i] + "\t");
			if((i+1)%5==0)//每当输出五个数就换行
				System.out.println();	
		}
	}
	//按降序顺序插入数组方法
	private static int InsertArr(int[] a, int k, int n) {
		int i = 0, j;
		while (a[i] > k && i < n)//确定要插入的位置
			i++;
		if (n == 20)
			j = 19;
		else
			j = n;
		while (j > i) {//所在位置之后所有的值统一后移一位
			a[j] = a[j - 1];
			j--;
		}
		if (a[j] < k)//当最后一个数值小于要插入的数值时,便进行插入操作
			a[j] = k;
		if (n == 19)
			return 19;
		else
			return n + 1;
	}
	//字符串转整形方法
	private static int stringToInt(String t) {
		Integer a;
		a = Integer.valueOf(t);
		int k = (a.intValue() - 27500039) / 100;
		return k;
	}
	//连接数值前缀后缀方法
	private static java.lang.String ConnectS() {
		String s = "275";
		int a = (int) (Math.random() * 10000) % 1000;
		if (a < 10)
			s += "00";
		else if (a < 100)
			s += 0;
		s += a + "39";//生成275???39字符串,其中?代表任意一位数(0,9)
		return s;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值