你好我有一個工作程序,讀取一個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.");
}
}
}
爲每個人創建一個類;每個類都有相應的方法。然後在主要方法中創建該類的對象,並按順序調用方法.... –