请帮助,我一生无法弄清为什么创建输出文件,但是没有写入任何内容。我读过一个类似的问题,我不得不关闭输出文件,因为信息被缓冲,并且仅在文件关闭时才输出。在阅读该帖子之前,我已经完成了此操作,除了找不到其他可以帮助我解决该问题的帖子
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Arrays;
public class MyFile {
public static void main(String[] args) {
try {
// creating a File instance to reference text file in Java
File inputFile = new File("C:\\input.txt");
//creating a Scanner instance to read File in Java
Scanner scanfile = new Scanner(inputFile);
//creating a file to output results of program to
Formatter outputfile = new Formatter("C:\\output.txt");
//read each line of file using the Scanner instance "scanfile"
while (scanfile.hasNextLine()) {
String line = scanfile.nextLine();
//splitting String "line" into 2 parts at the ":"
String[] parts = line.split(":");
String part1 = parts[0];
String part2 = parts[1];
//splitting part2 into an array of integers at ","
String[] lineNumString = part2.split(",");
int[] lineNums = new int[lineNumString.length];
for (int i = 0; i < lineNumString.length; i++ ) {
lineNums[i] = Integer.parseInt(lineNumString[i]);
}
/*now that we have the line split into an operator (min, max or avg) and an array of numbers
we can identify the operator with an if and else if statements, then manipulate the integer array according
to the operator*/
//Outputting max value if operator is max
if (part1 == "max") {
String outputText = "The Max of " + Arrays.toString(lineNums) + " is " + maxOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
//Outputting min value if operator is min
else if (part1 == "min") {
String outputText = "The Min of " + Arrays.toString(lineNums) + " is " + minOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
//Outputting avg value if operator is avg
else if (part1 == "avg") {
String outputText = "The Min of " + Arrays.toString(lineNums) + " is " + avgOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
}
scanfile.close();
outputfile.close();
}
// print out error messages for input file not found or for being unable to create or write to an output file.
catch (FileNotFoundException e) {
System.out.println("Error.");
}
}
// method for determining the max value in an array of integers
public static int maxOfArray(int[] inputArray) {
int maxValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
}
}
return maxValue;
}
// method for determining the min value in an array of integers
public static int minOfArray(int[] inputArray) {
int minValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
}
}
return minValue;
}
//method for determining avg value of an array of integers
public static int avgOfArray(int[] inputArray) {
int sum = 0;
for (int i : inputArray) {
sum += i;
}
int average = sum / inputArray.length;
return average;
}
}
答案 0 :(得分:0)
使用equals
或equalsIgnoreCase
而非==
if (part1.equals("max")) {
答案 1 :(得分:0)
看起来您正在正确打开/关闭文件。但是,您正在执行直接的String比较,这意味着您的所有布尔值每次都将返回false,从而导致文件为空。
在Java中比较字符串时,请使用.equals()
。例如:
if (part1.equals("max")) {do a thing}