简体   繁体   中英

How to input a file via Unix input redirection into my java programm as an array?

I'm a beginner in Java and need some help with this problem. First of all let me show you my actual program.

import java.util.Scanner;
import java.util.Arrays;

public class IntArraySort {


        public static void main(String[] args) {

                Scanner in = new Scanner(System.in);

                System.out.println("Array-Size: ");
                int num = in.nextInt();
                int[] arr = new int[num];

                for(int i = 0; i < arr.length; i++){
                        System.out.printf("%2d-tes element: ", i+1);
                        arr[i] = in.nextInt();
                }

                System.out.printf("%nOutput before sort: ");
                System.out.println(Arrays.toString(arr));

                int minIndex, tmp;
                int n = arr.length;

                for(int i = 00; i < n - 1; i++) {
                        minIndex = i;
                        for(int j = i + 1; j < n; j++)
                                if (arr[j] < arr[minIndex])
                                        minIndex = j;
                        if (minIndex != i) {
                                tmp = arr[i];
                                arr[i] = arr[minIndex];
                                arr[minIndex] = tmp;
                        }
                        System.out.printf("%n%2d-ter Run: ", i+1);
                        System.out.println(Arrays.toString(arr));
                }
         }

}

It's a program to sort an Int-array from smallest to biggest number while you scan the size of the array and the numbers via keyboard input. But I want to scan my array from a .dat-file. I named it sort.dat and it looks like this:

0
20
12
8
16
6
10
14
2
18
4
21
4
-1
-3

What do I need to change in my Java-program to scan in my sort.dat file via input redirection (IntArraySort.java < sort.dat) on Unix?

There are multiple ways. One way is to code the dat file read in your java program using java.io . Another is to create a shell (or bat, depending on your OS) script, which invokes your java program and cats the file contents into it as a program argument.

Java (DataFromFile.java):

public class DataFromFile {

    public static void main(String[] args){

        System.out.println("In DataFromFile");

        System.out.println("First arg is " + args[0]);
    }
}

data file (data.dat)

1
2
3

script:

#!/bin/bash

ARGS=`cat data.dat`

java DataFromFile $ARGS

prints

>DataFromFile.sh
In DataFromFile
First arg is 1

You do not need to change a lot of things just to read the input data which are of size args.length and store them in an array of integers.

For example:

ArrayList<Integer> inpudData = new ArrayList<Integer>();

if (args.length < 1) {
System.out.println("Proper Usage is: java IntArraySort < sort.dat");
System.exit(0);
} else {
    for (int i = 0; i < args.length; i++)
    inpudData.add(Integer.parseInt(args[i]));
}

Now you just have to call java IntArraySort < sort.dat where you redirect the content of the sort.dat file to the java program you are calling instead of having the user to type manually the number to be sorted one by one followed by an [ENTER].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM