java—将文件内容作为输入读入控制台

30byixjq  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(321)

我试图读取一个文件,然后获取该文件的内容,并将其作为用户输入执行。我使用scanner读取文件和用户输入,但我不确定这是否是正确的方法,因为scanner for input只能在system.in中执行,因此我不确定如何将数据从文件传递到输入scanner,以便在控制台中执行。下面是我的阅读课代码

public class readingFile {

Scanner fileReading = new Scanner(new File("somecontent.txt"));
Scanner input = new Scanner(System.in);

public readingFile() throws FileNotFoundException {
}

public void startReading()
{
    System.out.println("reading file...");
    while(fileReading.hasNextLine()){
        String data = fileReading.nextLine();
        System.out.println(data);
        Scanner input = new Scanner(System.in);
    }
}
uurity8g

uurity8g1#

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class readingFile  {
    static String javaFileFullPath = "D://myfolder/Program.java";

    public static void main(String[] args) {
        executeJavaFile();
    }

    public static void executeJavaFile() {
        try {
            System.out.println("executing java program from file....");

            Process compileProcess = Runtime.getRuntime().exec("cmd /c  javac "+javaFileFullPath);
            Thread.sleep(5000);

            System.out.println(compileProcess.exitValue());
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
            String line = "";
            while ((line = inputReader.readLine()) != null) {
                System.out.println(line);
            }
            inputReader.close();

            Process runProcess = Runtime.getRuntime().exec("cmd /c  java "+javaFileFullPath);
            Thread.sleep(5000);
            System.out.println(runProcess.exitValue());
            BufferedReader inReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
            String lineStr = "";
            while ((lineStr = inReader.readLine()) != null) {
                System.out.println(lineStr);
            }
            inReader.close();

        } catch (Exception ex) {
            System.out.println("Exception:"+ex.getMessage());   
        }
    }
}
wdebmtf2

wdebmtf22#

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class readingFile  {
    static String javaFileFullPath = "D://myfolder/Program.java";

    public static void main(String[] args) {
        executeJavaFile();
    }

    public static void executeJavaFile() {
        try {
            System.out.println("executing java program from file....");

            Process compileProcess = Runtime.getRuntime().exec("cmd /c  javac "+javaFileFullPath);
            Thread.sleep(5000);

            System.out.println(compileProcess.exitValue());
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
            String line = "";
            while ((line = inputReader.readLine()) != null) {
                System.out.println(line);
            }
            inputReader.close();

            Process runProcess = Runtime.getRuntime().exec("cmd /c  java "+javaFileFullPath);
            Thread.sleep(5000);
            System.out.println(runProcess.exitValue());
            BufferedReader inReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
            String lineStr = "";
            while ((lineStr = inReader.readLine()) != null) {
                System.out.println(lineStr);
            }
            inReader.close();

        } catch (Exception ex) {
            System.out.println("Exception:"+ex.getMessage());   
        }
    }
}

相关问题