您好我有一个工作程序,它读取一个txt文件(名称,ID,电子邮件,密码),只将名称和电子邮件写入.html扩展名的输出文件......
我的麻烦是我让程序在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 :(得分:1)
您正在寻找的是组件之间的逻辑分离。经验法则是:
"如果明天 我会写另一个程序 我可以重用一些代码吗?"
对于这种情况,请考虑一下程序中的不同部分:
处理文件 - 创建用于读/写文件的实用程序功能。直接写入输出流是一种设计理念,而不是重定向System.out
并使用System.out.println
进行编写(明天您可能希望写入多个输出流)。这也是处理错误的地方。
处理字符串数据 - split
,trim
,concatenate
等。您可以编写一个函数,该函数接受字符串输入并根据要求输出新处理的字符串。 (明天输入将来自网络而不是文件系统)。
具有main函数的文件,该函数调用其他2个文件上的函数并包装该进程。
答案 1 :(得分:0)
JAVA没有像C#那样的部分类。您可以使用聚合,委派和抽象基类来完成相同的任务。
答案 2 :(得分:0)
说实话,你可以通过多种不同的方式来解决这个问题。
考虑到这一点以及你已经拥有了所需的所有代码这一事实,我认为我只是提供了一个框架来说明我将如何处理它。
/*
* 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
}
}
}
答案 3 :(得分:0)
为那些可重用的代码部分(将来)创建类。 因此文件读取,写入和处理文件内容的业务逻辑。 因此,您的代码应该在三个不同的类中分开,并使用适当的方法。正如贾斯汀赛义德。 而main()方法将在一个新的Test类中,它将创建上面三个类的对象,并从它们调用各个任务的方法。