我将不得不在不久的将来使用JavaCompilers,所以我现在正在努力学习基础知识并选择最好的学习方法,并且我遇到了一个问题,我无法在网上找到答案
目前我正在查看JavaCompiler。如果我正在评估程序X并从System.in获取输入并将输出打印到System.out, 我可以编译并运行它,同时输入一些特定的输入并在文件中捕获它的输出吗?
如果是这样,网上是否有任何示例,我可以尝试更好地理解它?
编辑:
编译时传入的简单测试用例
import java.util.Iterator;
public class TestSet
{
public static void main(String[] args)
{
try
{
System.out.print( "[1]--constructor 1, size, isEmpty: ");
LinkedSet<String> s1 = new LinkedSet<String>();
if (s1.size() != 0)
System.out.print("*** size? ***");
if (!s1.isEmpty())
System.out.print("*** isEmpty? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[2]--constructor 2, size, isEmpty: ");
LinkedSet<String> s1 = new LinkedSet<String>( "alpha");
if (s1.size() != 1)
System.out.print("*** size? ***");
if (s1.isEmpty())
System.out.print("*** isEmpty? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[3]--contains: ");
LinkedSet<String> s1 = new LinkedSet<String>("alpha");
if (!s1.contains("alpha"))
System.out.print("*** elt. not found ***");
if (s1.contains("beta"))
System.out.print("*** non-elt ``found'' ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[4]--add: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
if (s1.size() != 3)
System.out.print("*** size? ***");
if (!s1.contains("alpha") ||
!s1.contains("beta") ||
!s1.contains("gamma"))
System.out.print("*** elt. not found ***");
if (s1.contains("delta"))
System.out.print("*** non-elt ``found'' ***");
s1.add("beta");
if (s1.size() != 3)
System.out.print("*** size/duplicates? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[5]--remove: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
s1.remove("beta");
s1.remove("alpha");
s1.remove("gamma");
if (s1.size() != 0)
System.out.print("*** size? ***");
if (s1.contains("alpha") ||
s1.contains("beta") ||
s1.contains("gamma"))
System.out.print("*** non elt. ``found'' ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[6]--print: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("this");
s1.add("is");
s1.add("assignment");
s1.add("four");
s1.print();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[7]--addAll: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
LinkedSet<String> s2 = new LinkedSet<String>();
s2.add("beta");
s2.add("delta");
s1.addAll(s2);
if (!s1.contains("alpha") || ! s1.contains("beta") ||
!s1.contains("gamma") || !s1.contains("delta"))
System.out.print("*** elt. ``dropped'' ***");
if (s1.size() != 4)
System.out.print("*** size ? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[8]--containsAll: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
LinkedSet<String> s2 = new LinkedSet<String>();
s2.add("beta");
s2.add("delta");
if (s1.containsAll(s2))
System.out.print("*** non-containment incorrect ***");
LinkedSet<String> s3 = new LinkedSet<String>();
s3.add("beta");
s3.add("alpha");
if (!s1.containsAll(s3))
System.out.print("*** containment incorrect ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[9]--removeAll: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
s1.add("delta");
LinkedSet<String> s2 = new LinkedSet<String>();
s2.add("beta");
s2.add("delta");
s1.removeAll(s2);
if (s1.contains("beta") || s1.contains("delta"))
System.out.print("*** some elts not removed? ***");
if (!s1.contains("alpha") || !s1.contains("gamma"))
System.out.print("*** wrong elts removed? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[10]--retainAll: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
s1.add("delta");
LinkedSet<String> s2 = new LinkedSet<String>();
s2.add("beta");
s2.add("delta");
s1.retainAll(s2);
if (s1.contains("alpha") || s1.contains("gamma"))
System.out.print("*** wrong elts retained? ***");
if (!s1.contains("beta") || !s1.contains("delta"))
System.out.print("*** wrong elts removed? ***");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
try
{
System.out.print( "[11]--elements: ");
LinkedSet<String> s1 = new LinkedSet<String>();
s1.add("alpha");
s1.add("beta");
s1.add("gamma");
s1.add("delta");
Iterator<String> list = s1.elements();
System.out.print("("+list.hasNext()+") ");
if (list.hasNext())
System.out.print(list.next()+"; ");
System.out.print("("+list.hasNext()+")");
if (list.hasNext())
System.out.print(list.next()+"; ");
System.out.print("("+list.hasNext()+")");
if (list.hasNext())
System.out.print(list.next()+"; ");
System.out.print("("+list.hasNext()+")");
if (list.hasNext())
System.out.print(list.next()+"; ");
System.out.print("("+list.hasNext()+")");
if (list.hasNext())
System.out.print(list.next()+"; ");
System.out.println();
}
catch(Exception e)
{
System.out.println("*** CRASH !!! ***");
}
}
}
答案 0 :(得分:1)
这不是一个编译器问题。我很确定你想要的只是输入/输出重定向,可能只需要一些脚本。
基础知识是:
<
字符>
字符例如,如果我在Foo.java中有一个程序,并且我想从in.txt给它输入,将其结果存储在out.txt中,我可能会输入:
javac Foo.java
java Foo <in.txt >out.txt
在Foo运行之后,out.txt会将Foo打印到System.out。有关文本输入和输出的更多信息,请查看shell文档。
答案 1 :(得分:1)
雅你可以......
1。您是在谈论命令行参数,如果是,那么它可能就是......
<强>例如强>
javac Test.java
enter code herejava Test Hello // Hello is the Input
在Java中,“Hello”输入被发送到其中的main()方法String [] array 。
public static void main(String[] args)
因此,要访问作为Hello
方法参数的参数传递的main()
,您需要执行此操作。
String s = args[0];
2. 现在,如果你想在程序内部将其写入文件,请执行此操作。
String s = args[0];
File f = new File(d:\\vivek.txt);
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(s);