我正在用Java编写一个程序,该程序仅读取输入文件的内容并将内容写入另一个文件。该程序将读取和写入整数,并跳过所有其他内容。我遇到的问题是,读取非整数时程序将退出。
我需要程序读取所有整数,跳过所有其他内容,继续读取整数,并将所有整数写入文件,并将非整数回显到终端。
输入文件包含以下内容:
2
1023
-56
84781
0
-9371
Rats
10
5
如果从输入文件中删除了Rats,该程序将运行,但是我不确定是否所有整数都已写入文件中。该文件是一个二进制文件,当我运行“ od outputFileName.bin”时,该文件显示在终端上:
0000000 000000 002400
0000004
我希望能看到更多。如果我更改代码以使用
写入文本文件input.nextLine();
output.println(line);
所有整数将写入输出文件。
这是我的代码:
import java.io.*;
import java.util.Scanner;
class DataTranslator {
public static void main(String[] args) {
Scanner input = null;
Scanner user = null;
PrintWriter output = null;
String inputFileName;
String outputFileName;
try {
// Create a Scanner for user input
user = new Scanner(System.in);
// Prepare the input file
System.out.print("Input File --> ");
inputFileName = user.nextLine().trim();
File file = new File(inputFileName);
input = new Scanner(file);
// Prepare the output file
System.out.print("Output File --> ");
outputFileName = user.nextLine().trim();
output = new PrintWriter(outputFileName);
} catch (IOException e) {
System.out.println("Problem opening file.");
System.out.println(e.getMessage());
if (input != null)
input.close();
if (output != null)
output.close();
if (user != null)
user.close();
return;
}
// processing loop
try {
// for each line of input
while (input.hasNextLine()) {
int line = Integer.parseInt(input.nextLine().trim());
// input.nextLine();
// output.println(line);
DataOutputStream dataOut = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(outputFileName)));
dataOut.writeInt(line);
System.out.println(line);
dataOut.close();
}
} catch (IOException e) {
System.out.println("File I/O Error");
System.err.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.print("Poor input data ");
System.err.println(e.getMessage() + ", this will not work");
// e.printStackTrace();
} finally {
// close the files
if (input != null)
input.close();
if (output != null)
output.close();
if (user != null)
user.close();
}
}
}
感谢您的帮助。
托尼
答案 0 :(得分:0)
读取输入文件时需要捕获该异常。只需将catch子句添加到try-catch块中。因此,您的应用不会因数字格式异常而中断。
try {
// Create a Scanner for user input
user = new Scanner(System.in);
// Prepare the input file
System.out.print("Input File --> ");
inputFileName = user.nextLine().trim();
File file = new File(inputFileName);
input = new Scanner(file);
// Prepare the output file
System.out.print("Output File --> ");
outputFileName = user.nextLine().trim();
output = new PrintWriter(outputFileName);
} catch (NumberFormatException ex) {
// ignore exception or whatever you want
} catch (IOException e) {
System.out.println("Problem opening file.");
System.out.println(e.getMessage());
if (input != null)
input.close();
if (output != null)
output.close();
if (user != null)
user.close();
return;
}
答案 1 :(得分:0)
此代码将为您打印所有数字,并跳过所有非数字值
Files.readAllLines(Paths.get("yourFileNameAndPath.txt")).stream()
.filter(s -> !s.isEmpty())
.filter(s -> s.matches("-?[0-9]+"))
.map(String::trim)
.collect(Collectors.toList()).forEach(System.out::println);
但是您应该为此使用Java 8 我对这些事情使用正则表达式,使用NumberFormatException(如果不需要)是一种不好的方法。
答案 2 :(得分:0)
考虑到如果遇到String
而不是Integer
,则此代码将起作用,循环将忽略它并继续处理下一行。只需在while
中捕获异常,然后执行continue
即可跳到下一行,如果有String
。
while (input.hasNextLine()) {
try {
int line = Integer.parseInt(input.nextLine().trim());
DataOutputStream dataOut = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(outputFileName)));
dataOut.writeInt(line);
System.out.println(line);
dataOut.close();
}catch(NumberFormatException ne){
continue;
}
}
答案 3 :(得分:0)
此代码读取文件,并仅写入数字。
public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
BufferedWriter outputStream = null;
try {
inputStream = new BufferedReader(new FileReader("entryFile.txt"));
outputStream = new BufferedWriter(new FileWriter("outputline.txt"));
String line;
while ((line = inputStream.readLine()) != null) {
boolean add = false;
for (int j = (line.length()-1); j >= 0; j--) {
if (Character.isDigit(line.charAt(j)) || (j==0 && Character.toString(line.charAt(j)).equals("-"))) {
add = true;
} else {
add = false;
break;
}
}
if(add){
outputStream.write(line + "\n");
} else {
System.out.println("this is not number: "+line);
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
entryFile.txt
2
1023
-56
84781
0
-9371
-2452-723878-
Rats
10
5
outputline.txt
2
1023
-56
84781
0
-9371
10
5
控制台输出:
run:
this is not number: -2452-723878-
this is not number: Rats
BUILD SUCCESSFUL (total time: 0 seconds)