【问题标题】:Reading file contents as input into console将文件内容作为输入读取到控制台
【发布时间】:2021-03-05 05:42:28
【问题描述】:

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

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);
    }
}

【问题讨论】:

    标签: java file input java.util.scanner


    【解决方案1】:
    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());   
            }
        }
    }
    

    【讨论】:

    • 好的,感谢您澄清要求。该程序将从文件系统中读取 java 文件并执行,然后显示输出。如果有任何问题,请尝试让我知道。谢谢。
    • 这是我执行的程序文件: public class Program { public static void main(String[] args) { System.out.println("Hello World !!"); } }
    【解决方案2】:
    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());   
            }
        }
    }
    

    【讨论】:

    • 试试这个。我刚刚在 eclipse 中创建了这个程序,它工作正常。谢谢。
    • 感谢您的帮助。我试过你的代码,但我只能打印文件的内容。我希望将文件中的内容作为用户输入传递到控制台
    • 好的,感谢您澄清要求。这个新程序将从文件系统中读取 java 文件并执行并显示输出。如果有任何问题,请尝试让我知道。谢谢。
    • 这是我从这段代码执行的 java 程序: public class Program { public static void main(String[] args) { System.out.println("Hello World !!"); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多