Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将一个文件程序分解为多个类?_Java_Class_Oop - Fatal编程技术网

如何在Java中将一个文件程序分解为多个类?

如何在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

您好,我有一个工作程序,它读取一个txt文件(名称、id、电子邮件、密码),只将名称和电子邮件写入扩展名为.html的输出文件

我的问题是我的程序在一个班下运行。。但我的要求是我需要多个课程。。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.");
    }
}
}

JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。

JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。

JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。

JAVA不像C#那样有分部类。通过使用聚合、委派和抽象基类,您可以做几乎相同的事情。

您需要的是组件之间的逻辑分离。经验法则是:

“如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?”

对于这种情况,请考虑程序中的不同部分:

  • 处理文件-创建用于读取/写入文件的实用程序函数。直接写入输出流是一种设计思想,而不是重定向
    System.out
    并使用
    System.out.println
    写入(明天您可能需要写入多个输出流)。这也是处理错误的地方

  • 处理字符串数据-
    split
    trim
    concatenate
    等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)

  • 一个包含main函数的文件,该函数调用其他两个文件上的函数并包装进程


  • 您需要的是组件之间的逻辑分离。经验法则是:

    “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?”

    对于这种情况,请考虑程序中的不同部分:

  • 处理文件-创建用于读取/写入文件的实用程序函数。直接写入输出流是一种设计思想,而不是重定向
    System.out
    并使用
    System.out.println
    写入(明天您可能需要写入多个输出流)。这也是处理错误的地方

  • 处理字符串数据-
    split
    trim
    concatenate
    等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)

  • 一个包含main函数的文件,该函数调用其他两个文件上的函数并包装进程


  • 您需要的是组件之间的逻辑分离。经验法则是:

    “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?”

    对于这种情况,请考虑程序中的不同部分:

  • 处理文件-创建用于读取/写入文件的实用程序函数。直接写入输出流是一种设计思想,而不是重定向
    System.out
    并使用
    System.out.println
    写入(明天您可能需要写入多个输出流)。这也是处理错误的地方

  • 处理字符串数据-
    split
    trim
    concatenate
    等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)

  • 一个包含main函数的文件,该函数调用其他两个文件上的函数并包装进程


  • 您需要的是组件之间的逻辑分离。经验法则是:

    “如果明天我再写一个程序,我会吗 我能够重用其中一些代码吗?”

    对于这种情况,请考虑程序中的不同部分:

  • 处理文件-创建用于读取/写入文件的实用程序函数。直接写入输出流是一种设计思想,而不是重定向
    System.out
    并使用
    System.out.println
    写入(明天您可能需要写入多个输出流)。这也是处理错误的地方

  • 处理字符串数据-
    split
    trim
    concatenate
    等。您可以编写一个函数,该函数接受字符串输入,并根据需要输出新处理的字符串。(明天输入将来自web而不是文件系统)

  • 一个包含main函数的文件,该函数调用其他两个文件上的函数并包装进程


  • @艾瑞尔没能比我抢先一步

    老实说,你可以从许多不同的方式来处理这个问题

    考虑到这一点,以及您已经获得了所需的所有代码,我想我只提供一个框架,说明如何实现它

    /*
    * 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