读取文本文件内容的遇到问题--如何合并顺序的数字?

cnhuai 2008-07-17 11:11:58
File file=new File("c:\\");
File[] tempFile=file.listFiles();//读取120文件
for(int i =0;i<tempFile.length;i++)
{
try
{
if(tempFile[i].isFile())
{
FileReader fr=new FileReader("c:\\"+tempFile[i].getName());
BufferedReader br=new BufferedReader(fr);
try
{
while(br.ready())
{
String line=br.readLine();
String[] line1=line.split("\t");

String first=line1[0];
String end=line1[1];

long firstip=Long.parseLong(first);
long endip=Long.parseLong(end); System.out.println(first+"\t"+end);
}
} finally
{
System.out.println("OK");
}
}

}//try
catch(IOException e)
{
System.out.println("error");
}

}


上面程序是读取文本文件的
文本的内容是:
3652113248 3652113263
3652113264 3652113279
3652113280 3652113407
3652113664 3652114175

程序运行的结果是:
3652113248 3652113263
3652113264 3652113279
3652113280 3652113407
3652113664 3652114175
OK

问题是:前三行的数字是连接在一起的,如何直接输出这样的结果:
3652113248 3652113407
3652113664 3652114175

万分感激
...全文
140 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhuai 2008-07-17
  • 打赏
  • 举报
回复
我再仔细说下吧
文本文件中的
3652113248 3652113263
3652113264 3652113279
3652113280 3652113407

代表意思是
3652113248到3652113263之间所有数字
3652113264到3652113279之间所有数字
3652113280到3652113407之间所有数字
3652113664到3652114175之间所有数字

而前三排是连续的
所以想在读取文本文件的时候
同时合并一些连续的:
3652113248 3652113407
daybybyby 2008-07-17
  • 打赏
  • 举报
回复
我承认我理解能力有限。。能不能在说的详细点
cnhuai 2008-07-17
  • 打赏
  • 举报
回复
已经说的很清楚了
3652113248 3652113263
3652113264 3652113279
3652113280 3652113407

前三排 数字是首尾相接的
就直接输出
3652113248 3652113407 即可


lovingprince 2008-07-17
  • 打赏
  • 举报
回复
完全不晓得你说啥子
mim198500 2008-07-17
  • 打赏
  • 举报
回复
try
{
long lastEnd = 0;
while(br.ready())
{
String line=br.readLine();
String[] line1=line.split("\t");

String first=line1[0];
String end=line1[1];

long firstip=Long.parseLong(first);
long endip=Long.parseLong(end);
if(first != (lastEnd + 1)){
System.out.println("\t" + lastEnd);
System.out.print(first);
lastEnd = endip; }
else{
lastEnd =endip;
}
}
System.out.println("\t" + lastEnd);


就是这个意思,会多个零。抛砖引玉了


  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test3 {

public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("aa.txt"));
long n = Long.MIN_VALUE;
for(String line = null; (line = br.readLine()) != null; ) {
String[] str = line.split("\\s+");
long a = Long.parseLong(str[0]);
long b = Long.parseLong(str[1]);
if(n + 1 != a) {
if(n != Long.MIN_VALUE) {
System.out.println(" " + n);
}
System.out.print(a);
}
n = b;
}
System.out.println(" " + n);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
hmsuccess 2008-07-17
  • 打赏
  • 举报
回复
这样,你把读入的每个值,转换为long型的,然后进行比较,连续的就跳过,
happystarting 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cnhuai 的回复:]
to:hmsuccess

是的 把该合并的合并了
[/Quote]
mim198500 2008-07-17
  • 打赏
  • 举报
回复
通过楼上的讲述,我懂了……

我觉得,想学好一种语言,还是先学好 local language
cnhuai 2008-07-17
  • 打赏
  • 举报
回复
to:hmsuccess

是的 把该合并的合并了
hmsuccess 2008-07-17
  • 打赏
  • 举报
回复
意思清楚了,你的意思就是说去掉一段连续数字中的中间部分,只输出两头对吗?
cnhuai 2008-07-17
  • 打赏
  • 举报
回复
大家来帮忙啊 还有不清楚的吗

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧