如何在Java中将一个文件程序分解为多个类?
您好,我有一个工作程序,它读取一个txt文件(名称、id、电子邮件、密码),只将名称和电子邮件写入扩展名为.html的输出文件 我的问题是我的程序在一个班下运行。。但我的要求是我需要多个课程。。1个用于处理,1个用于读取,1个用于写入。你建议我如何分解我的文件?我有点困惑,谢谢你的指导如何在Java中将一个文件程序分解为多个类?,java,class,oop,Java,Class,Oop,您好,我有一个工作程序,它读取一个txt文件(名称、id、电子邮件、密码),只将名称和电子邮件写入扩展名为.html的输出文件 我的问题是我的程序在一个班下运行。。但我的要求是我需要多个课程。。1个用于处理,1个用于读取,1个用于写入。你建议我如何分解我的文件?我有点困惑,谢谢你的指导 import java.io.*; public class test { public static void main(String[] args) throws Exception{ // de
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.");
}
}
}
JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。您需要的是组件之间的逻辑分离。经验法则是: “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?” 对于这种情况,请考虑程序中的不同部分:
System.out
并使用System.out.println
写入(明天您可能需要写入多个输出流)。这也是处理错误的地方split
、trim
、concatenate
等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)您需要的是组件之间的逻辑分离。经验法则是: “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?” 对于这种情况,请考虑程序中的不同部分:
System.out
并使用System.out.println
写入(明天您可能需要写入多个输出流)。这也是处理错误的地方split
、trim
、concatenate
等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)您需要的是组件之间的逻辑分离。经验法则是: “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?” 对于这种情况,请考虑程序中的不同部分:
System.out
并使用System.out.println
写入(明天您可能需要写入多个输出流)。这也是处理错误的地方split
、trim
、concatenate
等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)您需要的是组件之间的逻辑分离。经验法则是: “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?” 对于这种情况,请考虑程序中的不同部分:
System.out
并使用System.out.println
写入(明天您可能需要写入多个输出流)。这也是处理错误的地方split
、trim
、concatenate
等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)@艾瑞尔没能比我抢先一步 老实说,你可以从许多不同的方式来处理这个问题 考虑到这一点,以及您已经获得了所需的所有代码,我想我只提供一个框架,说明如何实现它
/*
* 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
*/
}
@艾瑞尔没能比我抢先一步 老实说,你可以从许多不同的方式来处理这个问题 考虑到这一点,以及您已经获得了所需的所有代码,我想我只提供一个框架,说明如何实现它
/*
* 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
*/
}
@艾瑞尔没能比我抢先一步 老实说,你可以从许多不同的方式来处理这个问题 考虑到这一点,以及您已经获得了所需的所有代码,我想我只提供一个框架,说明如何实现它
/*
* 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
*/
}
@艾瑞尔没能比我抢先一步 老实说,你可以接近f