2015-05-26 4 views
5

मैं किसी अन्य कक्षा में एक कक्षा के main() विधि का उपयोग कैसे करूं? मेरे पास दो वर्ग SaveData और DynamicTest हैं। दोनों वर्गों में main() विधि है। मैं अपना मुख्य भाग DynamicTest कक्षा में चलाने के लिए चाहता हूं। मैं SaveData को ठीक से कैसे कॉल करूं?मैं किसी अन्य वर्ग में एक वर्ग की मुख्य() विधि कैसे शुरू करूं?

public class SaveData { 

    private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>(); 


    public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{ 
     System.out.println("Please get: "+resourceParam); 
     if (myCachedTreeMap.containsKey(resourceParam)) { 
      // Use the cached file, to prevent an additional read. 
      System.out.println("Found in memory : "+resourceParam); 

     } 
     else { 
      // Load the file from disk 
      System.out.println("Not Found in memory : "+resourceParam); 
     } 

     return null; 


    } 


    public static void main(String[] args) throws IOException { 
     String target_dir = "C:\\myfiles\\config\\en"; 
     String output = "C:\\myfiles\\config\\en\\output.txt"; 
     File dir = new File(target_dir); 
     File[] files = dir.listFiles(); 

     if (files == null || files.length < 1) { 
      System.out.println("File list is empty..."); 
      return; 
     } 

     // open the Printwriter 
     PrintWriter outputStream = new PrintWriter(output); 

     try { 

      for (File textFile : files) { 
       if (textFile.isFile() && textFile.getName().endsWith(".txt")) { 
        readFromDisk(textFile);     
       } 
      } 
     } 
     finally { 
      outputStream.close(); 
     } 
     String fileNameFromCache = "en_synonyms.txt"; 
     Object Sheet1 = myCachedTreeMap.get(fileNameFromCache); 
     System.out.println(fileNameFromCache + " : \n" + Sheet1); 
    } 

    @SuppressWarnings("resource") 
    private static void readFromDisk(File textFile) throws FileNotFoundException, IOException { 

     BufferedReader inputStream; 
     inputStream = null; 
     String content = ""; 
     try { 
      inputStream = new BufferedReader(new FileReader(textFile)); 
      content = readFile(textFile); 
      System.out.println("Bytes Read = "+content.length()); 
      // Save contents 
      FileContentsObject Sheet1 = new FileContentsObject(System.currentTimeMillis(), 
        textFile.lastModified(), content, 
        textFile.getName(), 
        getLines(null, null)); 
      // add to map 
      myCachedTreeMap.put(textFile.getName(), Sheet1); 
     } 
     finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
     } 
    } 


    private static String readFile(File f) throws FileNotFoundException, IOException, UnsupportedEncodingException { 
     StringBuilder text = new StringBuilder(1024); 
     int read, N = 1024 * 1024; 
     char[] buffer = new char[N]; 


      BufferedReader br = null; 
      try { 
       br = new BufferedReader(
           new InputStreamReader(
           new FileInputStream(f), "UTF8")); 


       while(true) { 
        read = br.read(buffer, 0, N); 
        if (read > 0) 
         text.append(new String(buffer, 0, read)); 

        if(read < N) { 
         break; 
        } 
       } 
      } 
      finally { 
       if (br != null) 
        br.close(); 
      } 

     return text.toString(); 
    } 


    private static final class FileContentsObject { 
     private long cachedTime; // currentTime 
     private long lastModifiedTimestamp; 
     private String contents; 
     List<String> lines; 
     private String fileName; 

     public FileContentsObject(long cachedTime, long lastModifiedTimestamp, 
       String contents, String fileName, List<String> lines) { 
      this.cachedTime = cachedTime; 
      this.lastModifiedTimestamp = lastModifiedTimestamp; 
      this.contents = contents; 
      this.fileName = fileName; 
      this.lines = lines; 
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss"); 


      System.out.println("Current Time & Date:" + sdf.format(cachedTime)); 

      System.out.println("Last Modified Time Stamp:" 
        + sdf.format(lastModifiedTimestamp)); 

     } 
     /** 
     * 
     * @return The lines from the file 
     */ 
     List<String> getLines() { 
      return this.lines; 
     } 
     public String toString() { 
      return "Sheet1{" + "fileName='" + fileName + '\'' + ", contents='" 
        + contents + '\'' + ", lastModifiedTimestamp=" 
        + lastModifiedTimestamp + ", CurrentTime&Date=" 
        + cachedTime + '}'; 



     } 
    } 
} 

public class DynamicTest { 

    public static void main(String[] args) { 
     Charset charset = Charset.forName("UTF-8"); 



     try { 
      List<String> lines = CacheData.getLines("en_synonyms", charset) ; 
      if (lines != null) { 
       System.out.println("Number of Lines: "+lines.size()); 
       for (String line:lines) { 
        System.out.println("DynamicTest:: "+line); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 



     } 



     try { 
      List<String> lines = CacheData.getLines("en_stopwords", charset) ; 
      if (lines != null) { 
       System.out.println("Number of Lines: "+lines.size()); 
       for (String line:lines) { 
        System.out.println("DynamicTest:: "+line); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 



     } 


    } 

} 
+0

तुम नहीं जानते, तो स्थैतिक तरीकों को कैसे कॉल करें, आपको एक ट्यूटोरियल पढ़ना चाहिए। – fabian

+0

आप यह कर सकते हैं, लेकिन सिफारिश नहीं की जाती है। यदि आप केवल डायनामिकटेस्ट से सेवडाटा की 'मुख्य' विधि को कॉल करेंगे, तो मुख्य को किसी अन्य विधि में पुनर्नामित करें। मैं यह भी देखता हूं कि आपने तर्कों का उपयोग नहीं किया है और इसलिए आपके पास बिना किसी तर्क के एक विधि होनी चाहिए। –

उत्तर

3

आप इसे किसी अन्य स्थिर विधि कॉल की तरह फोन:

SaveData.main (args); // it's up to you whether to pass the same args or 
         // different args 
+0

यह, लेकिन मैं सुझाव देता हूं कि एक बड़ा '' '' '' 'विधि का उपयोग न करें। बस एक परिचित विधि को परिभाषित करें और इसे कॉल करें। इसके अलावा, आईएमओ कई प्रमुख घोषित नहीं करता है – Manu

+0

@manutero क्या आप "बड़े मुख्य विधि का उपयोग न करके" का अर्थ बता सकते हैं? – FullNelson

+0

@FullNelson * बड़े * के लिए * बड़ा * (क्षमा करें) बदलें; आपके मुख्य में कार्यक्रम तर्क से संबंधित ~ 25 लाइनें हैं, बस ** अच्छी कोडिंग अभ्यास **; [इस उत्तर से] (http://stackoverflow.com/a/7911881/1614677) इस पंक्ति का उद्धरण दें: * मुख्य() वास्तव में हमारे दैनिक वर्गों के डेटा और व्यवहार से संबंधित नहीं है, क्योंकि मुझे संदेह है कि प्रत्येक वर्ग की ज़रूरत है अपने आप पर निष्पादन योग्य होने के लिए। मुख्य() की चिंता हमारे कार्यक्रम को चलाने के साथ है। * इसके अलावा, [इस सवाल को देखें] (http://stackoverflow.com/questions/732151/java-main-method-good-coding-style) – Manu

1

DynamicTest लिखने इस SaveData.main(args);

स्टेटिक विधि कॉल की मुख्य विधि में:

Class.method(); 

गैर स्थिर विधि कॉल:

Class object = new Class(); 
object.method(); 
0

Main declaration हमेशा static ताकि आप, अन्य static तरीकों जैसे कि यह कॉल करना होगा अपने मामले में:

SaveData.main (args); 
+0

कोई विपक्ष है दो मुख्य तरीकों से संबंधित? कैशडाटा से मुख्य को हटाने के संबंध में मेरे विकल्प क्या हैं? – FullNelson

संबंधित मुद्दे