Java 文本文件从四列文本文件中删除双打
Java text file remove doubles from four column text file
我必须编写一段代码来获取一个包含整数和双精度数的文本文件,并在一个文件中打印双精度数,在另一个文件中打印整数。文本文件的格式如下:
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
。非常感谢所有帮助。
InputMismatchException
可以抛出,因为它是下界 Integer 或者 Double
最好将一部分作为字符串读取然后决定
当它决定时,它抛出 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();
}
}
根据您提供的代码,您只想拆分文件而不关心 double
和 int
值本身。因此,您可以将文件作为普通文本文件处理,并通过分隔空白字符拆分值。
该代码段对 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());
}
}
}
我必须编写一段代码来获取一个包含整数和双精度数的文本文件,并在一个文件中打印双精度数,在另一个文件中打印整数。文本文件的格式如下:
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
。非常感谢所有帮助。
InputMismatchException
可以抛出,因为它是下界 Integer 或者 Double
最好将一部分作为字符串读取然后决定
当它决定时,它抛出 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();
}
}
根据您提供的代码,您只想拆分文件而不关心 double
和 int
值本身。因此,您可以将文件作为普通文本文件处理,并通过分隔空白字符拆分值。
该代码段对 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());
}
}
}