请帮助,我一生都无法弄清楚为什么创建我的输出文件,但没有写入任何内容。我读到一个类似的问题,我不得不关闭输出文件,因为信息被缓冲并且只有在文件关闭时才输出。在阅读那篇文章之前,我已经这样做了,除此之外,我找不到任何其他可以帮助我解决此问题的文章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); }
添加回答
举报
0/150
提交
取消