我在同一个包中有一组 Java 文件,每个文件都有主要方法。我现在希望从另一个类逐步调用每个类的主要方法。 Splitter.java 就是这样的一个类文件。这是它的代码。
public static void main(String[] args) throws IOException {
InputStream modelIn = new FileInputStream("C:\\Program Files\\Java\\jre7\\bin\\en-sent.bin");
FileInputStream fin = new FileInputStream("C:\\Users\\dell\\Desktop\\input.txt");
DataInputStream in = new DataInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = br.readLine();
System.out.println(strLine);
try {
SentenceModel model = new SentenceModel(modelIn);
SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
String sentences[] = sentenceDetector.sentDetect(strLine);
System.out.println(sentences.length);
for (int i = 0; i < sentences.length; i++) {
System.out.println(sentences[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (modelIn != null) {
try {
modelIn.close();
} catch (IOException e) {
}
}
fin.close();
}
}
我现在希望在主方法内的 AllMethods.java
中调用它。
那么我该怎么做呢?还有其他几个类文件具有带有 IOException
的主方法,必须在 AllMethods.java
文件中调用这些方法。
更新 -
我有具有 IOException
的主要方法,以及没有 IOEXception
的主要方法,这些方法必须在 AllMethods.java
中调用。
最佳答案
你可以做到。 Main 方法也与任何其他静态方法一样
public static void main(String[] args) throws IOException {
....// do all the stuff
Splitter.main(args); // or null if no args you need
}
关于Java:从另一个类的主方法调用主方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33055575/