在IntelliJ IDEA中如何配置命令行参数指定输入输出文件?
问题描述
我拥有input.txt和output.txt两个文件,程序需从input.txt读取矩阵,经处理后将新矩阵写入output.txt。当前程序在IntelliJ IDEA中可正常运行,但需通过控制台启动,因此需要配置命令行参数。请问应传入何种参数,语法格式如何正确书写?
当前代码存在的问题
当前程序的readMtx方法中硬编码了输入输出文件的固定路径,没有使用main方法的args参数,因此目前无法通过命令行参数指定自定义的文件路径,需要先修改代码以支持命令行参数传递。
修改后的程序代码
Main类
package vsu.cs.vega; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { // 检查命令行参数数量是否正确 if (args.length != 2) { System.err.println("参数错误,请传入输入文件路径和输出文件路径,格式:java Main <输入文件路径> <输出文件路径>"); return; } Solution output = new Solution(); String error = "您输入的是非矩形矩阵,请检查数据后重新输入。"; try { // 将命令行参数传入readMtx方法 output.readMtx(args[0], args[1]); } catch (ArrayIndexOutOfBoundsException exception) { System.err.print(error); } } }
Solution类
package vsu.cs.vega; import java.io.*; import java.util.ArrayList; import java.util.Arrays; public class Solution { // 修改readMtx方法,接收输入和输出文件路径参数 void readMtx(String inputFilePath, String outputFilePath) throws IOException { BufferedReader br = new BufferedReader(new FileReader(inputFilePath)); ArrayList<String> lines = new ArrayList<>(); while (br.ready()) { lines.add(br.readLine()); } int matrixHeight = lines.size(); int[][] matrix = new int[matrixHeight][]; for(int i = 0; i < matrixHeight; ++i) { String[] nums = lines.get(i).split("\\s*,\\s*"); matrix[i] = new int[nums.length]; for(int j = 0; j < nums.length; ++j) { matrix[i][j] = Integer.parseInt(nums[j]); } } int[][] matrixForOutput = calc(matrix); try(PrintWriter out = new PrintWriter(new FileOutputStream(outputFilePath))) { // 修复循环条件:使用结果矩阵的行数而非原矩阵行数 for (int i = 0; i < matrixForOutput.length; ++i) { out.println(Arrays.toString(matrixForOutput[i]).replaceAll("^\\[|]$", "")); } } catch (ArrayIndexOutOfBoundsException ignored) { } } int[][] calc(int[][] array) { int rows = array.length; int cols = array[0].length; boolean[] minRows = null, maxRows = null; boolean[] minCols = null, maxCols = null; Integer min = null; Integer max = null; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (null == min || array[i][j] < min) { minRows = new boolean[rows]; minRows[i] = true; minCols = new boolean[cols]; minCols[j] = true; min = array[i][j]; } else if (array[i][j] == min) { minRows[i] = true; minCols[j] = true; } if (null == max || array[i][j] > max) { maxRows = new boolean[rows]; maxRows[i] = true; maxCols = new boolean[cols]; maxCols[j] = true; max = array[i][j]; } else if (array[i][j] == max) { maxRows[i] = true; maxCols[j] = true; } } } int rowsToDelete = 0, colsToDelete = 0; for (int i = 0; i < rows; i++) { if (minRows[i] || maxRows[i]) { rowsToDelete++; } } for (int i = 0; i < cols; i++) { if (minCols[i] || maxCols[i]) { colsToDelete++; } } if (rows == rowsToDelete || cols == colsToDelete) { return new int[1][0]; } int[][] result = new int[rows - rowsToDelete][cols - colsToDelete]; for (int i = 0, r = 0; i < rows; i++) { if (minRows[i] || maxRows[i]) continue; // 跳过包含最小值或最大值的行 for (int j = 0, c = 0; j < cols; j++) { if (minCols[j] || maxCols[j]) continue; // 跳过包含最小值或最大值的列 result[r][c++] = array[i][j]; } r++; } return result; } }
命令行参数语法说明
修改代码后,启动程序时需要传入两个参数:输入文件的完整路径和输出文件的完整路径,语法格式如下:
java vsu.cs.vega.Main /path/to/input.txt /path/to/output.txt
- 如果文件和编译后的class文件在同一目录下,可直接使用文件名,比如:
java vsu.cs.vega.Main input.txt output.txt - 如果文件在其他目录,需填写绝对路径或相对路径(相对于控制台当前工作目录)
IntelliJ IDEA中配置命令行参数
在IDEA的运行配置界面中,找到Program arguments输入框,填写参数内容,格式为:
input.txt output.txt
或对应原代码中的路径:
readInFileOutFromFile/src/vsu/cs/vega/input.txt readInFileOutFromFile/src/vsu/cs/vega/output.txt
(参数之间用空格分隔,无需添加引号,除非路径包含空格)
补充说明
同时附上IntelliJ IDEA中配置命令行参数的界面截图,需在对应位置填写参数。
内容的提问来源于stack exchange,提问作者vegaLDN
相关产品推荐
相关产品推荐

