Java 将文件内容作为输入读取到控制台
我试图读取一个文件,然后获取该文件的内容,并将其作为用户输入执行。我使用扫描仪读取文件和用户输入,但我不确定这是否是正确的方法,因为用于输入的扫描仪只能在System.in中运行,因此我不确定如何将数据从文件传递到输入扫描仪,以便在控制台中执行。下面是我阅读课的代码Java 将文件内容作为输入读取到控制台,java,file,input,java.util.scanner,Java,File,Input,Java.util.scanner,我试图读取一个文件,然后获取该文件的内容,并将其作为用户输入执行。我使用扫描仪读取文件和用户输入,但我不确定这是否是正确的方法,因为用于输入的扫描仪只能在System.in中运行,因此我不确定如何将数据从文件传递到输入扫描仪,以便在控制台中执行。下面是我阅读课的代码 public class readingFile { Scanner fileReading = new Scanner(new File("somecontent.txt")); Scanner input
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);
}
}
试试这个。我刚刚在eclipse中创建了这个程序,它运行良好。谢谢,谢谢你的帮助。我尝试了你的代码,但我只能打印文件的内容。我希望将文件中的内容作为用户inputOk传递到控制台,感谢您澄清需求。这个新程序将从文件系统读取java文件,并执行和显示输出。如果有任何问题,请尽量让我知道。谢谢。这是我用这段代码执行的java程序:公共类程序{publicstaticvoidmain(String[]args){System.out.println(“helloworld!!”);}}}}好的,谢谢你澄清了需求。这个程序将从文件系统中读取java文件并执行,然后显示输出。如果有任何问题,请尽量让我知道。谢谢。这是我执行的程序文件:公共类程序{publicstaticvoidmain(String[]args){System.out.println(“helloworld!!”);}
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());
}
}
}
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());
}
}
}