我将不得不在不久的将来使用Java编译器,所以我现在正在尝试学习基础知识并选择最好的一个来学习,我遇到了一个我无法在网上找到答案的问题
目前我正在研究 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 !!! ***");
}
}
}
你可以......
1. 你在谈论命令行参数吗,如果是的话那么它可能..
例如:
javac Test.java
enter code herejava Test Hello // Hello is the Input
在 Java 中,这个 “Hello”输入被发送到 String[] 数组中的 main() 方法。
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);
基础知识是:
<
字符的程序输入
>
字符的程序输出
javac Foo.java
java Foo <in.txt >out.txt
Foo 运行后,out.txt 将包含 Foo 打印到 System.out 的内容。查看 shell 文档以获取有关文本输入和输出的更多信息。