请求文件名作为输入--Java

时间:2011-10-05 07:57:15

标签: java file file-io arguments

我的代码:

import java.util.*;
import java.io.*;

public class Percolation {
    // given an N-by-N matrix of open sites, return an N-by-N matrix
    // of sites reachable from the top via a vertical path of open sites
    private static Scanner scan = null;

    public static boolean[][] readOpenFromFile() {
        final File f = new File("file.txt");
        try {
            scan = new Scanner(f);
        }
        catch(FileNotFoundException ex) {
            System.exit(0);
        }

        final int m = scan.nextInt();
        final int n = scan.nextInt();
        final boolean[][] grid = new boolean[m][n];

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                grid[i][j] = readBoolean();
            }
        }
        return grid;
    }

    public static boolean readBoolean() {
        final String s = scan.next();

        if(s.equalsIgnoreCase("true")) {
            return true;
        }
        if(s.equalsIgnoreCase("false")) {
            return false;
        }
        if(s.equals("1")) {
            return true;
        }
        if(s.equals("0")) {
            return false;
        }
        throw new java.util.InputMismatchException();
    }

    public static boolean[][] flow(final boolean[][] open) {
        final int n = open.length;
        final boolean[][] full = new boolean[n][n];
        for(int j = 0; j < n; j++) {
            flow(open, full, 0, j);
        }
        return full;
    }

    public static void flow(final boolean[][] open, final boolean[][] full, final int i, final int j) {
        final int n = open.length;

        // base cases
        if(( i < 0) ||( i >= n)) {
            return; // invalid row
        }
        if(( j < 0) ||( j >= n)) {
            return; // invalid column
        }
        if(!open[i][j]) {
            return; // not an open site
        }
        if(full[i][j]) {
            return; // already marked as full
        }

        // mark i-j as full
        full[i][j] = true;

        flow(open, full, i + 1, j); // down
        flow(open, full, i, j + 1); // right
        flow(open, full, i, j - 1); // left
        flow(open, full, i - 1, j); // up
    }

    // does the system percolate?
    public static boolean percolates(final boolean[][] open) {
        final int n = open.length;
        final boolean[][] full = flow(open);
        for(int j = 0; j < n; j++) {
            if(full[n - 1][j]) {
                System.out.println("percolates");
                return true;
            }
        }
        System.out.println("does not percolate");
        return false;
    }

    public static void print(final boolean[][] grid) {
        final int m = grid.length;
        final int n = grid[0].length;
        System.out.println(m + " " + n);
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j]) {
                    System.out.print("1 ");
                } else {
                    System.out.print("0 ");
                }
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) {
        final boolean[][] open = readOpenFromFile();
        print(flow(open));
        System.out.println(percolates(open));
    }
}

可以看出,该程序通过从file.txt文件中获取输入来工作。但是,如何在每次运行程序时修改此代码以便请求 要求文件名(可能在命令行中),并使用 作为输入? 我想将String添加为参数,然后将该String更改为文件名。但这说起来容易做起来难。建议?

3 个答案:

答案 0 :(得分:0)

您可以使用此代码:

public static boolean[][] readOpenFromFile(String file) {
    final File f = new File( file );
    // same code ...
}


public static void main(String[] args) {
    if(args != null && args.length == 1) {
        String file = args[0];    

        final boolean[][] open = readOpenFromFile(file);
        print(flow(open));
        System.out.println(percolates(open));
    }
}

答案 1 :(得分:0)

您可以将其作为参数并将代码修改为 -

public static void main(String[] args) {
    // args[0] - Full path of the file
    final boolean[][] open = readOpenFromFile(args[0]);
    print(flow(open));
    System.out.println(percolates(open));
}

public static boolean[][] readOpenFromFile(String filepath) {
    final File f = new File(filepath);
    try {
        scan = new Scanner(f);
    }
    catch(FileNotFoundException ex) {
        System.exit(0);
    }

    final int m = scan.nextInt();
    final int n = scan.nextInt();
    final boolean[][] grid = new boolean[m][n];

    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            grid[i][j] = readBoolean();
        }
    }
    return grid;
}

答案 2 :(得分:0)

你需要这样的东西,但你必须添加检查文件名:

InputStreamReader converter = new InputStreamReader(System.in);
      BufferedReader in = new BufferedReader(converter);


      while (!(CurLine.equals("quit"))){
          CurLine = in.readLine();

          if (!(CurLine.equals("quit"))){
              System.out.println("You typed: " + CurLine);
          }
      }

Link to site where you can find it