Java 将两个带整数的文本文件合并为一个文本文件
我需要将两个文本文件合并成一个合并文本文件。文件仅由数字组成,然后必须按升序列出数字。我已经把它编码成这样,但是,我不能让它加上最后一个数字,它给了我一个numberformatexception错误。我相信这是因为我的上一个数字没有任何东西可以比较,所以我无法将其添加到列表中。我不知道如何添加最后一个数字,当它本身也没有什么可比较的时候(我很确定我需要另一个if语句,我只是不知道如何做),我不知道正确的while语句是什么,但是程序使用我使用的while语句正确运行,没有最后一个数字Java 将两个带整数的文本文件合并为一个文本文件,java,Java,我需要将两个文本文件合并成一个合并文本文件。文件仅由数字组成,然后必须按升序列出数字。我已经把它编码成这样,但是,我不能让它加上最后一个数字,它给了我一个numberformatexception错误。我相信这是因为我的上一个数字没有任何东西可以比较,所以我无法将其添加到列表中。我不知道如何添加最后一个数字,当它本身也没有什么可比较的时候(我很确定我需要另一个if语句,我只是不知道如何做),我不知道正确的while语句是什么,但是程序使用我使用的while语句正确运行,没有最后一个数字 publ
public static void main(String[] args)
{
FileReader file1 = null;
FileReader file2 = null;
BufferedReader readfile1 = null;
BufferedReader readfile2 = null;
FileWriter fileout = null;
PrintWriter dataout;
String fname1 = "list1.txt";
String fname2 = "list2.txt";
int md = 0;
int file1num = 0;
int file2num = 0;
String file1str;
String file2str;
try
{
file1 = new FileReader(fname1);
}
catch
(FileNotFoundException xyz)
{
System.out.println("File not found: " + fname1);
System.exit(-1);
}
catch
(IOException abc)
{
System.out.println("IOException: caught");
System.exit(-1);
}
readfile1 = new BufferedReader(file1);
try
{
readfile2 = new FileReader(fname2);
}
catch
(FileNotFoundException xyz)
{
System.out.println("File not found: " + fname2);
System.exit(-1);
}
catch
(IOException abc)
{
System.out.println("IOException: caught");
System.exit(-1);
}
readfile2 = new BufferedReader(file2);
try
{
fileout = new FileWriter("merged.txt");
}
catch(IOException adc)
{
System.out.println("file error");
System.exit(-1);
}
dataout = new PrintWriter(fileout);
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
file2str = file2.readLine();
file2num = Integer.parseInt(file2str);
while(md !=-1)
{
if(file1num<file2num)
{
md=file1num;
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
}
if(file2num<file1num)
{
md=file2num;
file2str = file2.readLine();
file2num = Integer.parseInt(file2str);
}
if(file1num==file2num)
{
md=file1num;
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
}
}
文件2:
3
5
6
8
9
预期输出:
1
2
3
3
4
5
6
6
8
8
9
您尝试使用trim()
Integer.parseInt(blabla.trim())代码>因为您的数字有一个或多个空格字符,所以解析失败
示例代码:
publicstaticvoidmain(字符串[]args)引发畸形的DurLexException、IOException{
File file1 = new File("C:\\Users\\Melih\\Desktop\\file1.txt");
File file2 = new File("C:\\Users\\Melih\\Desktop\\file2.txt");
File output = new File("C:\\Users\\Melih\\Desktop\\file3.txt");
BufferedReader file1Reader = new BufferedReader(new FileReader(file1));
BufferedReader file2Reader = new BufferedReader(new FileReader(file2));
PrintWriter writer = new PrintWriter(output);
String file1Num = file1Reader.readLine();
String file2Num = file2Reader.readLine();
while (file1Num != null && file2Num != null) {
int num1 = Integer.parseInt(file1Num.trim());
int num2 = Integer.parseInt(file2Num.trim());
if (num1 < num2) {
writer.println(num1);
file1Num = file1Reader.readLine();
} else if (num2 < num1) {
writer.println(num2);
file2Num = file2Reader.readLine();
} else {
writer.println(num1);
file1Num = file1Reader.readLine();
}
}
while (file1Num != null) {
writer.println(file1Num);
file1Num = file1Reader.readLine();
}
while (file2Num != null) {
writer.println(file2Num);
file2Num = file2Reader.readLine();
}
file1Reader.close();
file2Reader.close();
writer.close();
File file1=新文件(“C:\\Users\\Melih\\Desktop\\file1.txt”);
File file2=新文件(“C:\\Users\\Melih\\Desktop\\file2.txt”);
文件输出=新文件(“C:\\Users\\Melih\\Desktop\\file3.txt”);
BufferedReader file1Reader=新BufferedReader(新文件读取器(file1));
BufferedReader file2Reader=新BufferedReader(新文件读取器(file2));
PrintWriter=新的PrintWriter(输出);
字符串file1Num=file1Reader.readLine();
字符串file2Num=file2Reader.readLine();
while(file1Num!=null&&file2Num!=null){
int num1=Integer.parseInt(file1Num.trim());
int num2=Integer.parseInt(file2Num.trim());
如果(num1
}假设您有两个列表,其中包含第一个和第二个文件的整数值,
如果将这两个文件都读入列表,例如lst1和lst2,那么合并到lst3应该很容易:
Integer a1 = null;
Integer a2 = null;
while (!lst1.isEmpty() || !lst2.isEmpty()){
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){}
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){}
if(a1 == null && a2 != null) lst3.add(a2);
else if(a1 != null && a2 == null) lst3.add(a1);
else{
while(a1 < a2){
lst3.add(a1);
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){break;}
}
while(a1 >= a2){
lst3.add(a2);
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){break;}
}
while(a1 != null && lst2.isEmpty()){
lst3.add(a1);
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){break;}
}
while(a2 != null && lst1.isEmpty()){
lst3.add(a2);
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){break;}
}
}
}
整数a1=null;
整数a2=null;
而(!lst1.isEmpty()| |!lst2.isEmpty()){
试一试{
a1=(整数)lst1.remove(0);
}
捕获(例外e){}
试一试{
a2=(整数)lst2.remove(0);
}
捕获(例外e){}
如果(a1==null&&a2!=null)lst3.add(a2);
如果(a1!=null&&a2==null)lst3.add(a1),则为else;
否则{
而(a1=a2){
lst3.添加(a2);
试一试{
a2=(整数)lst2.remove(0);
}
catch(异常e){break;}
}
while(a1!=null&&lst2.isEmpty()){
lst3.添加(a1);
试一试{
a1=(整数)lst1.remove(0);
}
catch(异常e){break;}
}
while(a2!=null&&lst1.isEmpty()){
lst3.添加(a2);
试一试{
a2=(整数)lst2.remove(0);
}
catch(异常e){break;}
}
}
}
然后您可以将lst3
的内容写入输出文件。
免责声明:我使用记事本编写了代码-您可能需要“修补”它一点;)当一个文件或另一个文件用完时,你需要考虑该怎么办。< /P>
readLine()在EOF上返回一个null,因此您将查找null
逻辑是这样的
str1 = read file1
num1 = parse( str1 ) //parse() should be a method that can properly handle a null
str2 = read file2 //and return something appropriate, like -1, when null
num2 = parse( str2 ) //is encountered
while str1 != null || str2 != null
if str2 == null || num1 < num2
write num1
str1 = read file1
num1 = parse( str1 )
else if str1 == null || num1 > num2
write num2
str2 = read file2
num2 = parse( str2 )
else if num1 == num2
write num1
write num2
str1 = read file1
num1 = parse( str1 )
str2 = read file2
num2 = parse( str2 )
str1=读取文件1
num1=parse(str1)//parse()应该是一个可以正确处理null的方法
str2=读取file2//并在null时返回适当的值,如-1
num2=parse(str2)//遇到
而str1!=空| | str2!=无效的
如果str2==null | | num1num2,则为else
写入num2
str2=读取文件2
num2=parse(str2)
如果num1==num2,则为else
写入num1
写入num2
str1=读取文件1
num1=解析(str1)
str2=读取文件2
num2=parse(str2)
您应该提供两个输入文件和一个预期输出文件的示例这就是全部代码吗?在哪里生成输出?输出被放入第三个合并文件中@alfasin我不知道哪里有一个附件按钮,但例如文件1是1234689,文件2是356689(每行一个数字)。预期的输出将是1 2 3 3 4 5 6 8 8 9(同样每行1个数字)。如果这不是一个类练习,则可以使用排序
命令轻松完成。看起来将这两个文件读入一个数组列表
(例如),然后使用集合.sort()
更容易,感谢这项工作,我唯一需要添加的是在每个语句中添加一个嵌套的if语句,这样,如果str1为null,它就不会执行num1=parse(str1)
str1 = read file1
num1 = parse( str1 ) //parse() should be a method that can properly handle a null
str2 = read file2 //and return something appropriate, like -1, when null
num2 = parse( str2 ) //is encountered
while str1 != null || str2 != null
if str2 == null || num1 < num2
write num1
str1 = read file1
num1 = parse( str1 )
else if str1 == null || num1 > num2
write num2
str2 = read file2
num2 = parse( str2 )
else if num1 == num2
write num1
write num2
str1 = read file1
num1 = parse( str1 )
str2 = read file2
num2 = parse( str2 )