[英]Using variables/objects across methods - Java
So, in the following code I am trying to let the objects "reader" and "writer" be recognized by some of my methods, so I later will be able to run them at the same time (I'm thinking like using threads). 因此,在下面的代码中,我试图让对象“ reader”和“ writer”被我的某些方法识别,以便以后可以同时运行它们(我在想像使用线程) 。 So that's basically my question, is there a way I can get the "reader" and "writer" objects to be recognized in my other methods?
所以这基本上是我的问题,是否可以通过其他方法来识别“读取器”和“写入器”对象?
package com.Kaelinator; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class ServerManager2 { static boolean Running = false; //static String command = "C:/Users/Kael/Desktop/minecraftserver/RUN.bat"; static String command = "C:/Users/Owner/Desktop/rekt/Run.bat"; static String shutDown = "C:/Windows/System32/shutdown.exe -r -t 0"; public static void main(String[] args) throws IOException { runServer(); while (Running) { chatChecker(); } Commands(); } public static void chatChecker() { LocalDateTime now = LocalDateTime.now(); int hour = now.get(ChronoField.HOUR_OF_DAY); int minute = now.get(ChronoField.MINUTE_OF_HOUR); int second = now.get(ChronoField.SECOND_OF_MINUTE); String hourSyntax = Integer.toString(hour); String minuteSyntax = Integer.toString(minute); String secondSyntax = Integer.toString(second); String chatChecker = "[" + hourSyntax + ":" + minuteSyntax + ":" + secondSyntax + "] [Server thread/INFO]: "; System.out.println(chatChecker); } public static void sleepThread(long time) { try { Thread.sleep(time * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void runServer() throws IOException { Process process = Runtime.getRuntime().exec(command); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); } public static void readOutput() { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } public static void Commands() throws IOException { Running = true; try { sleepThread(3); writer.append("say SERVER STARTED"); writer.newLine(); writer.flush(); sleepThread(14100); writer.append("say restarting in 5 minutes"); writer.newLine(); writer.flush(); sleepThread(60); writer.append("say restarting in 4 minutes"); writer.newLine(); writer.flush(); sleepThread(60); writer.append("say restarting in 3 minutes"); writer.newLine(); writer.flush(); sleepThread(60); writer.append("say restarting in 2 minutes"); writer.newLine(); writer.flush(); sleepThread(60); writer.append("say restarting in 1 minutes"); writer.newLine(); writer.flush(); sleepThread(30); writer.append("say restarting in 30 seconds"); writer.newLine(); writer.flush(); sleepThread(20); writer.append("say restarting in 10 seconds"); writer.newLine(); writer.flush(); sleepThread(5); writer.append("say restarting in 5 seconds"); writer.newLine(); writer.flush(); sleepThread(1); writer.append("say restarting in 4 seconds"); writer.newLine(); writer.flush(); sleepThread(1); writer.append("say restarting in 3 seconds"); writer.newLine(); writer.flush(); sleepThread(1); writer.append("say restarting in 2 seconds"); writer.newLine(); writer.flush(); sleepThread(1); writer.append("say restarting in 1 second"); writer.newLine(); writer.flush(); writer.append("stop"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
While this is not a recommended Object Oriented practice, one way to do what you want would be to set reader
and writer
as static
fields, just like your Running
field. 尽管这不是推荐的面向对象的做法,但是一种实现所需方法的方法是将
reader
和writer
设置为static
字段,就像Running
字段一样。
EDIT: 编辑:
The following code compiles and runs. 以下代码编译并运行。 I added two static fields and added a missing
throws
on a method. 我添加了两个静态字段,并在方法上添加了缺失的
throws
。
package com.Kaelinator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class ServerManager2 {
static BufferedWriter writer;
static BufferedReader reader;
static boolean Running = false;
//static String command = "C:/Users/Kael/Desktop/minecraftserver/RUN.bat";
static String command = "C:/Users/Owner/Desktop/rekt/Run.bat";
static String shutDown = "C:/Windows/System32/shutdown.exe -r -t 0";
public static void main(String[] args) throws IOException {
runServer();
while (Running) {
chatChecker();
}
Commands();
}
public static void chatChecker() {
LocalDateTime now = LocalDateTime.now();
int hour = now.get(ChronoField.HOUR_OF_DAY);
int minute = now.get(ChronoField.MINUTE_OF_HOUR);
int second = now.get(ChronoField.SECOND_OF_MINUTE);
String hourSyntax = Integer.toString(hour);
String minuteSyntax = Integer.toString(minute);
String secondSyntax = Integer.toString(second);
String chatChecker = "[" + hourSyntax + ":" + minuteSyntax + ":" + secondSyntax +
"] [Server thread/INFO]: ";
System.out.println(chatChecker);
}
public static void sleepThread(long time) {
try {
Thread.sleep(time * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void runServer() throws IOException {
Process process = Runtime.getRuntime().exec(command);
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
process.getInputStream();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
}
public static void readOutput() throws IOException {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
public static void Commands() throws IOException {
Running = true;
try {
sleepThread(3);
writer.append("say SERVER STARTED");
writer.newLine();
writer.flush();
sleepThread(14100);
writer.append("say restarting in 5 minutes");
writer.newLine();
writer.flush();
sleepThread(60);
writer.append("say restarting in 4 minutes");
writer.newLine();
writer.flush();
sleepThread(60);
writer.append("say restarting in 3 minutes");
writer.newLine();
writer.flush();
sleepThread(60);
writer.append("say restarting in 2 minutes");
writer.newLine();
writer.flush();
sleepThread(60);
writer.append("say restarting in 1 minutes");
writer.newLine();
writer.flush();
sleepThread(30);
writer.append("say restarting in 30 seconds");
writer.newLine();
writer.flush();
sleepThread(20);
writer.append("say restarting in 10 seconds");
writer.newLine();
writer.flush();
sleepThread(5);
writer.append("say restarting in 5 seconds");
writer.newLine();
writer.flush();
sleepThread(1);
writer.append("say restarting in 4 seconds");
writer.newLine();
writer.flush();
sleepThread(1);
writer.append("say restarting in 3 seconds");
writer.newLine();
writer.flush();
sleepThread(1);
writer.append("say restarting in 2 seconds");
writer.newLine();
writer.flush();
sleepThread(1);
writer.append("say restarting in 1 second");
writer.newLine();
writer.flush();
writer.append("stop");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.