2016-04-19 55 views
0

因此,在下面的代碼中,我試圖讓對象「reader」和「writer」被我的一些方法識別,所以我稍後將能夠在同時(我正在想使用線程)。所以這基本上是我的問題,是否有一種方法可以讓我的其他方法識別「讀者」和「作家」對象?跨方法使用變量/對象 - Java

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(); 
 
    } 
 
    } 
 
}

+0

讓它們成爲全局變量? – brso05

+0

xD我該怎麼做?不要把它們放在一個方法中? @ brso05 – Kaelinator

+0

@ brso05,*全局變量*是什麼意思? – Andrew

回答

0

雖然這不是一個推薦的面向對象的實踐中,做你想做的是設置readerwriterstatic領域,就像你Running場什麼樣的方式。

編輯:

以下代碼編譯並運行。我添加了兩個靜態字段,並在方法上添加了一個缺失的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(); 
    } 
    } 
} 
+0

這會引發錯誤:「刪除無效的修飾符」:/我不確定這是否僅僅是因爲它在方法中。 – Kaelinator

+0

@Kelinator沒有一個具體的例子,我不能告訴你你做錯了什麼。我已經爲我的答案添加了一個完整的示例。此代碼編譯並運行。 – GuiSim

+0

謝謝,這工作! @GuiSim – Kaelinator