简体   繁体   English

如何避免在java中硬编码文本文件

[英]How to avoid hardcoding text files in java

I'm working on a project where 3 text files stored into a folder.我正在处理一个项目,其中 3 个文本文件存储到一个文件夹中。 I'm trying to avoid hardcoding the 3 paths into the source file.我试图避免将 3 个路径硬编码到源文件中。

That being said, I would like to run the program by typing at the prompt:java sourceCode folderName.话虽如此,我想通过在提示符下键入来运行该程序:java sourceCode folderName。

Please see below what I've achieved so far请在下面查看我迄今为止取得的成就

:

    import java.util.Scanner ;
    import java.io.*;

    public class Indexes {

        //main method
        public static void main (String[] args)throws IOException {

            for (String MyFolder: args) {
                Scanner textFile =new Scanner(inputFile);

                //read the inputFile
                inputFile=textFile.nextLine();

                //two other objects
                File input =new File(inputFile);
                Scanner scan=new Scanner(input);

                int thesize=0;  
                int page=0;

                while (scan.hasNext()){

                    String readprt=scan.next();
                    //char[] c = s.toCharArray(); //conversion into character

                    char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
                    int size=charArray.length ; //length of word

                    thesize +=size;

                    if (thesize <=100){

                    }
                }
            }
        }
    }





import java.util.Scanner ;
import java.io.*;


public class Indexes {


    /*

    //creation of constructor
    public ReadFile (String inf){


    }

    public WriteFile (String outf){


    } */


    //main method
    public static void main (String[] args)throws IOException {

        for (String MyFolder: args) {

            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby.txt";
            //System.out.println(MyFolder);



        //create objects for ("huckfinn.txt")

        //String inputFile="huckfinn.txt" ; //, outputFile="huckfinn_output.txt" ;
        String inputFile=MyFolder ;
        Scanner textFile =new Scanner(inputFile);

        //read the inputFile
        inputFile=textFile.nextLine();

        //write into  the outputFiles by defining an object for each of the files

        PrintWriter printer1 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn_output.txt");
        PrintWriter printer2 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap_output.txt");
        PrintWriter printer3 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby_output.txt"); 

        //two other objects
        File input =new File(inputFile);
        Scanner scan=new Scanner(input);

        int thesize=0;  
        int page=0;



        while (scan.hasNext()){

            String readprt=scan.next();

            //char[] c = s.toCharArray(); //conversion into character


            char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
            int size=charArray.length ; //length of word




                        /*printer1.println(readprt+":"+" "+size); */
                        //printer1.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer2.println(readprt+":"+" "+size);
                        //printer2.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer3.println(readprt+":"+" "+size);
                        //printer3.println("Size of"+" "+readprt+":"+ " "+counter);


                                                 //read next word on the next line 
                    /* }
                }
            }  */

            //printer1.println(readprt);
            //printer2.println(readprt);
            //printer3.println(readprt); 


                                    // the government of the people by the people

                                    thesize +=size;

                                    if (thesize <=100){

                                        printer1.println(readprt+":"+" "+size);
                                        printer2.println(readprt+":"+" "+size);
                                        printer3.println(readprt+":"+" "+size);
                                        continue ;
                                    } else {

                                        page ++;

                                        printer1.println("character count <=100:"+ " "+(thesize-size));
                                        printer1.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer2.println("character count <=100:"+ " "+(thesize-size));
                                        printer2.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer3.println("character count <=100:"+ " "+(thesize-size));
                                        printer3.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        thesize=0;

                                    }



            }
        //close objects 
        printer1.close();
        printer2.close();
        printer3.close(); 




        //object to read in methods
        //Index ReadObj = new Index();



        }
    }
}

I am also new to JAVA, but I guess the question is very basic.我也是 JAVA 的新手,但我想这个问题非常基本。

If you already know what is the path then you should create a .properties file for your project and read the path from this configuration file.如果您已经知道path是什么,那么您应该为您的项目创建一个.properties文件并从这个配置文件中读取路径。

If you can know the path only on run time, the you should read it from args inserted in command line (or other input resource available in run time).如果您只能在运行时知道path ,您应该从命令行中插入的args (或运行时可用的其他输入资源)中读取它。

if the the prefix of the path is known but the suffix isn't then you should use the combination of the two approaches above and do the following:如果path的前缀已知但后缀未知,则应结合使用上述两种方法并执行以下操作:

String fullPath = prefix + suffix;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM