2014-02-25 19 views
0

你好我有一個工作程序,讀取一個txt文件(名稱,ID,電子郵件,密碼),並只寫入名稱和電子郵件到.html擴展名輸出文件...如何在Java中將1個文件程序分解成多個類?

我的麻煩是我有程序工作都在1級以下..但我的要求是我需要多個班級.. 1爲處理,1爲閱讀,1爲寫作。你會如何推薦我分手我的文件?我有點困惑和任何指導表示讚賞謝謝

import java.io.*; 

public class test { 

public static void main(String[] args) throws Exception{ 
    // define the path to your text file 

    System.out.println("Enter your file name \n"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    String myFilePath = in.readLine(); 
    String file1 = (myFilePath + ".txt");  
    System.setOut(new PrintStream(new FileOutputStream(myFilePath + ".html"))); 
    // read and parse the file 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(new File(file1))); 
     String line, name, email; 
     // read through the first two lines to get to the data 
     line = br.readLine(); 
     line = br.readLine(); 
     while ((line = br.readLine()) != null) { 
      if (line.contains("|")) { 
       // do line by line parsing here 
       line = line.trim(); 
       // split the line 
       String[] parts = line.split("[|]"); 
       // parse out the name and email 
       name = parts[1].trim(); 
       email = parts[2].trim(); 
       // rearrange the name 
       String[] nParts = name.split(" *"); 
       if (nParts.length == 3) { 
        name = nParts[1] + " " + nParts[2] + " " + nParts[0]; 
       } else { 
        name = nParts[1] + " " + nParts[0]; 
       } 
       // all done now, let's print the name and email 
       System.out.println(email + " " + name); 
      } 
     } 
     br.close(); 
    } catch (Exception e) { 
     System.out.println("There was an issue parsing the file."); 
    } 
} 
} 
+0

爲每個人創建一個類;每個類都有相應的方法。然後在主要方法中創建該類的對象,並按順序調用方法.... –

回答

0

@Ariel T擊敗了我。

說實話,你可以從許多不同的方式接近這一點。

假設,事實上,你已經得到了所有你所需要的代碼,我以爲我會只是提供了我會怎樣處理它的框架。

/* 
* Class Responsibility: read the file content and return it as a something usable 
*/ 
class MyFileReader { 
    public List<String> read(String filename) { 
     /* 
     * 1. open a file for reading 
     * 2. read each line and add it to a list 
     * 3. return the list (represents our file content) 
     */ 
    } 
} 

/* 
* Class Responsibility: take the content which we read from a file and turn it 
* into usable Java objects 
*/ 
class MyFileProcessor { 
    public List<Info> process(List<String> linesFromFile) { 
     /* 
     * 1. for each string (that represents a line from the input file) 
     * 2. split it into identifiable parts 
     * 3. create a new object to hold each of these parts 
     * 4. add that object to a list 
     * 5. return the list 
     */ 
    } 
} 

/* 
* Class Responsibility: write the java objects to a file 
* Note*: this is much easier if you override the toString method for your 
*   info object 
*/ 
class MyFileWriter { 
    public void writeToFile(List<Info> infoObjects, String filename) { 
     /* 
     * 1. open a file using the filename for writing 
     * 2. write some content 
     * 3. close file 
     */ 
    } 
} 

/* 
* Class Responsibility: hold the values that we care about 
*/ 
class Info { 
    private String name; 
    private int id; 
    private String email; 
    private String password; 

    public Info(String name, int id, String email, String password) { 
     this.name = name; 
     this.id = id; 
     this.email = email; 
     this.password = password; 
    } 

    /* 
    * ... getters and setters 
    */ 
} 

編輯:

main()保持正確的它在哪裏。其實,這是main(),將在他們的建造後使用你的課程!

檢查出來:我們將讀取該文件,修改一個對象,那麼這些對象寫回文件。

public class Main { 
    public static void main(String[] args) { 
     MyFileReader fileReader = new MyFileReader(); 
     MyFileProcessor fileProcessor = new MyFileProcessor(); 
     MyFileWriter fileWriter = new MyFileWriter(); 

     List<String> lines = fileReader.read("input-file.txt"); 
     List<Info> fileContentAsObjects = fileProcessor.process(lines); 

     if (fileContentAsObjects.size() > 0) 
     { 
      Info singleInfo = fileContentAsObjects.get(0); 
      System.out.println(singleInfo);    //if you've overridden toString() ;) 

      singleInfo.setName("changedName");   // modify it 

      fileContentAsObjects.remove(0);    // get rid of the old one 
      fileContentAsObjects.insert(0, singleInfo); // replace it with the same updated one 

      fileWriter.write(fileContentAsObjects);  // writes updated info object to file 
     } 
    } 
} 
+0

感謝兄弟你有果汁..你在哪裏建議我的main()去? – user3349184

+0

我爲你更新了:) –

0

JAVA沒有像C#那樣的部分類。通過使用聚合,委派和抽象基類,你幾乎可以做同樣的事情。

0

你要找的是什麼成分之間的邏輯分離。經驗法則是:

「如果明天我寫另一個程序,將 我能夠重用一些這個代碼?」

對於這種情況,想想不同部分在你的程序:

  1. 使用文件處理 - 讀/寫文件創建的實用功能。寫入到輸出流直接是一家集設計理念不是重定向System.out和使用System.out.println(明天你可能要寫入多個輸出流)編寫。這也是處理錯誤的地方。

  2. 處理字符串數據 - splittrimconcatenate等,您可以編寫一個函數,採用一個字符串輸入和輸出新的處理字符串根據需要。 (明天的輸入將來自網絡而不是文件系統)。

  3. 與另2個文件調用的函數和包裝過程中的主要功能的文件。

0

爲那些可重複使用的代碼部分(將來)做類。 所以文件讀取,寫入和處理文件內容的業務邏輯。因此,你的代碼應該用三種不同的類分開,其中有適當的方法。正如賈斯汀賽義德。 而main()方法將在一個新的Test類中,它將創建上面三個類的對象,並從它們中爲各個任務調用方法。

相關問題