Java文本文件从四列文本文件中删除双精度数

时间:2015-04-28 19:47:13

标签: java io text-files

我必须编写一个代码来获取一个包含整数和双精度的文本文件,并在文件中打印双打,在另一个文件中打印整数。文本文件的格式如下:

double int int int  
double int int int  
...  
double int int int

它保存在"raw.txt"文件中。

输出应如下所示:

int int int  
int int int   
...  
int int int  

这是我到目前为止所尝试的:

import java.io.*;
import java.util.*;

public class DATA {
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(new File("sorted.txt"));
        Scanner reader = new Scanner(new File("raw.txt"));
        int temp = 0, count = 1;
        while (reader.hasNext()) {
            try {
                temp = reader.nextInt();
            }
            catch (InputMismatchException e) {
                reader.nextLine();
                temp = (int) reader.nextDouble();
            }
            writer.print(temp);
            if (count % 4 == 0) 
                writer.println(); 
            count++;    

        }
        writer.close();
        reader.close();
    }
}

当前代码抛出InputMismatchException。非常感谢所有帮助。

2 个答案:

答案 0 :(得分:2)

根据您提供的代码,您只想拆分文件,而不关心doubleint值本身。因此,您可以将文件作为普通文本文件处理,并使用分隔空白字符分割值。

该代码段对raw.txt文件的格式做了一些假设,并未进行优化。因此,根据您的需求进行修改应该是一件容易的事。

public static void main(String[] args) throws IOException {
    List<String> rawLines = Files.readAllLines(Paths.get("raw.txt"));
    try (Writer intWriter = Files.newBufferedWriter(
            Paths.get("int_values.txt"),
            StandardOpenOption.CREATE_NEW);
         Writer doubleWriter = Files.newBufferedWriter(
            Paths.get("double_values.txt"),
            StandardOpenOption.CREATE_NEW)) {
        for (String line : rawLines) {
            // the split might need to be amended if the values
            // are not separated by a single blank
            String[] values = line.split(" ");

            // to be amended if there are not alway four values in a row
            if (values.length != 4) {
                continue;
            }

            doubleWriter.write(values[0]);
            doubleWriter.write(System.lineSeparator());

            intWriter.write(values[1]);
            intWriter.write(' ');
            intWriter.write(values[2]);
            intWriter.write(' ');
            intWriter.write(values[3]);
            intWriter.write(' ');
            intWriter.write(System.lineSeparator());
        }
    }
}

答案 1 :(得分:1)

InputMismatchException可以被抛出,因为它是双重整数 将部件作为字符串读取然后再决定是更好的 当它决定时,它会抛出NumberFormatException,这可以被捕获 在下面的代码中,有两个编写器按您的意愿分开,它可能看起来比这个代码更好 我已经纠正了你的写作文件。我没有测试过它,但我真的认为如果你做writer.print(temp);,它会把所有整数都没有空格,那就没用了。
试试这段代码,但未经过测试

import java.io.*;
import java.util.*;

public class DATA {
     public static void main(String[] args) throws FileNotFoundException {
        PrintWriter writerInt = new PrintWriter(new File("sortedInt.txt"));
        PrintWriter writerDou = new PrintWriter(new File("sortedDou.txt"));
        Scanner reader = new Scanner(new File("raw.txt"));
        int temp = 0, countInt = 1, countDou = 1;
        while (reader.hasNext()) {
            String next = reader.next();
            try{
                temp=Integer.parseInt(next);
                writerInt.print(temp+" ");
                if (countInt % 4 == 0) 
                    writerInt.println(); 
                countInt++;    
            }catch(NumberFormatException e){
                try{
                    writerDou.print(Double.parseDouble(next)+" ");
                    if (countDou % 4 == 0) 
                        writerDou.println(); 
                    countDou++;   
                }catch(NumberFormatException f){
                    System.out.println("Not a number");
                }
            }

        }
        writerInt.close();
        writerDou.close();
        reader.close();
    }
}